Skip to content

Instantly share code, notes, and snippets.

@ketanbhatt
ketanbhatt / json2csv.py
Created February 4, 2017 12:32
Converts rows of JSON to CSV
import sys
import csv
import ujson
input_file_name, output_file_name = sys.argv[1:3]
headers = sys.argv[-1].split(',')
print input_file_name,
output_rows = []
from django.contrib.admin import ModelAdmin
class MyTableAdmin(ModelAdmin):
...
paginator = LargeTablePaginator
...
@ketanbhatt
ketanbhatt / create_tables.sql
Last active May 14, 2017 09:39
Avoid Joins: Creating Tables and Inserting Data
-- User Table
CREATE TABLE user (
id serial PRIMARY KEY,
account_id int not NULL,
name varchar(10)
);
-- Purchase Table
CREATE TABLE purchase (
id SERIAL PRIMARY KEY,
@ketanbhatt
ketanbhatt / join.sql
Created May 14, 2017 09:47
Avoid Joins: Join Query
SELECT "purchase"."id"
FROM "purchase"
INNER JOIN "user" ON ("purchase"."user_id" = "user"."id")
WHERE "user"."account_id" IN
(SELECT generate_series(1,1000));
@ketanbhatt
ketanbhatt / evaluate_select.sql
Last active October 18, 2019 09:37
Avoid Joins: Evaluate and Select Query
WITH user_ids AS
(SELECT id
FROM user
WHERE account_id IN
(SELECT generate_series(1,1000)))
SELECT purchase.id
FROM purchase
WHERE user_id IN
(SELECT id
FROM user_ids);
@ketanbhatt
ketanbhatt / raw_sql_example.py
Last active July 8, 2017 11:04
Original Query with RawSQL
q = MyModel.objects.values('some_fk_id').annotate(
avg_duration=Avg('duration'),
perc_90_duration=RawSQL('percentile_disc(%s) WITHIN GROUP (ORDER BY duration)', (0.9,)),
)
print q.query
# SELECT "some_fk_id",
# AVG("duration") AS "avg_duration",
# (percentile_disc(0.9) WITHIN
@ketanbhatt
ketanbhatt / raw_annotation_example.py
Created July 8, 2017 11:04
Query with RawAnnotation
class RawAnnotation(RawSQL):
"""
RawSQL also aggregates the SQL to the `group by` clause which defeats the purpose of adding it to an Annotation.
"""
def get_group_by_cols(self):
return []
# The Query
q = MyModel.objects.values('some_fk_id').annotate(
avg_duration=Avg('duration'),
@ketanbhatt
ketanbhatt / sentry_client.py
Created February 25, 2018 04:07
Custom Sentry Client for ignoring specific errors
from django.conf import settings
from raven.contrib.django.client import DjangoClient
ignore_exc_msg_prefixes = getattr(settings, 'CUSTOM_SENTRY_CLIENT_IGNORE_EXCEPTION_MESSAGE_PREFIXES', [])
class IgnoreExceptionsDjangoClient(DjangoClient):
"""
Overrides DjangoClient's `skip_error_for_logging` method.CUSTOM_SENTRY_CLIENT_IGNORE_EXCEPTION_MESSAGE_PREFIXES
@ketanbhatt
ketanbhatt / db_timeouts.py
Last active March 31, 2018 03:12
DB Timeouts from Django App
from django.conf import settings
# NOTE
# The timeout values here only restrict the roles from application code. The actual timeout set in the DB could
# be a different value.
# Timeout set in DB on 27/03/2018 is 50s.
DEFAULT_DB_TIMEOUT_IN_MS = 50000
default_conn = settings.DJANGO_DEFAULT_DB_CONNECTION_NAME
@ketanbhatt
ketanbhatt / stream_files.js
Created September 22, 2018 11:56
Stream Files to Amazon S3
var express = require('express');
var router = express.Router();
var multer = require('multer'), //for handling multipart/form-data
fs = require('fs'),
S3FS = require('s3fs'), //abstraction over Amazon S3's SDK
s3fsImpl = new S3FS('your-bucket-here', {
accessKeyId: 'Your-IAM-Access',
secretAccessKey: 'Your-IAM-Secret'
});