notes blog about

2016-05-13

All Python objects are instances of one class or another.

Variables

Python variables are more like labels that reference various objects (integers, strings, class instances, …):

x = "2"     # x is string
x = int(x)  # x is now integer

Built-in data types

Built-in data types can be manipulated with operators, built-in functions, library functions, or a data type’s own methods.

numbers

>>> type(1)
<class 'int'>
>>> type(1.1)
<class 'float'>

strings

Strings are immutable.

Raw strings (r"") do not interpret escape sequences.

>>> print(r"hello\nworld")
hello\nworld
>>> print("hello\nworld")
hello
world

>>> x = "Do not meddle    in \the affairs of wizards"
>>> x.replace("\t", "t")
'Do not meddle    in the affairs of wizards'

>>> import re
>>> tabs = re.compile(r"[\t ]+")
>>> tabs.sub(" ", x)
'Do not meddle in he affairs of wizards'

lists

[]
[1]
[1, "two", (3, 4), ["a", "b"]]

tuples

immutable lists

(0, 1, 2)
(1, ) # one element tuple

dictionaries

{ 1: "one", 2: "two" }

sets

>>> x = set([1, 2, 3])
>>> 1 in x
True
>>> 4 in x
False

file objects

f = open("myfile", "r")
line1 = f.readline()
f.close()

What is Truth

Exceptions

#!/usr/bin/env python3
# Argument parsing and error handling

import argparse

parser = argparse.ArgumentParser(description="This program's description")
parser.add_argument('-f', type=str, help='Name of file to open', required=True)

cmdargs = parser.parse_args()
f = cmdargs.f

try:
    fh = open(f)
    line = fh.readline()
except Exception as e:  # generic exception;
                        # more specific exceptions could be caught before
    print("There was an error:", e)
    exit(1) # like tchrist said
else:
    print("1st line from", f, "is", line, end="")
    fh.close()

Calling to System

os module

import os
os.getcwd()
os.getenv('PATH')
os.system('ls -la')

subprocess module

from subprocess import call
call(['ls', '-la'])

Filenames and Paths

#!/usr/bin/env python3

import os
import sys

def walk(dirname):
    """Walk through a directory and print names of files
    """
    for name in os.listdir(dirname):
        path = os.path.join(dirname, name)
        if os.path.isfile(path):
            print(path)
        elif os.path.isdir(path):
            walk(path)

if __name__ == "__main__":
    walk(sys.argv[1])

Threading

Not really a Python’s strength due to GIL (see Golang).

#!/usr/bin/env python3

import threading
import time

class aThread(threading.Thread):
    def __init__(self, num, val):
        threading.Thread.__init__(self)
        self.threadNum=num
        self.loopCount=val

    def run(self):
        print("Starting to run thread: ", self.threadNum)
        myfunc(self.threadNum, self.loopCount)

def myfunc(num, val):
    count=0
    while count < val:
        print(num, " : ", val*count)
        count=count+1
        #time.sleep(1)

t1=aThread(1, 15)
t2=aThread(2, 20)
t3=aThread(3, 30)

t1.start()
t2.start()
t3.start()

threads = []
threads.append(t1)
threads.append(t2)
threads.append(t3)

# wait for all threads to complete by entering them
for t in threads:
    t.join()

Classes and instances

#!/usr/bin/env python3

class me:
    def __init__(self, foo):
        self.myvar = foo

    def getval(self):
        return self.myvar

    def setval(self, bar):
        self.myvar = bar

x = me('bla')
y = x.getval()
z = me('baz')
print(y)
x.setval('ble')
print(x.getval())
print(z.getval())

Sources