Python

From Code Self Study Wiki
Jump to: navigation, search

This page has notes about Python.

Python 2 to Python 3 Conversion[edit]

Resources[edit]

Python Fu:

Algorithms and Data Structures[edit]

Other things to research[edit]

Save Interactive Session[edit]

import readline
readline.write_history_file('history.txt')

Windows doesn't have readline. On Windows, you can pip install pyreadline and check these docs for more info.

Code Documentation[edit]

Python HTTP Servers[edit]

Python 2[edit]

Navigate in the terminal to your directory. Then type this:

python -m SimpleHTTPServer

Then go to localhost:8000 in the browser.

Or provide a port number:

python -m SimpleHTTPServer 5000

Python 3[edit]

python -m http.server

If Python 3 isn't the default, you may need to use this variation:

python3 -m http.server

Strings[edit]

help(str):

  1. capitalize -- first character
  2. center(width[, fillchar]) -- defaults to space
  3. count(sub[, start[, end]]) -- count number of occurances in sub
  4. decode -- character encoding
  5. encode --
  6. endswith(suffix[, start[, end]]) -- returns true if string ends with suffix, else false. Suffix can also be a tuple of strings.
  7. expandtabs([tabsize]) -- replaces tabs with spaces. Default 8.
  8. find(sub [,start [, end]]) -- return lowest index in the string where sub is found.
  9. format(*args, **kwargs) -- return formatted version of string using substitutions from args and kwargs {}
  10. index(sub [,start [,end]]) -- like find but raise ValueError when substring not found
  11. isalnum -- True if all characters are alphanumeric and there is at least one character.
  12. isalpha -- alphabetic
  13. isdigit -- digits
  14. islower -- true if all are lowercase and there is at least one character
  15. isspace -- if all whitespace
  16. istitle -- return true if all titlecased
  17. isupper -- if all uppercase
  18. join(iterable) -- return a string which concatenates iterable with the string as a separator
  19. ljust(width[, fillchar]) -- left justified
  20. lower -- lowercase
  21. lstrip([chars]) -- returns copy with leading whitespace removed or remove chars instead.
  22. partition(sep) -- search for sep and return head, sep, tail
  23. replace(old, new[, count]) -- return copy with count recurrances replaced
  24. rfind(sub [,start [,end]]) return the highest index where sub is found. Return -1 on not found.
  25. rindex -- like rfind but raise ValueError when not found
  26. rjust -- see ljust
  27. rpartition(sep) -- see partition, searches from right
  28. rsplit([sep [,maxplit]]) -- return list of words using sep as delimiter starting at the end.
  29. rstrip([chars]) -- return copy with trailing space removed
  30. split([sep [,maxsplit])
  31. splitlines([keepends]) -- breaks on line boundaries, doesn't include line breaks unless keepends is True.
  32. startswith(prefix[, start[, end]]) -- returns true if stars with prefix
  33. strip([chars]) -- removes leading and trailing whitespace (or chars)
  34. swapcase
  35. title -- titlecase
  36. translate(table [,deletechars]) -- translates characters
  37. upper -- conver to uppercase
  38. zfill(width) -- pad a numeric string with zeros on the left

Python Lists[edit]

a = ["an","array","of","things", [1,2,3]]
 
# Example method:
a.append("Another item")
 
# Get information on available methods:
dir(a)
  • append
  • count
  • extend
  • index
  • insert
  • pop
  • remove
  • reverse
  • sort

List Slices[edit]

# Get a slice. In this case elements 2 and 3 but not 4. Starts at zero.
a[2:4]
 
# Gets elements 0, 1, 2
a[:3]
 
# Gets all elements
a[:]

Iteration with Index[edit]

months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
days_in_month = [31, 29, 31, 30, 31, 30, 31, 31, 30 ,31, 30, 31] # Using max possible days in Feb
for idx, month in enumerate(months):
    print('{}: {}'.format(month, str(days_in_month[idx])))

Returns:

January: 31
February: 29
March: 31
April: 30
May: 31
June: 30
July: 31
August: 31
September: 30
October: 31
November: 30
December: 31

Side note: you can also zip lists together into an iterable with zip():

for month, days in zip(months, days_in_month):
    print(month, days)

Searching in Lists[edit]

# Returns true or false
'Hammer' in a
# If statement
if 'Hammer' in a:
    # do something

Functional Tools for Lists[edit]

filter()[edit]

Applies a function to each item and returns items that return true. It returns a list unless given a string or tuple.

# Return true if num isn't divisible by 2 and 3
def f(num):
    return x % 2 != 0 and x % 3 != 0
 
# Return all in the range where it's true
filter(f, range(1, 100))

map()[edit]

Applies a function to each item in a list.

# Create a function to cube numbers
def cube(num):
    return num * num * num
 
# Apply the function to a range of numbers from 1-10 inclusive
map(cube, range(1, 11))

You can pass in a couple of arguments:

# Create a list
seq = range(10)
 
# Adds two numbers
def add(x, y):
    return x + y
 
# Adds each item of the list as x and y
map(add, seq, seq)
# returns [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

reduce()[edit]

Does something to the first two items of the list, then the next items one by one. It returns a single value. Example: add up all numbers between 1-10:

# Adds two numbers
def add(x, y):
    return x + y
 
# Adds all numbers in a range
reduce(add, range(1, 11))

Read more in the docs.

List Comprehensions[edit]

Shorthand for creating lists:

# Create a list of squares
[x**2 for x in range(10)]

Another example from the docs:

# Create tuple pairs where the numbers are different
[(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
 
# Returns: [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Fun Examples[edit]

English alphabet:

[chr(c) for c in range(ord('a'), ord('z')+1)]

Prime numbers with nested list comprehensions:

prime_numbers = [x for x in range(2, 500) if x not in [j for i in range(2, 8) for j in range(i*2, 500, i)]]

Lambda[edit]

Create an anonymous function and execute it immediately:

squares = map(lambda x: x**2, range(10))

Python Dictionaries[edit]

Tip: Python dicts don't preserve order.

a = { 'name':'John', 'age':'30' }
a['name'] # John
a.keys() # ['age', 'name']
'name' in a # true
dir(a) # get info about available methods

Dictionaries can contain arrays:

a = { 'name':'Bob', 'hobbies':['jogging','programming','cooking'] }
a['hobbies'][2] # cooking
a['hobbies'].append('stamp collecting')
  1. clear -- remove all items
  2. copy -- shallow copy
  3. fromkeys[S[,v]) -- new dict with keys from S and values equal to v. V defaults to None.
  4. get(k[,d]) -- D[k] if k in D, else d. d defaults to None.
  5. has_key(k) -- True if has key k
  6. items -- list of key value pairs as 2-tuples
  7. keys -- list of keys
  8. pop(k[,d]) -- remove k and return value. If key not found then d is returned if it's there, else KeyError
  9. popitem -- remove and return key value pair as a 2-tuple
  10. setdefault(k[,d]) -- get(k,d), also set it if k not in the dict
  11. update([E, ]**F) -- e.g., dict1.update(dict2) would add dict2 items to dict1
  12. values -- list of values
  13. viewitems -- a set-like object with a view of the dict's items
  14. viewkeys -- a set-like object with a view of the dict's keys
  15. viewvalues -- like viewitems but for values

for Loops[edit]

Lists:

for item in fruit:
    print item

To get the index, you can use enumerate:

for idx, val in enumerate(mylist):
    print idx, val

Dictionaries:

# note that the order of the dictionary won't be maintained
for key in fruit:
    print key + ": " + fruit[key]

while Loops[edit]

i = 0 
while (i < len(people)):
    print people[i]
    i = i + 1 # NOT i++

Iterables[edit]

You can get the index of an iterable with enumerate:

for i, v in enumerate(my_list):
    print i, v

You can combine to iterables with zip:

for  name, height in zip(names, heights):
    print "%s is %d meters tall" % (name, height)

Functions[edit]

def my_function(arg):
    # do something

Working with Files[edit]

Not done well, but just a quick example of methods and options to read in some text, split the words and then write a new file as a list of words:

import string
f = open('some_content.txt', 'r')
x = f.read()
out = x.translate(string.maketrans("",""), string.punctuation)
z = out.split()
 
# Better to use `while`
q = open('wordlist3.txt', 'a+')
for word in z:
    q.write(word + '\n')
q.close()

Databases[edit]

MySQL[edit]

$ pip install mysqlclient # a fork of MySQL-python

Then:

import MySQLdb as mdb
import sys
 
try:
    con = mdb.connect('host', 'user', 'password', 'database');
 
    cur = con.cursor()
    cur.execute("SELECT stuff FROM table_name LIMIT 10")
 
    for row in cur.fetchall():
        print row[0]
 
except mdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)
 
finally:
    if con:
        con.close()

Error Handling[edit]

import sys
 
try:
    # do something that causes an error
except:
    print "exception",sys.exec_info()[0]
 
# the program will print the error and then continue...

Debugging[edit]

code.interact[edit]

Quick and easy:

import code
code.interact(local=locals())

pdb[edit]

https://docs.python.org/3.4/library/pdb.html

ipdb[edit]

Installation: pip install ipdb

Running it: python -m ipdb myfile.py

Tutorials:

To research:

Testing[edit]

See Python Testing.

Profiling[edit]

Unicode and Character Sets[edit]

import chardet
chardet.detect("hi there")
# =>  {'confidence': 1.0, 'encoding': 'ascii'}
 
x = "asfd"
x.decode()
# => u'asdf'

RegEx[edit]

Configuration Files[edit]

Check out configparser, shlex, and json.

There is also pickle, though it's a binary format and not human-readable.

Bottle[edit]

See Bottle Python Web Framework.

Flask[edit]

See Flask Python Web Framework.

More Python[edit]

See also:

Don't forget Virtualenv.