python cheatsheet

Preface

Source:

List

copy a list:

y = x[:]

append items to list:

>>> x = [1, 2]
>>> x + [3, 4]
[1, 2, 3, 4]

multiply a list:

>>> a = [1, 3]
>>> a * 2
[1, 3, 1, 3]

by default, nested list are copied by reference, use copy.deepcopy() to make a deep copy:

>>> original = [[0], 1]
>>> shallow = original[:]
>>> import copy
>>> deep = copy.deepcopy(original)
>>> original
[[0], 1]
>>> shallow
[[0], 1]
>>> deep
[[0], 1]

>>> shallow[0][0] = 'zero'
>>> shallow
[['zero'], 1]
>>> original    # the original is modified, too
[['zero'], 1]
>>> deep        # deep copy is not affected
[[0], 1]

Tuples

NOTES tuples are more efficient than lists, they are mostly used as dictionary keys

tuples can't be modified, but if it contains modifiable elements, those elements can be modified:

>>> a = [1, 2]
>>> b = (a, 3)
>>> b[0][0]
1
>>> b[0][0] = 100
>>> b
([100, 2], 3)
>>> a[1] = 'first'
>>> b
([100, 'first'], 3)
>>> b[1] = 'abc'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

multiple assignments in one line:

>>> (one, two) = (1, 2)
>>> one 
1
>>> two
2

>>> one, two = 1, 2  # the same, but even more simple
>>> one 
1
>>> two
2

swapping variables:

>>> a = 1
>>> b = 2
>>> a, b = b, a
>>> a
2
>>> b
1

in python 3, a even more powerful feature, use a * to receive multiple items as a list:

>>> x = (1, 2, 3, 4)
>>> a, b, *c = x
>>> a, b, c
(1, 2, [3, 4])
>>> a, *b, c = x
>>> a, b, c
(1, [2, 3], 4)
>>> *a, b, c = x
>>> a, b, c
([1, 2], 3, 4)
>>> a, b, c, d, *e = x
>>> a, b, c, d, e
(1, 2, 3, 4, [])

this can also be done with list:

>>> [a, b] = 1, 2
>>> c, d = [10, 20]
>>> a, b, c, d
(1, 2, 10, 20)

Set

set operations:

>>> s1 = {1, 2}
>>> s2 = {2, 3}
>>> s1 | s2
{1, 2, 3}
>>> s1 & s2
{2}
>>> s1 ^ s2
{1, 3}

NOTES set's elements must be immutable, but set itself is mutable

frozenset is immutable, and can be used as set element:

>>> s = {1, 2}
>>> s.add(3)
>>> s
{1, 2, 3}

>>> s2 = frozenset(s)
>>> s2.add(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

>>> s.add(s2)
>>> s
{1, 2, 3, frozenset({1, 2, 3})}

Strings vs. Bytes

Bytes are bytes; characters are an abstraction. An immutable sequence of Unicode characters is called a string. An immutable sequence of numbers-between-0-and-255 is called a bytes object.

bytes object is immutable, you can change a single byte by convert it to bytearray first

    In [29]: ba = bytearray(b)

    In [30]: ba
    Out[30]: bytearray(b'abc')

    In [31]: ba[0] = 65

    In [32]: ba
    Out[32]: bytearray(b'Abc')

Functions

lambda function:

>>> foo = lambda x: x ** 2
>>> foo(3)
9

generator funtion:

def gen():
    x = 0
    while x < 5:
        yield x     # generate a value
        x += 1

for i in gen():
    print(i)

decorators:

def my_decorator(func):
    print('in my_decorator, decorating', func.__name__)
    def wrapper_func(*args):
        print("Executing", func.__name__)
        func(*args)
    return wrapper_func

@my_decorator   # decorator's name
def my_func(x):
    print(x ** 2)

my_func(3)

outputs:

in my_decorator, decorating my_func
Executing my_func
9

Module

three forms of import:

import modulename
from modulename import name1, name2, ...
from modulename import *

The * stands for all the exported names in modulename. This imports all public names from modulename—that is, those that don't begin with an underscore, and makes them available to the importing code without the necessity of prepending the module name. But if a list of names called __all__ exists in the module (or the package's __init__.py), then the names are the ones imported, whether they begin with an underscore or not.

Set Module Searching paths

scoping and namespaces

there are always three namespaces in python: local, global, built-in

The global namespace of a function is the global namespace of the containing block of the function (where the function is defined). It's independent of the dynamic context from which it's called.

the 'sys' module

sys.path        module importing paths list
sys.argv        arguments
sys.stdin
sys.stdout
sys.stderr
sys.exit

redirect standard out to a file:

import sys

class RedirectStdoutTo:
    def __init__(self, out_new):
        self.out_new = out_new

    def __enter__(self):
        self.out_old = sys.stdout
        sys.stdout = self.out_new

    def __exit__(self, *args):
        sys.stdout = self.out_old

print('A')
with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):
    print('B')
print('C')

filesystem, path related

create dirs

remove dirs

copy dirs

useful functions:

files

use file object as an iterator, iterate all lines in it:

file_object = open("myfile", 'r')
count = 0
for line in file_object:
    count = count + 1
print(count)
file_object.close()

read file lines to a list:

a = [l.rstrip() for l in open('/path/to/file')]

open and close files using with statement:

with open('path/to/file', encoding='utf-8') as a_file:
    line_count = 0
    for line in a_file:
        line_count += 1
    print(line_count)

Classes / Object-oriented programming / OOP

Regular Expressions

syntax

only syntaxes need attentions are listed here

compiled vs. simple functions

prog = re.compile(pattern)
result = prog.match(string)

is equivalent to

result = re.match(pattern, string)

compiled form is more efficient when the expression will be used several times in a single program

flags

raw string notation

\ do not have any specail meaning in raw strings

r'\n' means a string with two characters \ and n

'\n' is a one-character string containing a newline

so raw strings are often used in regular expression patterns

use triple quotes as delimiter when you want to include a quote mark in the string

In [71]: r"""hello ' " quotes"""
Out[71]: 'hello \' " quotes'

functions

Python 3

encoding

strings

Style tips

naming conventions

package: all_lower_case
module: all_lower_case_and_short
class: CamelCase, _CamelCase for internal use
exception: SomeError

function: lower_case
global variable: lower_case

constants: ALL_CAPS

uninstall a package

sudo pip uninstall phpsh

and remove any remaining files

locate phpsh