Notes on Python
General tips
In Linux you can make a python file executalbe by setting the first line of the file to:
#!/usr/bin/python3
Set the executable permision with chmod a+x somefile.py
In windows kun je een .py file runnen door er op te dubbel klikken. Je moet de .py extentie dan
associeren met met pythonw.exe die staat in ...\AppData\Local\Programs\Python\Python36-32
The Windows python launcher, py
py is an executable which is installed in C:\Windows so that it is available without requiring
PATH modifications.
You can start a specific version by specifying it on the command line
py -3.5
met py --list kun je een lijst krijgen van python versies op je systeem. Er staat een * achter de default versie.
In the environment variable PY_PYTHON kun je aangeven welke versie py als default gebruikt, bijvoorbeeld:
PY_PYTHON=3.8
print(sys.version) # Print the python version with which the current script is running
Virtual environment:
python3 -m venv venv
pip install -r requirements.txt
pip freeze > requirements.txt
see also https://docs.python.org/3/tutorial/venv.html
Managing site-packages:
python -m pip install somepackage # installs somepackage
Usefull site-packages:
pywin32 # win32api, win32security
Indentation:
print('a'); print('b') # The semicolon can be used to separate statements
if 4 < 5: # Normally indentation is used to make compount statements
print('a')
print('b')
if 4 < 5: print('a') # one line statements can be placed at the same line
if 4 < 5: print('a'); print('b') # this is ok too
pass # The pass keyword is a null operation. It is useful as a placeholder when a statement is required syntactically but no code needs to be executed
Printing stuff:
print('hello world') # print to standard output
print('hello world', end = '') # idem, but supresses new-line at the end
print('hoi', end='') # print without the automatic newline
print(result) # result can be a lot of types, like integer, string, etc.
Stopping a program:
exit()
Getting user input from console:
s = input()
strings
s2 = s[2] # getting a substring
s2 = s[3:5] # get substring from indeces [3...5)
s2 = s[:5] # get substring from indeces [0...5)
s2 = s[3:] # get substring from indeces [3...last]
path = "qwe\\asd\\zxc.txt" # Backslashes must be escaped
path = r"qwe\asd\zxc.txt" # Backslashes are used as litterals
String formatting:
see also http://docs.python.org/3/library/string.html
"test {0} {1} {3} {2}".format(44, 55, 66, 77, 88) # Creates the string: test 44 55 77 66
"{0:10}".format("hoi") # Append spaces to a substring to make it a certain length
## Floating point:
f = 123.456789123456789
print("{0}".format(f)) # 123.45678912345679 Default
print("{0:e}".format(f)) # 1.234568e+02 Exponent notation
print("{0:.3f}".format(f)) # 123.457 3 decimals behind the dot
print("{0:16f}".format(f)) # 123.456789 Use at least 16 digits
print("{0:16.3f}".format(f)) # 123.457 Use at least 16 digits and 3 behind the dot
## Integer:
n = 123456789
print("{0}".format(n)) # 123456789 Default
print("{0:16d}".format(n)) # 123456789 Use 16 positions
print("{0:<16d}".format(n)) # 123456789 Use 16 position and left align
print("{0:x}".format(n)) # 75bcd15 Hex with lower case
print("{0:X}".format(n)) # 75BCD15 Hex with upper case
print("{0:08X}".format(n)) # 075BCD15 Hex fixed length
print("{0:010d}".format(n)) # 0123456789 Decimal, padded at the left with zeros
## Strings
"{0:8s}".format("test") % "test "
s = b'abcde\r\nfghij' # puts the \r and \n literally in the string. The string will be of type 'bytes' instead of 'str'
s2 = s1.decode('utf-8', errors = 'replace') # Convert a bytes object to a str object by applying the given encoding
b = bytes(s, 'utf-8') # Convert str to bytes
Search and replace in strings:
s2 = s1.replace('2', 'twee') # Replacing in a string
pos = s.find('hoi') # search for a substring. If found returns the index, else return -1
parts = s.split(",") # split a string into a list
line = line.strip() # strips white spaces from begin and end of a string. See also rstrip() lstrip() to strip only from left or right
line = line.strip("xyz") # strips characters x y and z from begin and end of a string. See also rstrip() lstrip() to strip only from left or right
line = line.rstrip() # Remove newlines from string
Regular expressions:
import re
pattern = re.compile(r'\s+')
sentence = re.sub(pattern, '', sentence)
s2 = re.sub(r"</?\[\d+>", "", s1)
https://docs.python.org/3/library/re.html#module-re
Importing libraries
import somedir1.somefile # Tell python we are going to use a module with filename somefile.py
# The function, classes and global variables are put in a namespace
# which has the same name as the module.
import somedir1.somefile as foo # The namespace in which the contents from the module is put is now called 'foo'
from somedir1.somefile import something # Import something from somefile.py into the current namespace. You don't need to prefix the imported items
from somedir1.somefile import something as asd # Import something from somefile.py into the current namespace and give it name 'asd'. You don't need to prefix the imported items
from somedir1.somefile import * # Import everything from somefile.py into the current namespace. You don't need to prefix the imported items
Relative import (todo: uitzoeken hoe dat werkt)
from .somefile import * # import a file which is in the same dir
from . import something # explicitly import something which is in the current (directory, module, package?)
If a module is loaded the first time its global functions are executed
sys.path.append('c:/somedir/etc/') # add to the the module search path
De directory waar het script in staat dat je aan het runnen bent komt automatisch in het path te staan.
init.py
If you put an init.py script in a sub folder, this script will be run at the moment you import the folder.
By having this file in a folder, the folder becomes a python module
Introspection and meta information:
help(something) # prints help info
a = dir(sys) # getting all functions that are in module "sys"
a = dir(s) # get all member functions of an object
a = str(something) # give a string representation of something
a = type(something) # give the type of something
a = getattr(someobject, 'something') # same as: a = someobject.something
a = id(someObject) # returns the identity of an object
def __str__(self): # This function is automatically called if a string representation of an object is needed
__name__ # This variable holds the name of the current module. It is usually equal to the filename, except for main, which has some extra underscores
dir(__builtins__) # Gives all built-in funtions
globals() # returns a list of all global objects
del something # deletes something from the current namespace
System calls and info
import subprocess
print(sys.executable) # Shows the location of the python interpreter
subprocess.call(["something.exe", "arg1", "arg2"]) # call an external executable
os.getcwd() # Get the current working directory
os.path.dirname(os.path.realpath(__file__)) # Get the directory of the current running script
os.chdir('somedir') # change the current working directory
sys.exit() # exit the programm
sys.exit(n) # exit the programm and set the exit code to n
sys.path # search path for python modules
a = sys.argv # getting command line arguments
n = len(sys.argv) # get number of command line arguments
a = os.environ
a = os.environ['TERM']
time.sleep(2) # wait for given number of seconds
datatime = time.asctime(time.localtime(time.time())) # get the data and time in human readable form
Files
import os
import glob
result = os.path.exists('somepath') # Returns True if somepath exists, false otherwize
os.mkdir('somedir') # Create a directory
result = os.listdir('somepath') # returns a directory listing
os.chdir('somedir') # change the current working directory
result = os.path.isfile('somefile') # returns true if somefile exists
result = os.path.isdir('somepath')
result = os.path.islink('somepath')
result = os.path.ismount('somepath')
time = os.path.getmtime('somefile') # Returns modification time of the file
shutil.copy("sourcefile", "destfile") # Copy a file
qwe = open('file1', 'r') # opens a text file for reading
qwe = open('file1', 'rb') # opens a binary file for reading
qwe = open('file1', 'w') # creates a new file for writing. Use 'wb' for binary files.
qwe.write('something') # writes to file
qwe.close() # manualy close a file
a = qwe.read() # lees hele file in een string
a = qwe.read(3) # lees 3 bytes
a = qwe.readline() # leest de volgende regel
a = qwe.readlines() # leest hele file in een list van strings
if not line: break # break out of loop at end of file
qwe.seek(0) # rewind the file
## Split the filename in path, name and extension:
filedir = os.path.dirname('something')
filename = os.path.basename('something')
basename = os.path.splitext(filename)[0]
extension = os.path.splitext(filename)[1]
files = glob.glob('*.cpp') # get alle filenames which match the whildcard pattern
os.path.dirname() # removes the last item from a path which is usually the filename. You can apply it multiple times to remove more parts of the path
os.remove('filename.txt') # Deletes a file
## Example of reading from file:
file = open('test1_data.txt', 'r')
while True:
line = file.readline()
if not line: break
file.close
Conditionals
if a > 5:
print('bigger')
elif a == b:
printf('equal')
else:
print('smaller')
result = 'Failed' if error else 'Passed' # Ternary operator
Logical operators
True, False # Build-in boolean 'true' and 'false' vaules
a == b # Evaluatues to True if a and b have the same value according to the __eq__ or __cmp__ operator
a != b # Evaluatues to True if a and b have different values according to the __eq__ or __cmp__ operator
a is b # Evaluatues to True if a and b are the same object according to the id() function
a is not b # Evaluatues to True if a and b are different objects according to the id() function
and
or
not
Membership operators
in
not in
Bitwise operators
& # Bitwise and
| # Bitwise or
^ # Bitwise xor
~ # Bitwise inverse
<< # Left shift
>> # Right shift
Relational operators:
if ((a and b) or not(c)) : # Logical operators
if (a < b) : # Comparison operators: == != < > <= >=
For Loop
for n in range(1, 20): # for loop n = [1...20)
for a in ['red', 'green', 'blue']: # for loop over list items
for i in iterable:
# some code with i
is basically a shorthand for:
iterator = iter(iterable)
while True:
try:
i = next(iterator)
except StopIteration:
break
# some code with i
While Loop
while a < 10: # while loop
continue # directly move on to next loop iteration (also works in for loop)
Lists
a = [] # create an empty list
a = [12, 4j, "Python", 4] # list
a = range(16) # create list 0,1,...15
a = range(4, 16) # create list 4,5,6,...,15
a = range(4, 12, 2) # create list 4,6,8,...,14
a = range(4,-1,-1) # create list 4, 3, 2, 1, 0
a = [0] * 256 # create a reference to a list with 256 zero's
b = a[2] # getting a value from a list
b = a[3:5] # get values from indeces [3...5)
b = a[:5] # get values from indeces [0...5)
b = a[3:] # get values from indeces [3...last]
b = a[-1] # get the last value from a list (-2 gives second last etc.)
n = len(a) # get the length of a list
b = a[4:9] # returns a new list with contains elements [4...9)
a.append('someting') # add a new element to the end of the list
a.remove("something") # removes "something" from the list
a = sorted(somelist, reverse = True) # sort a list, optionaly in reverse order
a = set(a) # remove duplicates
a = sorted(set(a)) # sort the list and remove duplicates
del a[2] # remove element on index 2 from the list
a.pop() # remove the element at the end of the list (i.e. the element from the latest append) and return it
a.pop(0) # remove the element at the beginning of the list and return it
Note that list variables are only references to a list. If you copy them, you only copy the reference
so both list variables then point to the same list. This also happens if you give a list as a function argument.
To truely copy a list, you have to call the list constructor. So,
b = a # create a new reference to the a list. a and b point to the same list
b = list(a) # copies a list. a and b are two seperate lists
for value in values: # Loop over the items in the list
for value in reversed(values): # Loop over the items in the list in reverse order
a = [expression(n) for n in b if filter(n)] # list comprehension, i.e. build a list from another list
lst = [n * 10 for n in lst]
colums = [col.strip() for col in colums] # strip spaces from every string in a a list by using list comprehension
data = [M * [None] for m in range(0, N)] # Create a N x M two dimensional list
data = N * ([M * [None]]) # Wrong way to create a two dimensional list. All rosw reference to the same memory
a,b,c = lst # Shorthand for a = lst[0], b = lst[1], c = lst[2]
a, = lst # Shorthand for a = lst[0]
col = [p[1] for p in points] # Extract column 1 from a two dimensional array points
Tuples:
A tuple is a list with unchangeable values, i.e. the values are fixed once the tupple is created.
a = () # Create an empty tuple
a = 1,2,3,4,5 # Create a tuple (1,2,3,4,5)
Dictionaries:
One can think of a dictionary as an unordered set of key,value pairs with the requirement that the keys are unique. It's an array which you can index with other things than just integers
a = {} # create an empty dictionary
a = {"red":12, "green":"G", "blue":something} # Create a dictionary. Note that many object types can be used as key and values. They can also be mixed.
b = a["green"] # getting a value from a dictionary, in this example "G"
a["yellow"] = 17 # Adding a new element
a["green"] = a["green"] + "something" # Change the value of a certain element
"red" in a # Returns True if the key is pressent
a.pop("yellow") # Remove an item. Its value is returned
a.keys() # Returns the keys from the dictionary
for key in a: # Iterate over an dictionary
print(a[key])
iteration
next(iter(someObject)) # get first item from an iteratable object
Working wiht numbers:
a = 2 ** 8 # raising to some power
a = 34 # integer
a = -34 # negative integer
a = 34636478563849653894 # long integer
a = 0x22 # hexadecimal notation (34 decimal)
a = 042 # octal notation (34 decimal)
a = int(12.34) # convert float to int by truncateting
a = int("34") # convert string to int
a = True # boolean true value
a = False # boolean false value
a = (7 < 6) # boolean result (False)
a = 12.34 # float
a = 1234e-2 # scientific notation
a = float(12) # convert int to float
a = float("12.34e-12") # convert string to float
a = 12+34j # complex number
a = complex(12) # convert to complex (12+0j)
a = "asdfg" # string
a = 'asdfg' # string
a = "%02x" % ord("n") # convert to string in hex notation (6e)
a = "%02X" % ord("n") # convert to string in hex notation (6E)
s2 = s[3:6] # get sub-string s[3,4,5]
s[len(s)::-1] # reverse a string
a = round(12.45) # rounds to nearest integer but no conversion to int
a = chr(0x6E) # give the character (n) of an ascii (byte) value
a = hex(110) # convert to string to hex notation (0x6e)
a = len(something) # give the length of something
a = ord("p") # give the ascii (byte) value of a character
a = math.floor(12.45) # gives 12
a = math.ceil(12.45) # gives 13
n = int('0xCD', 16) # Convert hex string to an integer
a = (12, 4j, "Python", 4) # tupple
b = a[2] # getting a value from a tupple
Using functions:
def myFunction(arg1) : # Define a function
print ("Hello from myFunction\n") # Function body
def someOhterFunction :
myFunction(4) # Call the function
def myfunc(a, b, c = 7): # supply a default argument value
def myfunc(a, b, *c): # remaining arguments are passed as list c
def myfunc(a, b, **c): # remaining arguments are passed as name=value pairs in dictionary c
if __name__ == "__main__": # put this at the end of a script which has a main() function.
main()
Lamda functions:
lambda a, b : a + b # defines an anonymous function which takes two parameters, a and b, and returns their sum
a.sort(key=lambda x: x[1]) # Sorts a list of lists by looking at index [1] of the inner list
args and *kwargs
def somFunction(a, b, **kwargs):
kwargs is an dictionary with keword arguments. You can call the as follows:
someFunction(2, 3, color = "red")
In this case the function should check if the keyword "color" is in the dictionary, and
if so, use the value it contains.
Network
a = os.popen('telnet 127.0.0.1')
a = os.popen('netcat 127.0.0.1 1200')
datatypes:
bytes
str
list
dict
tuple
bool
int
float
complex
function
builtin_function_or_method
classobj
instancemethod
file
module
NoneType
Classes:
class SomeClass:
def __init__(self, value): # Optional initialisation function
self.a = value
def __del__(self): # Optional destructor
print('Destructor called')
def function1(self): # member function
print(self.a)
object1 = SomeClass() # make an new object. Don't forget the parentesis. They are (unlike in C++) always required.
object1.function1() # call the function1 on the object
object1 = None # create an empty object. Can for example be returned from a function if the actual object can not be constructed.
SomeClass.fun = somefunction # You can runtime add functions to a class
enums:
from enum import Enum
class Colors(Enum):
COL_Red = 0
COL_Green = 1
COL_Blue = 2
Exception handling:
try:
someFunction()
except FileNotFoundError:
print('Exception: File not found')
except Exception as e:
print('Exception: {0}'.format(e))
raise # Optionally, re-raise the exception so it can be handled elsewhere
else:
print('someFunction ran without any exceptions')
finally:
print('Doing some clean-up') # This code is always ran even if the exception handlers throw new exceptions
print('Next statement') # This code will not be reached if the exception handlers themselves throw new exceptions
raise SomeException() # Raise an exception
class SomeException(Exception): # User defined exception:
def __str__(self):
return 'Something wend wrong'
Calling C functions:
from ctypes import *
mydll = cdll.LoadLibrary('D:/PythonTest/cproject/Debug/cproject.dll')
result = mydll.add(2, 3)
mydll.addf.restype = c_float # Specify the return type. If you don't do this, the default return type 'int' is used
c = mydll.addf(c_float(5), c_float(2)) # Specify the type of the arguments
property() is a build-in function for creating getters() and setters()
Als je @property als functie decorator gebruikt, dan wordt deze functie als een getter() of setter() beschouwd, en kun je de functie naam buiten de class gebruiken alsof het een gewone member variable is.
Misc:
If you copy a list you do not actually get a copy but a reference:
a = [1,2,3,4]
b = a
b[1] = 20
print(a)
[1, 20, 3, 4]
You can explicitly make a copy of an object with the copy() member function. Todo: find out how exactly this works. It seesm that python sometimes make just a reference when you only use the asignment = without calling copy()
Scoping of objects and variables works differently than in C++. Loops and if statements don't create a new scope.
You can run your script in debug mode by adding these lines at the top of the script
import pdb
pdb.set_trace()
You can run your script in interactive mode by first starting an interactieve python sesion by entering 'python' on the command prompt.
Next, on the python command prompt, enter
import somescript
in which somescript.py is the script you want to run
Running an external command
import subprocess
result = subprocess.run(["svn", "info", "--show-item", "url", name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
text = result.stdout.decode('latin-1')