Pythonian code
General
-
Conditional or Ternary operator
result = x if a > b else y
-
lambda function
y = lambda x: x+2
-
Single line functions in python are lambda functions
perms = lambda xs: (list(x) for x in itertools.permutations(xs))
-
Ignore output by using an underscore
_ = foo()
-
splat operator: allows lists and dictionaries to be unpacked1
# this sends the list as inputs to the function def somefun(p1, p2, p3) a = ['a', 3, 4.5] some_func(*a) # this sends the dictionary as parameter, value pairs b = {'p1': 'a', 'p2': 3, 'p3': 4.5} some_fun(**b)
-
walrus operator:
y = z if (z:=np.mean(x)) > y else y
List
-
list comprehension
result = [function(items) for items in list] # can be used to make empty list of lists result = [[] for i in range(n)]
-
multiple level list comprehension
y = [x for xs in xss for x in xs]
-
list comprehension + if statement
[i for i, c in enumerate(a) if c in b]
-
list comprehension + if/else statement
result = [x if a > b else y for a in list]
-
finding and removing an element from list
indexes = [i for i, e in enumerate(list) if e == element] for r in reversed(range(len(indexes))): lines[indexes[r]] = []
-
filtering elements in a list based on a condition
# list comprehension with if statement works a = [x for x in a if x != 2] #alternative a = list(filter(lambda x: x!= 2, a)) # necessary as the following line will only remove the first item in the list a.remove(2)
-
swapping elements in a list
A[j],A[k] = A[k],A[j]
Dict
-
dict comprehension
result = {k:v for k,v in zip(keys, values)}
-
iterating through dict keys or/and values
for key in mydict.keys(): print(key) for val in mydict.values(): print(val) for key, val in mydict.items(): print(key, val)
-
getting values from dictionary
val = my_dict.get(key, default) # returns default if key does not exist
Tuple
-
Combining two list into a list of tuples
a = list(range(0,10)) b = list(range(10,20)) c = list(zip(a, b))
-
Converting tuples into lists
d = list(zip(*c)) #d[0] == a #d[1] == b
Common modules
Argparse
Easy to setup command line input.
import argparse
parser = argparse.ArgumentParser(description='Program description')
parser.add_argument("folder", nargs='?', default=os.getcwd(), help="input folder") # optional positional argument
parser.add_argument("-v", "--verbose", dest = "verbose", default=False, action='store_true', help="Output info logs") # flag
parser.add_argument('--optional-argument', dest="opt_arg", default=[1.0,2.0,15.0], type=lambda y: [float(x) for x in y.split(',')], help="Input desired floats separated by ','; e.g. '1,1,1' or '12.35, 3.74, 1' ")
# optional argument that is processed by a function
args = parser.parse_args()
Itertools
Generating combinations in python.
Use case: Get all possible combinations of each of the lists in this list of lists2 - [[1,2], [2,3], [4,5,6], [1]]
from itertools import product
source = [[1,2], [2,3], [4,5,6], [1]]
combinations = list(product(*source))
Logging
import logging
logging.basicConfig(format='%(asctime)s %(message)s',
filename='logs.log',
filemode='w', ## if logs need to be rewritten
level=logging.DEBUG if debug else logging.INFO)
logging.info("Info message")
logging.debug("Debug message")
I like to copy the logs to stderr3 when a verbose flag is passed, so that I can directly read the command line output and don’t have to look at the log.
if verbose:
logging.getLogger().addHandler(logging.StreamHandler())
os and glob
-
Finding specific files in a folder:
import os files = [f for f in os.listdir(folder) if f.endswith('.ext')]
import glob files = glob(os.path.join(folder, "*.ext"))
Pickle
import pickle
#or
import dill as pickle
with open(pickle_file, 'wb') as f:
# Pickle the 'data' dictionary using the highest protocol available.
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
Note: If you have to pickle lambda functions or functions returning another function, use dill instead of pickle (drop in replacement).4
Regular expressions
IPython (Jupyter)
-
Autoreload libraries5
%reload_ext autoreload %autoreload 2
-
timeit - very useful to calculate how fast your function/code is
%timeit myfunction()
-
Clear variables
%reset -f
-
Progress bar
from ipywidgets import IntProgress fp = IntProgress(min=min, max=max, description='Progress') display(fp) # required to show the progress bar # update value using fp.value = new_value # Use FloatProgress if the value is float
Footnotes
-
https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists and https://stackoverflow.com/questions/4979542/python-use-list-as-function-parameters ↩
-
https://stackoverflow.com/questions/3169825/generating-combinations-in-python and https://docs.python.org/3/library/itertools.html#itertools.product ↩
-
https://stackoverflow.com/questions/25348532/can-python-pickle-lambda-functions ↩
-
https://stackoverflow.com/questions/56059651/how-to-make-vscode-auto-reload-external-py-modules#57245926 ↩