Skip to content

Instantly share code, notes, and snippets.

Amit Upadhyay amitu

Block or report user

Report or block amitu

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@amitu
amitu / encoded_key.py
Last active Apr 29, 2016
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).
View encoded_key.py
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 Apr 12, 2016
Self documenting makefile
View 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 Mar 17, 2016
ZSH: Print time to execute each command as it is executed.
View .zshrc
preexec () {
START="$(python -c 'import time; print time.time()')"
LAST_CMD=$1
}
precmd () {
LAST=$?
if [ -z ${START} ];
then
else
View CICharField.py
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
View clone_schema.sql
-- 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 Jan 15, 2016
DB Performance - Index Usage - Bad Ones
View index_usage_bad_ones.sql
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
View postgres_stats.sql
with table_stats as (
select psut.relname,
psut.n_live_tup,
1.0 * psut.idx_scan / greatest(1, psut.seq_scan + psut.idx_scan) as index_use_ratio
from pg_stat_user_tables psut
order by psut.n_live_tup desc
),
table_io as (
select psiut.relname,
sum(psiut.heap_blks_read) as table_page_read,
View luigi_example.py
import luigi
class SimpleTask(luigi.Task):
name = luigi.Parameter()
def output(self):
return luigi.LocalTarget(self.name)
def run(self):
View .psqlrc
\pset linestyle unicode
\pset border 2
\set PROMPT1 '%[%033[33;1m%]%x%[%033[0m%]%[%033[1m%]%/%[%033[0m%]%R%# '
\pset pager off
\timing
\pset format wrapped
@amitu
amitu / Makefile
Created Jul 27, 2015
reload browser on sass change
View Makefile
build:
sassc -I foundation/scss scss/main.scss > css/main.css
osascript reload-chrome.applescript
watch:
make build
fswatch -e main.css scss | xargs -n1 make build
You can’t perform that action at this time.