Python

Things I Have Written

How to set up anaconda - gists.github.com/mpilosov/

String Formatting (making titles out of lowercase strings)

Logging

filemode='w' will wipe any existing logs and start fresh.

import sys
import logging
import logging.handlers

# Take care of some logging
logger_filename = 'twitterbot.log'

# Remove all handlers associated with the root logger object.
# this part may be unecessary??
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

loglevel = logging.INFO
format_str = "%(asctime)s [%(name)s - %(threadName)s] [%(levelname)s - %(funcName)s()]  %(message)s"
date_format = '%m/%d/%Y %I:%M:%S %p'

root_logger = logging.getLogger()
root_logger.setLevel(loglevel)
handler = logging.handlers.RotatingFileHandler(
    filename=logger_filename,
    maxBytes=1048576,
    backupCount=5,
    encoding='utf8',)
formatter = logging.Formatter(format_str)
handler.setFormatter(formatter)
root_logger.addHandler(handler)
root_logger.addHandler(logging.StreamHandler(sys.stderr))

This will enable UTF-8 encoding, so it should run across all types of devices. Here is a good stack overflow answer that explains exactly what is going on with how Python handles ASCII and Unicode.

Argument Parsing

From my twitterbot:

if __name__ == '__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Query twitter for a list of mentions. Load list of tweets already replied to. If mentions are new, reply to them.')
    
    parser.add_argument('-t', '--test', action='store_true', 
        help="Runs bot in testing mode (doesn't actually tweet).")
    parser.add_argument('-n', '--num', default = -1, type=int,
        help="Run only for `n` most recent mentions")
    args = parser.parse_args()

Access arguments with args.longname, where longname is num or test in the example above. The single-character version is just for command-line. Run --help to print the contents of description.

Alternatively, include this file in your project (repo linked below) from dmulholl, which enables a suite of convenience functions for argument parsing.

Virtual Environments

The instructions for setting up a “new python” with isolated project dependencies (and link to Jupyter) are as follows (from within a project’s root directory):

  1. pip install virtualenv (if you do not already have it)
  2. mkdir venv (make a folder called venv for the dependencies to live inside of)
  3. virtualenv venv (initialize virtual environment in the venv folder)
  4. source venv/bin/activate (activate the virtual environment. Validate that which python and which pip now point to the versions in this folder)
  5. pip install -r requirements.txt (install project dependencies)
  6. python -m ipykernel install --user --name mypython --display-name "EffYeahPython" (create ipython kernel named pystaff based on current activated environment, so that Jupyter can access it) (change mypython and EffYeahPython to your liking, you can name the folder venv whatever you want, and it can live anywhere you want it to, but inside of a project’s directory … makes a lot of sense)
pip install virtualenv
mkdir -p venv
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
python -m ipykernel install --user --name py3 --display-name "Py3"

Imports

In order to import files from sub-directories of your project, put a file (yes, it can be completely empty, apparently) called __init__.py in the folder, communicating to the Python interpreter that this directory should be importable. For example:

- foo/
|- __init__.py
|- newfun.py
|- mysubmodule/
  |- __init__.py
  |- otherfun.py
- main.py

Now main.py can import foo.newfun and import foo.mysubmodule.otherfun

Plotting

Quick Marginal Distribution Visualization with Seaborn

Piecewise-defined functions and 3D Matplotlib Plots

Drawing Lines atop scatterplots

Basics: Plotting a circle

Three-Dimensional Projections (with Linear Algebra)

Time

import datetime
str(datetime.timedelta(seconds=500000))

# you cannot add timedelta to time, but you can add it to datetime
my_time = (datetime(1970,1,1) + timedelta(seconds=my_seconds)).time()

Note that timedelta is not overflow safe, which leads to numerical error.

Example of timedelta arithmetic gone wrong:

str(timedelta(hours=8) - timedelta(hours=10)) # result: -1 days

f-strings

Python 2.7+:

greeting = "Welcome, {name}!".format(name=user.first_name)

Python 3.6+:

greeting = f"Welcome, {user.first_name}!"

(example courtesy of Miles Erickson)


Things Others Have Written

Advanced Jupyter Notebook Tricks (and tips, though some may be outdated)

Jupyter Shortcuts

Learning Pandas (like R… for Python)

Scraping Webpages with Pandas

Using *args and **kwargs in Python

Package for simplified command-line flag/options/argument parsing

Jupyter Notebook Math Examples - Markdown Syntax

Magic in Jupyter - ctrl+F almost anything you can think of

Plotting Options in Python (2D Density Plots and more)

Some SymPy examples

twitter API

Avatar
Michael Pilosov
An (applied) mathematician on a mission. Denver, CO.
Next
Previous