Python Notes

This will encompass things that I find useful and end up looking up later when I haven't coded in a while.

Python3 Simple HTTP Server

python3 -m http.server <port>

Get password as input

Python 2

import getpass
    password = getpass.getpass()
except Exception as error:
    print('ERROR', error)


Python 3

import getpass

    password = getpass.getpass()
except Exception as error:
    print('ERROR', error)

Run a command from the command line

python -c 'print "A" * 32'

Regex and group matching

>>> import re
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
'Isaac Newton'
('Isaac', 'Newton')

Test your regexs:


List operations

Reverse list

>>> l = [1,2,3,4]
>>> rl = l[::-1]
>>> rl
[4, 3, 2, 1]


Count elements in a list



String to list



Get first two characters of a string

<string var>[2:]


File operations

Read file line-by-line and print each line

def print_lines(file):
    with open(file) as f:
        for line in f:

Read file into list

def return_list(file):
    with open(file) as f:
        list = f.readlines()
        return list

Remove line from a file containing a specified string

def remove_line_containing_string(file):
    f = open(file, 'r')
    contents = f.readlines()

    f = open (file, 'w')
    for line in contents:
        if not 'some string' in line:

List only files in a directory

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

Remove file


Check if file exists

def file_exists(file):
    if os.path.isfile(file):
        return True
        return False

Run command and get output

def run_cmd(cmd):
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    (output, err) = p.communicate()
    p_status = p.wait()
    return output

List comprehension

This for loop:

for x in range(int(sp),int(ep)+1):

can also be represented as this list comprehension:
[port_scan(site, int(x)) for x in range(int(sp), int(ep)+1)]

Print a line from a list


String interpolation with Python 3.6.x

import os
site = ''
os.system(f"open -a /Applications/Google\ {site}")

String interpolation with older Python

import os
site = ''
os.system("open -a /Applications/Google\ %s" % site)

Multiline list

list = [

Create requirements.txt with pip

pip freeze > requirements.txt

Debugger one-liner

import pdb; pdb.set_trace()

Show global vars in pdb


Show available methods for an object


Show object type


Remove \n and extra quotes from an object

obj.rstrip().replace('"', '')

Check if current user is root, exit if not

def check_root():
    if not os.geteuid() == 0:
        sys.exit('This script must be run as root!')

Dictionary Notes

Add value to list in dictionary if it doesn't exist; otherwise append the value to the existing list.

if not dict.has_key(key):
    dict[key] = [value]

Create empty dict

dict = {}

Assign incrementor to key

In this instance, we'll say that c is a random character, and we're currently counting the number of instances of this character in a string.
dict[c] = 1

Increment incrementor

dict[c] += 1

Print a dictionary to a table

Just use and save yourself a headache.

from prettytable import PrettyTable
def output_dict_to_table(in_dict):
        Print an input dictionary as a table

        output_dict_to_table({'Key': ['Value'], 'Key2': ['Value2', 'Value3']})
        t = PrettyTable(['Key', 'Value'])
        for k, v in in_dict.items():
                t.add_row([k, v])
        print t

JSON from output of running a program to dict

This example uses the run_cmd function above.

# Output is bytes; convert to string with the .decode('utf-8')
output = run_cmd('terraform output -json').decode('utf-8')
tf_json = json.loads(output)

The tf_json object will be properly set up as a python dictionary.


Take command line args

Just use argparse; it's clean and can be implemented with minimal fuss.

import argparse

def parse_arg():
    parser = argparse.ArgumentParser(description='Do stuff')
    parser.add_argument("-a", '--argument', required=True, help='description of what your awesome code does')
    return parser.parse_args()
    # Return value of a single argument if you don't have multiple ones:
    #return parser.parse_args().arg


Run remote command using ssh on an aws instance

import paramiko

k = paramiko.RSAKey.from_private_key_file("/path/to/pem/file")
c = paramiko.SSHClient()
c.connect( hostname = "<ec2 hostname>", username = "<username>", pkey = k )

stdin , stdout, stderr = c.exec_command("<command to run>")
print("stdout: " +'utf-8'))
print("stderr" +'utf-8'))


Auto enable virtualenv for a directory

Use, which can be installed via brew, pip, etc.

Once it's installed, add this line to your bash_rc or zshrc or whatever (the path will vary based on which installation method you used):
source /usr/local/opt/autoenv/

Once this is done, you simply drop a .env file into the directory which has a virtualenv, that should look like this:
source .venv/bin/activate

Now whenever you cd into this directory, virtualenv will automatically be activated. You will have to deactivate manually when you leave though.

Commenting Standards

A nice example can be found here for google style.


Set up pipenv for a project with python version 3.6.5

pipenv --python 3.6.5

Install a dependency

pipenv install <package>

Lock the dependencies

You want to do this before a release essentially.
pipenv lock

Run a command from the virtualenv for a project

pipenv run python -V
pipenv run python -c 'print("A" * 32)'

Activate the virtualenv

pipenv shell

Deactivate the virtualenv

CTRL-d or type exit


List licenses of dependencies

pip install pip-licenses

Get first occurrence of a char in a string

The index() function gets the index of the first occurrence of a char starting from the beginning of a string and returns it.

The rindex() function gets the index of the first occurrence of a char starting from the end of a string and returns it.

>>> s = "dedjdedl"
>>> s.index('d')
>>> s.rindex('d')

Combine two lists with zip

>>> l1 = ['a','b','c']
>>> l2 = [1,2,3]
>>> print(list(zip(l1,l2)))
[('a', 1), ('b', 2), ('c', 3)]

Print the columns of a matrix

>>> a = [[1,2],[3,4],[5,6]]
>>> print(list(zip(*a)))
[(1, 3, 5), (2, 4, 6)]

Data types and examples

Complexity chart


Store unordered collections of unique elements

>>> items = [1,5,2,2,3,5,1]
>>> set(items)
{1, 2, 3, 5}

Iterate through list with idx and value

for idx, val in enumerate(list_of_stuff):

Iterate through range with an incrementor of 100

for i in range(start, end, 100):