Yearly Archives: 2021

Home-2021

Python Class

Anatomy A python class is defined using the 'class' keyword followed by the class name and a colon.  Instance variables and methods will be defined as shown below with proper indentation. The constructor (__init__) and the destructor (__del__) are both optional. class Dog: x = 0 def __init__(self): print('Constructor executed') def __del__(self): print('Destructor executed') def

By |2021-07-17T03:20:21+00:00July 17th, 2021|Categories: Python|0 Comments

Parsing JSON with Python

Using the json Library The loads method in the json library returns a dictionary for anything in curly brackets and a list for anything in square brackets. The following code shows interaction with dictionaries. import json data = '''{ "name": "John", "phone": { "type": "intl", "number": "+1 111 222 3333" }, "email": { "hide": "yes"

By |2021-02-06T21:04:09+00:00February 6th, 2021|Categories: Python|0 Comments

Parsing XML with Python

Using the ElementTree library in Module xml Example 1: Selecting elements with findall import xml.etree.ElementTree as ET input = '''<stuff> <users> <user x="x1"> <id>001</id> <name>John</name> </user> <user x="x2"> <id>002</id> <name>Sam</name> </user> </users> </stuff>''' stuff = ET.fromstring(input) lst = stuff.findall('users/user') print('User count:', len(lst)) for item in lst: print('Name:', item.find('name').text) print('ID:', item.find('id').text) print('Attribute:', item.get('x'))   Example 2:

By |2021-01-23T21:43:12+00:00January 23rd, 2021|Categories: Python|0 Comments

Python HTTP

Using Sockets The following is similar to a telnet session, where we first connect to the server, we submit a GET request, and we read the response. import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mysock.connect( ('data.pr4e.org', 80) ) cmd = 'GET HTTP://data.pr4e.org/romeo.txt HTTP/1.0\r\n\r\n'.encode() mysock.send(cmd) while True: data = mysock.recv(512) if (len(data) < 1): break print(data.decode()) mysock.close()

By |2021-01-23T19:45:22+00:00January 18th, 2021|Categories: Python|0 Comments

Regular Expressions

To use regular expressions, import re. Use re.search() to see if a string matches a regular expression. Use re.findall() to extract strings that match a regular expression. Reference: https://docs.python.org/3/howto/regex.html Match a Regular Expression import re fileH = open('file.txt') for line in fileH: line = line.rstrip() if re.search('From: ', line): print(line) Extract Substring Matching a Regular

By |2021-01-03T02:26:52+00:00January 3rd, 2021|Categories: Python|0 Comments

Tuple

A tuple is an immutable list of values.  Iteration and indexes work the same way as lists, but they cannot be modified.  While list literals use square brackets, tuple literals use parenthesis. x = (1,4,6) print(x[0]) 1 for val in x: print(val) Assignment (x,y) = (4,'Fred') print(y) Fred print(x) 4 Iteration Method items() in dictionary

By |2021-01-02T04:27:26+00:00January 2nd, 2021|Categories: Python|0 Comments

Dictionary

A dictionary is a collection that stores data in key/value pairs and is equivalent to a java map.  Keys can be of any immutable type. Create items = dict() # or items = {} items['apple'] = 12 items['grape'] = 5 items['orange'] = 7 print(items) {'apple': 12, 'grape': 5, 'orange': 7} print(items['apple']) 12 otherItems = {'apple':

By |2021-01-01T19:24:10+00:00January 1st, 2021|Categories: Python|0 Comments

Lists

A list is an ordered collection that can contain elements of any different type, including other lists.  Iteration on a list can be performed through a for-in loop.  Lists are mutable. print([1, 'two', 3]) print([]) Specific items in a list can be retrieved using indexes: list = [1,2,3,4] first = list[0] Range Function The range

By |2021-01-01T04:25:44+00:00January 1st, 2021|Categories: Python|0 Comments

Text Files

This entry provides code to read text files in python.  The open function returns a handle that can be treated as a sequence of strings.  The file handle also has a read method that returns the file content in a string. Reading Files A file handle open for read can be treated as a sequence

By |2021-01-01T02:34:03+00:00January 1st, 2021|Categories: Python|0 Comments
Go to Top