Zach's Mugspideyclick logo

GitHub

GitLab

Linkedin

Instagram

Youtube

SoundCloud

Email

Python Tips

Cheat Sheet

http://overapi.com/python

Handy Python Functions

https://gitlab.com/snippets/1906782

Pair with configFind.sh to keep your private data out of your projects:

https://gitlab.com/snippets/1906783

Awesome quick HTTP Server

cd /directory/with/images
python -m http.server

This allows you to access local files in chrome dev tools and such by using localhost:8000/xyz.png for example.

Profiling with Cprofile + SnakeViz

First, create the profile file: python -m cProfile [-o output_file] [-s sort_order] myscript.py

On a your client machine, install SnakeViz: sudo python -m pip install snakeviz

Then download the output file from the server and visualize: snakeviz foo.pstats

This should open http://127.0.0.1:8080 to the SnakeVis page.

Catch all exceptions

try:
    #stuff
except Exception as e:
    print(e)

Quick Nested Object Traversal

From https://stackoverflow.com/questions/14692690/access-nested-dictionary-items-via-a-list-of-keys

from functools import reduce  # forward compatibility for Python 3
import operator

def getFromDict(dataDict, mapList):
    return reduce(operator.getitem, mapList, dataDict)

myDictionary = {'a':1, 'b':2, 'c':{'a':1, 'b':2}}

getFromDict(myDictionary, 'a/b'.split('/'))

You could also create a set in dict function:

def setInDict(dataDict, mapList, value):
    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value