Skip to content

Instantly share code, notes, and snippets.

@robdmc
robdmc / logging_example.py
Created November 4, 2015 19:23
Python Logging Example
#! /usr/bin/env python
import logging
"""
I never remember all the steps for setting up logging in python.
This gist goes through the steps so I can copy and paste what I need.
"""
# create logger on the current module and set its level
logger = logging.getLogger(__file__)
@robdmc
robdmc / beta_distribution_ranking.py
Created November 9, 2015 19:58
Beta Distribution Ranking
import numpy as np
from scipy import stats
import pandas as pd
def score_for_confidence(alpha, beta, confidence=.95):
"""
alpha: 1 + num_positive
beta: 1 + num_negative
confidence: confidence you can have that positive fraction is greater than this
"""
@robdmc
robdmc / db_stash.sh
Created November 12, 2015 22:13
Stash the current dev database in vagrant
#! /usr/bin/env bash
echo "DROP DATABASE IF EXISTS ambition_dev_stash; CREATE DATABASE ambition_dev_stash WITH TEMPLATE ambition_dev OWNER ambition_dev;" | psql
#! /usr/bin/env bash
echo "DROP DATABASE IF EXISTS ambition_dev; CREATE DATABASE ambition_dev WITH TEMPLATE ambition_dev_stash OWNER ambition_dev;" | psql
@robdmc
robdmc / django_query_sql.py
Created December 7, 2015 21:19
sql and query plan for django queryset
mcqs = MetricConfig.objects.filter(name__in=['lme_order_volume_profit', 'broadsoft_call_incoming_calls'])
qs = Animal(mcqs, 'day').all()
#qs = qs.filter(time__lte=datetime.datetime(2020, 1, 1,1).date()) #, time__gte=datetime.datetime(1970, 1, 2,1))
print str(qs.query)
print
cursor = connections[qs.db].cursor()
cursor.execute('explain %s' % str(qs.query))
for s in cursor.fetchall():
@robdmc
robdmc / install_ucs4_python.sh
Last active December 10, 2015 15:44
install python with ucs4 unicode library
#! /usr/bin/env bash
# RUN THIS SCRIPT WITH SUDO !
# You should add the following the the `dependencies` section of your circle file
# cache_directories:
# - ./Python-2.7.9.tgz
# - ./Python-2.7.9
set -x
@robdmc
robdmc / style_pandas.py
Created February 23, 2016 19:30
Add Styling to dataframes
# from
# http://pandas.pydata.org/pandas-docs/stable/style.html
df.style.background_gradient(cmap='viridis', low=1, high=0).set_precision(1)
@robdmc
robdmc / number_formatter.py
Last active March 18, 2016 19:21
Format numbers like %g, but never use scientific notation
import numbers
import re
def formatter(val, precision=2):
"""
Converts numbers to a have a given precision in a way that nicely displays in UI
elements.
Inputs:
val: input value (either numeric or string)
@robdmc
robdmc / ecdf.py
Last active March 8, 2016 15:30
Plot CDF
import pylab as pl
from statsmodels.distributions.empirical_distribution import ECDF
def ecdf(x, n_out=100, label=None, xlabel=None, ylabel=None):
x_out = np.linspace(min(x), max(x), n_out)
y_out = ECDF(x)(x_out)
# pl.plot(x_out, 1 - y_out, '-', label=label)
pl.fill_between(x_out, 100*(1 - y_out), label=label, alpha=.3)
if label:
pl.legend(loc='best')
if xlabel:
@robdmc
robdmc / csv_logger.py
Last active December 13, 2022 09:57
Useful CSV logging with python
#! /usr/bin/env python
import logging
import datetime
import fleming
import pytz
import os
from itertools import islice
import sys