Python For Me
#file io #file management functions classes / modules subprocess / os commands
json
useful standard libs retreive url parse html conda and envs eggs / wheels django databases fabric
exit()
Print and string concat:
x = 5
print("test " str(x))
print("test", x)
Split string:
a = x.splitlines() # split string by lines
Sort a list:
sorted(a)
Lambda
- anonymous function
- assigned to a variable that can be passed around
- single expression
- takes args
# lambda args : expression
x = lambda a : a 5
print( x(3) )
x = lambda a : "Value is: " str(a * 2)
print(x(5))
Lambda to check if element in dict and return default value:
t = { "a": "test1", "b": "test2" }
b = lambda a,i : a[i] if i in a else "default"
b(t,"c")
Check length:
len(a) # list length
type(a) # check type
List comprehension:
newlist = [x for x in fruits if "a" in x]
Enumerate a list with index and value:
a = [ 'a', 'b', 'c' ]
for i in a:
print(i)
for i,j in enumerate(a):
print(i " " j)
Initialize dict or list:
a = []
d = {}
Populate list with regex split:
a = re.split(",", "a,b,c,d,e,f,g") # populate
List slicing:
a[-1] # last element
a[2:7] # 2-7 exclude last
a[2:7:2 ] # 2-3, step 2, exclude last
a[7:2:-1 ] # count backwards
a[:5] # begin to 5
a[5:] # 5 to end
a[:] # entire list
a[::-1] # reverse list
Sorting:
a.sort() # sort in place
a.sort(reverse=True)
a.sort(key = str.lower)
a.reverse()
Sort with custom function:
def test1(x):
return len(x)
a.sort(key=test1)
Check if exists in list:
if 5 in a:
List operations:
a[3] = "x" # assign value
a.insert(2, "abc") # insert at index
a[1:3] = ["a", "b"] # replace slice
a.append("x") # append element
a.extend(a2) # append list or any iterable
a = a a2 # concatenate lists
a.append(a2) # append entire list as a sublist
a.remove("x") # remove first match
a.pop() # remove/return last
a.pop(5) # remove/return at index
del a[3] # delete
del a # delete entire list
a.clear() # empty the list
a.index("x") # get index of first match
a.count("x") # number of matches
a2 = a.copy() # copy list
Enumerate Dict / Hash:
d = {'a': 123, 'b': 456, 'c':789, 'd': 000}
for i, k in enumerate(d): # index and key
print(i, k)
for i, (k, v) in enumerate(d.items()):
print(i, k, v)
with open("data1.txt") as f1:
data1 = f1.readlines()
with open("data1.txt") as f1:
data1 = f1.read()
data1 = [ x.strip() for x in data1 ] # strip newline and padding
Globbing:
import glob
a = glob.glob('*.txt')
for i in a:
print(i)
File Management
import os # OS module
import shutil # Some utils
shutil.copy(src, dst) # Copy file
os.rename(src, dst) # rename file
os.unlink("/tmp/foo.txt") # delete file
os.listdir("/dir") # List files
os.mkdir(path[, mode]) # Create directory
shutil.copy(src, dst) # copy content and permission
os.rmdir(path) # remove dir, only empty dirs
os.removedirs(path) # remove empty dir, recursive
File IO
‘r’ - read - default ‘w’ - write ‘rb’ - read binary ‘wb’ - write binary ‘a’ - append ‘a ‘ - append but also read and seek backwards but always write to end
f = open('workfile', 'w') # r or w
f.write('This is a test\n') # write
f.read() # entire file
f.readline() # line at a time
f.readlines() # entire file, as list
f.close() # close it
Get home dir:
import os
print(os.path.expanduser('~'))
from pathlib import Path
print(Path.home())
=========================================
Regular Expressions Characters
\d decimal digit [0-9] \s whitespace [ \t\n\r\f\v] \w alphanumeric [a-zA-Z0-9_] \D non decimal \S non whitespace \W non-alphanumeric probably more ….. ^ start $ end . any character except newline
- 0 or more 1 or more ? 0 or 1 {0,1}
Flags
re.S Make . match any char, including newlines re.I Do case-insensitive matches re.M Multi-line matching, affecting ^ and $
import re
m = re.match(r'cups', data1) # first match, check BEGINNING, ret: match obj or null
m = re.search(r'cups', data1) # first match, check all lines, ret: match obj or null
m = re.search(r'((sys)t)(emd)', data1) # using separate and nested groups
m.group() # match found - first match
m.groups() # groups if using grouping - from first match
m = re.findall(r'systemd', data1) # find all matches, return list of matched strings
m = re.findall(r'((sys)temd)', data1) # find all matches, return groups as list of tuples, NOT matched strings
# need more than one group
print(m)
a = re.split(r'\s', data1) # split into list ( ex: by space )
a = re.split(r'\W ', data1) # split out list of words
x = re.sub( r'xxxx', 'abc', 'xxxx xxxx')
x = re.sub( r'(blue|white|red)', 'colour', 'blue socks and red shoes')
x = re.sub( r'xxxx', '5', 'process_data.sh input_xxxx.dat output_xxxx.dat')
# check if match found:
if m:
print 'Match found: ', m.group()
# compile first for efficiency ( if you plan to loop / iterate a lot ):
p = re.compile(r”match this”)
m = p.search('string goes here')
a = p.split('test test')
x = p.sub( 'colour', 'blue socks and red shoes')
p = re.compile('ab*', re.I | re.M) # case insensitive, multi-line
Function:
def function1(one, two='Yes', *name2, **name3):
return value
Subprocess / OS Commands:
https://www.datacamp.com/tutorial/python-subprocess <==== review more
import subprocess
subprocess.check_output( "uname -a", stderr=subprocess.STDOUT, shell=True )
subprocess.run(["ls", "-l"])
cmd1 = "ls -l /tmp | awk '{print $2,$4}'"
x = subprocess.run(cmd1, shell=True)
x = subprocess.run(cmd1, shell=True, capture_output=True, text=True)
check # raise error on non-zero timeout # specify timeout
x.returncode
x.args
x.stdout
x.stderr
JSON
import json
x = '{ "name":"John", "age":30, "city":"New York"}'
y = json.loads(x) # string to object
print(y["age"])
x = {
"name": "John",
"age": 30,
"city": "New York"
}
y = json.dumps(x) # object to string
print(y)
Error Handling - Try / Except
if x < 0:
raise Exception("Sorry, no numbers below zero")
if not type(x) is int:
raise TypeError("Only integers are allowed")
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
nesting:
try:
f = open("demofile.txt")
try:
f.write("Lorum Ipsum")
except:
print("Something went wrong when writing to the file")
finally:
f.close()
except:
print("Something went wrong when opening the file")
input
# python 3.6
username = input("Enter username:")
print("Username is: " username)
# python 2.7
username = raw_input("Enter username:")
print("Username is: " username)
Args:
import sys
print( sys.argv[0] ) # current script
if len(sys.argv) >= 1:
print( sys.argv[1] ) # first arg
CSV:
https://realpython.com/python-csv/
More
Check if object has an attribute:
if hasattr(instance, 'tags'):
....
Show type:
print(type(instance.tags))
Show members of a class:
print(instance.__dict__