Low Orbit Flux Logo 2 F

Python For Me



pass       # place holder, does nothing
exit()

Print and string concat:


x = 5
print("test " + str(x))
print("test", x)

Split string:



a = x.splitlines()   # split string by lines

Empty list/dict:



if not a:            # empty list   <== use this
if len(a) == 0:      # empty list

if len(s) == 0:      # empty string
if s == "":          # empty string
if s.strip() == "":  # only spaces
if s is None:        # undefined
if not s:            # undefined or empty   <== use this

Check length:



len("test"  # string length
len(a)      # list length
type(a)     # check type

Sleep:



import time
time.sleep(5)

Control Flow



if x < 5:
    print('less than 5')
    x = x + 2
elif x == 50:
    print('Medium')
elif x == 100:
    print('Large')
else:
    print('Default')

More:



if x != "taco":           # negative string compare
    pass

if x < 10 and x > 5:      # logical and
    pass

if x > 10 and x < 5:      # logical or
    pass

if "taco" not in food:    # not in list/collection
    pass

if x is y:                # same object?
    pass

print("yes") if "taco" in food else print("no")    # ternary operators
("no","yes")["taco" in food]
(print("no"),print("yes"))["taco" in food]

Match:



match option:                       # like switch statement, stops after first match
    case 123:
        print("First Option")
    case 456:
        print("Second Option")

Loops

Range:



range(5, 15)       # 5 6 7 8 9 10 11 12 13 14
range(5, 25, 2)    # 5 7 9 11 13 15 17 19 21 23
sum(range(4))

For loop:



for i in a:
    print(i)

for i in range(5):
    print(i)

for i in range(len(a)):
    print(i, a[i])

for i in range(0, 9, 2):           # Python C style for loop
    print(i)


for i in a:
    if i == "pizza":
        continue              # skip to next iteration
    if i == "taco":
        break                 # break out of loop
else:
    print("no tacos found")   # if no break during loop

While loop:



x = 0
while x < 50:
    print("x: " + str(x))
    x = x + 1

while True:
    print("x")

Tuples



t = ()           # empty tuple
t = 'pizza',     # tuple with one value
t = ('pizza',)   # tuple with one value
t = ('pizza')    # NOT a tuple, needs a comma

t = 'burger', 'taco', 'pizza'
t = ('buger', 'taco', 'pizza')
x = t, ('mustard', 'hot sauce')          # nested tuple

t[0]         # index a tuple
a, b, c = t  # unpack

List Stuff

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):   # index and value
    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:



sorted(a)        # sort a list, return a new list, works on all iterables
sorted("test")   # sort a string ( NOT with sort funstion though )

a.sort()                      # sort in place
a.sort(reverse=True)
a.sort(key = str.lower)
a.reverse()                   # reverse list in place
reversed(a)                   # works on any iterator, returns an iterator
list(reversed(a))             # convert to list

b = set(a)       # create set from list ( unique items only )

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(1, "taco")  # insert at index ( between 0 and 1
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()      # delete all elements ( clear list )
del a[:]       # delete all elements ( clear list )


a.index("x")   # get index of first match
a.count("x")   # number of matches

a2 = a.copy()  # copy list, return shallow copy in place, same as: a[:]

Zip:



meal = ["breakfast", "lunch", "dinner"]
food = ["pizza", "noodle", "burger"]
for a, b in zip(meal, food):
    print(a + ": " + b )

List Comprehensions



a = [0, 1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5, 4]

[x*2 for x in a]            # 1 for loop ( basic )
[x+3 for x in [1,2,3]]      # 1 for loop ( basic )
[x for x in a if x > 1]     # for and if
[abs(x) for x in a]         # abs example
[x + y for x in a for y in b]  # 2 for loops    - left loop, then right

[(x,x*5) for x in a]    # return list of tuples
[[x,x*5] for x in a]    # return list of lists

[(x, y) for x in a for y in b if x != y]        # 2 for loop, if, return tuple - left loop, then right

b = [[j * i for j in range(len(a))]for i in a]  # nested - right loop then left

Queues with deque



from collections import deque
my_q = deque(["pizza", "burger", "sushi"])
my_q.append("taco")
my_q.append("noodle")
my_q.popleft()
my_q.popleft()

Dict / Hash Stuff

Enumerate Dict / Hash:



d = {'a': 123, 'b': 456, 'c':789, 'd': 000}

list(d)    # list of keys in insertion order
sorted(d)   # keys in sorted order

for i, k in enumerate(d):               # index / key
    print(i, k)

for k, v in d.items():                  # key / value
    print(i, k, v)

for i, (k, v) in enumerate(d.items()):  # index / key / value
    print(i, k, v)

{x: x*5+3 for x in (22, 45, 67, 1)}        # Dict comprehension:

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

Get home dir:



import os
print(os.path.expanduser('~'))

from pathlib import Path
print(Path.home())

Globbing:



import glob
a = glob.glob('*.txt')
for i in a:
    print(i)

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



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

Regular Expressions

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:

Arbitrary argument lists:

**name will hold a dictionary with all keyword args that aren’t formal args
*name tuple containing all positional args that aren’t formal args


def function1(one, two='Yes', *name2, **name3):           
    value = str(one) + str(two)
    return value

function1("a", "b")
function1("a", "b", 1, 2, 3, x="4", y="3")
function1("a", "b", 1, 2, 3, 4, 3)
z1 = ["a", "b"]
z2 = {'one':"a", 'two':"b"}
function1(*z1)           # pass list to be unpacked
function1(**z2)          # pass dict to be unpacked

Function Annotations - types for each argument and type for return value:



def test3(a: str, b: str = 'xyz') -> str:
    print("Annotations: ", test3.__annotations__)

Lambda


# 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")

Function to generate a lambda:



def test1(n):
    return lambda x: x + n
x = test1(42)
x(0)

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

CSV:

https://realpython.com/python-csv/

XLS

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

Classes and Objects



if hasattr(instance, 'tags'):   # Check if object has an attribute
    ....

print(type(instance.tags))      # Show type

print(instance.__dict__         # Show members of a class

Polymorphism

Inheritance

Modules

Packages

Iterators

Dates