python

normalize json

from pandas.io.json import json_normalize
json_normalize([{"a":{"b":1}}])

pandas

res = CONN.execute("SELECT * FROM information_schema.TABLES")
df = pd.DataFrame(res.fetchall(), columns=res.keys())

matplotlib

import matplotlib.pyplot as plt

update and return a map

a.update(b=(1 + a.get("b", 0))) or a

reduce

from functools import reduce
reduce(lambda current, value : current.update({value: current[value] + 1} ) or current, res2, defaultdict(int))

group_by

from itertools import groupby

mysql

pip install mysql-connector-python
engine = create_engine("mysql+mysqlconnector://127.0.0.1/phrase_test")

postgres

pip install psycopg2-binary

gzip

with gzip.open("out.txt.gz", mode="rt") as f:
    f.write("foo\n")
    f.write("bar\n")
    f.write("baz\n")

json

with gzip.open("out.json.gz", mode="wt") as f:
    payload = {"foo": "bar", "baz": "badass"}
    json.dump(payload, f)

time delta

from datetime import timedelta
delta = timedelta(days=365) # or seconds, microseconds
print("%.06f" % (delta.total_seconds()))

time format

datetime.now().isoformat()

calendar