This is some quick documentation of standard Python functions, modules and objects used in Network Programming. This is by no means an exhaustive list, nor are the explanations complete. This is intended to focus on that which is needed for this course and not confuse the reader with information not needed.
Some text on this is page was copied from the above official Python documentation pages. To provide a more gentle introduction, in most cases the copied text was condensed and simple examples added.
Cast a variable to an integer
|Parameters:||x (long, string or float) – value to convert|
Cast a variable to a floating point number
|Parameters:||x (integer, long or string) – value to convert|
Convert a variable to a string
|Parameters:||x (integer, long or most any object) – value to convert|
Note that most classes include a __str__() method, which returns an appropriate string representation of the object. Python calls this method when str(obj) is called.
With a single argument iterable, return the smallest item of a non-empty iterable (such as a string, tuple or list). With more than one argument, return the smallest of the arguments.
With a single argument iterable, return the largest item of a non-empty iterable (such as a string, tuple or list). With more than one argument, return the largest of the arguments.
Returns a list of strings containing the names of all the attributes (data and methods) contained in the module.
Built-in sequence storage container
Remove item i from list s
s.append(x) – Add item x to end of list s
s.extend(x) – Add items from list x to the end of list s
s.count(x) – Return number of i‘s for which s[i] == x
s.index(x) – return smallest k such that s[k] == x
s.insert(i, x) – add item x to s at position i, such that s[i] == x
s.pop([i]) – Remove and return item from s, at position i. Default value of i is len(s) - 1, which is also -1 (the last item). Same as x = s[i]; del s[i]; return x
s.remove(x) – Remove the first item from s whose value is x. Same as del s[s.index(x)]
s.reverse() – reverses the items of s in place
s.sort([cmp[, key[, reverse]]]) – sort the items of s in place
Built-in dictionary (associative array) storage container
Return the number of items in the dictionary d.
Return the item of d with key key. Raises a KeyError if key is not in the map.
Set d[key] to value.
Remove d[key] from d. Raises a KeyError if key is not in the map.
Return True if d has a key key, else False.
Equivalent to not key in d.
Return an iterator over the keys of the dictionary. This is a shortcut for iterkeys().
Remove all items from the dictionary.
Return a shallow copy of the dictionary.
Create a new dictionary with keys from seq and values set to value.
fromkeys() is a class method that returns a new dictionary. value defaults to None.
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
Test for the presence of key in the dictionary. has_key() is deprecated in favor of key in d.
Return a copy of the dictionary’s list of (key, value) pairs.
Return an iterator over the dictionary’s (key, value) pairs.
Return an iterator over the dictionary’s keys.
Return an iterator over the dictionary’s values.
Return a copy of the dictionary’s list of keys.
If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.
Remove and return an arbitrary (key, value) pair from the dictionary.
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.
Return a copy of the dictionary’s list of values.
Functions from the string module operate on data type objects of type str. Because str is a sequence type, meaning that it can used in a for loop and in slicing operations (see Using and Generating Python Sequences), one might think of a string as a mutable data container, such as Built-in List Operations. However, like other built-in data types, such as int and float, the str class defines an immutable data type. This means that an assignment statement must be used to modify a string:
>>> import string >>> st = "Bob" >>> st.replace('ob','ill') # This only displays the returned value 'Bill' >>> st 'Bob' >>> st = st.replace('ob','ill') # An assignment makes a change >>> st 'Bill'
It is common to see string module functions expressed in one of two ways:
>>> import string >>> st = "Hello, world" >>> st = st.replace('world', 'class') >>> st1 = string.replace(st, 'Hello', 'Good night') >>> print st, '--', st1 Hello, class -- Good night, class
The first syntax, which uses the object_name.method() notation, is more commonly used than the later. However, to make it clear that these functions are part of the string module and to be consistent with the official Python documentation of string functions, the later notation is used. In each case, the variable st represents the string being operated on.
Return a copy of string st with all occurrences of substring old replaced by new.
Return a string which is the concatenation of the strings in the sequence seq. The separator between elements is st. join() is often used with a string literal, which appears a little strange the first time ones sees it:
>>> se = ['And', 'now,', 'for', 'something', 'completely', 'different'] >>> st = ' '.join(se) >>> st 'And now, for something completely different'
Return a list of the words of the string st. If the optional second argument sep is absent, the words are separated by arbitrary strings of whitespace characters (space, tab, newline). If the second argument sep is present, it specifies a string to be used as the word separator.
>>> st = "I like pizza" >>> st.split() ['I', 'like', 'pizza'] >>> st.split('i') ['I l', 'ke p', 'zza']
Return True if st starts with the prefix, otherwise return False.