Pythonian code

  • Conditional or Ternary operator

    result = x if a > b else y
  • list comprehension

    result = [function(items) for items in list]
    # can be used to make empty list of lists
    result = [[] for i in range(n)]
  • dict comprehension

    result = {k:v for k,v in zip(keys, values)}
  • Multiple for loops

    y = [x for xs in xss for x in xs]
  • Single line for, if statement

    [i for i, c in enumerate(a) if c in b]
  • Single line for, if else statement

    result = [x if a > b else y for a in list]
  • 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:

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

IPython (Jupyter)

  • Clear variables

    %reset -f

Footnotes

  1. https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists and https://stackoverflow.com/questions/4979542/python-use-list-as-function-parameters

  2. https://stackoverflow.com/questions/3169825/generating-combinations-in-python and https://docs.python.org/3/library/itertools.html#itertools.product

  3. https://stackoverflow.com/a/13733863

  4. https://stackoverflow.com/questions/25348532/can-python-pickle-lambda-functions