Skip to content

Instantly share code, notes, and snippets.

View amitu's full-sized avatar
😀
Building fastn.com

Amit Upadhyay amitu

😀
Building fastn.com
View GitHub Profile
@amitu
amitu / hindic-top1000.txt
Last active May 21, 2016 18:47
https://github.com/amitu/scriptic/blob/master/cmu.py generated transliteration for 1000 most frequently used words in english
you: Y UW1 => यू
I: AY1 => आय
to: T UW1 => टू
the: DH AH0 => दै
a: AH0 => ऐ
and: AH0 N D => ऐन्ड
that: DH AE1 T => दैट
it: IH1 T => इट
of: AH1 V => आव
me: M IY1 => मी
@amitu
amitu / encoded_key.py
Last active April 29, 2016 11:00
Django Model base class for encoded key. Useful for when you want to pass id in URL or JSON, but do not leak data to world (about how many objects you have of that kind).
from Crypto.Cipher import AES
from Crypto import Random
import base64
import binascii
import struct
from django.db import models
from django.conf import settings
@amitu
amitu / Makefile
Created April 12, 2016 07:49
Self documenting makefile
.DEFAULT_GOAL := help
node_modules: ## Install dependencies.
node_modules: package.json
npm install
help: ## Show this help.
@IFS=$$'\n' ; \
help_lines=(`fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##/:/'`); \
for help_line in $${help_lines[@]}; do \
@amitu
amitu / .zshrc
Last active March 17, 2016 11:07
ZSH: Print time to execute each command as it is executed.
preexec () {
START="$(python -c 'import time; print time.time()')"
LAST_CMD=$1
}
precmd () {
LAST=$?
if [ -z ${START} ];
then
else
@amitu
amitu / CICharField.py
Created March 5, 2016 18:06
CICharField
class CICharField(models.CharField):
def db_type(self, connection):
assert connection
return 'CITEXT'
def from_db_value(self, value, expression, connection, context):
if value and isinstance(value, str):
value = value.decode("utf-8")
return value
@amitu
amitu / clone_schema.sql
Created February 7, 2016 09:35
clone schema
-- Function: clone_schema(text, text)
-- DROP FUNCTION clone_schema(text, text);
CREATE OR REPLACE FUNCTION clone_schema(
source_schema text,
dest_schema text,
include_recs boolean)
RETURNS void AS
$BODY$
@amitu
amitu / index_usage_bad_ones.sql
Last active January 15, 2016 08:31
DB Performance - Index Usage - Bad Ones
WITH query_stat AS (
SELECT
relname, 100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used,
n_live_tup rows_in_table, idx_scan, seq_scan
FROM
pg_stat_user_tables
WHERE
seq_scan + idx_scan > 0
ORDER BY
seq_scan DESC
@amitu
amitu / manage.py
Created November 6, 2013 05:30
Find out why is something getting imported, as import fails in django sometimes leads to unhelpful "ImproperlyConfigured" exception
#!/usr/bin/env python
import sys, traceback
class ImportBlocker(object):
def find_module(self, fullname, path=None):
if "mobil" in fullname and "mobile" not in fullname:
print fullname
print traceback.print_stack()
raise Exception()
@amitu
amitu / load_keen.py
Last active December 24, 2015 07:49
Upload some test data to keen.io
# find the list of backup files
# for each file see the position till where we have read the file from "stack"
# for each file see if there is more in it.
# if there is more, start reading and writing to keen
# find total size of all files
# go thru in ls -t order so we always traverse them in same order
from progressbar import ProgressBar
import keen, pdb, json, time
@amitu
amitu / views.py
Created September 15, 2013 17:28
djangothis with labels
from importd import d
from path import path
from datetime import datetime
from djangothis.app import dotslash, read_yaml, watchfile
from django.shortcuts import render
posts = None
labels = {}