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
 
try:
    password = getpass.getpass()
except Exception as error:
    print('ERROR', error)

Resource: https://www.geeksforgeeks.org/getpass-and-getuser-in-python-password-without-echo/

Python 3

import getpass

try:
    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")
>>> m.group(0)
'Isaac Newton'
>>> m.group(1)
'Isaac'
>>> m.group(2)
'Newton'
>>> m.group(1,2)
('Isaac', 'Newton')

Test your regexs: https://regex101.com/

Resource: https://docs.python.org/2/library/re.html

List operations

Count elements in a list

len(list)

Resource: https://stackoverflow.com/questions/4130027/python-count-elements-in-list

String to list

string.split()

Resource: https://stackoverflow.com/questions/8266529/python-convert-string-to-list

Get first two characters of a string

<string var>[2:]

Resource: https://stackoverflow.com/questions/1270990/how-to-remove-two-chars-from-the-beginning-of-a-line

File operations

Read file line-by-line and print each line

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

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.close()

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

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

os.remove('path/to/file')

Check if file exists

def file_exists(file):
    if os.path.isfile(file):
        return True
    else:
        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):
    port_scan(site,int(x))

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

print(list[0])

String interpolation with Python 3.6.x

import os
site = 'https://google.com'
os.system(f"open -a /Applications/Google\ Chrome.app {site}")

String interpolation with older Python

import os
site = 'https://google.com'
os.system("open -a /Applications/Google\ Chrome.app %s" % site)

Multiline list

list = [
    'item1',
    'item2',
]

Create requirements.txt with pip

pip freeze > requirements.txt

Debugger one-liner

import pdb; pdb.set_trace()

Show global vars in pdb

globals()

Show available methods for an object

dir(object)

Show object type

type(object)

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]
else:
    dict[key].append(value)

Print a dictionary to a table

Just use https://pypi.python.org/pypi/PrettyTable and save yourself a headache.

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

        Usage:
        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.

Resources:
https://stackoverflow.com/questions/606191/convert-bytes-to-a-string
https://realpython.com/python-json/

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

Resource: https://stackoverflow.com/questions/4188467/how-to-check-if-an-argument-from-commandline-has-been-set

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.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect( hostname = "<ec2 hostname>", username = "<username>", pkey = k )

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

Resource: http://diego-pacheco.blogspot.com/2016/05/having-fun-with-boto-3.html

Auto enable virtualenv for a directory

Use https://github.com/kennethreitz/autoenv, 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/activate.sh

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.

Pipenv

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

Resources:
https://github.com/pypa/pipenv
https://github.com/pypa/pipenv/issues/84
https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv

List licenses of dependencies

pip install pip-licenses