Things I Have Written

How to set up anaconda -

String Formatting (making titles out of lowercase strings)


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[:]:

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()
handler = logging.handlers.RotatingFileHandler(
formatter = logging.Formatter(format_str)

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"


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

- foo/
|- mysubmodule/

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


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)


import datetime

# 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


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

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