Skip to content

Instantly share code, notes, and snippets.

@lovesh
lovesh / admin.py
Last active November 7, 2019 19:37
django-admin register all models
"""
Here models are in different modules and the models.py imports them from different modules like this
from model1 import * # or the name of models you want to import
from model2 import User
or you might write all your models in models.py
"""
@lovesh
lovesh / mysql_wrapper.py
Created December 8, 2013 18:39
A MySQLdb wrapper class
import MySQLdb
import MySQLdb.cursors
from config import config
class Cursor(object):
def __init__(self, mysql_cursor):
self.cursor = mysql_cursor
def __iter__(self):
@lovesh
lovesh / gcm_wrapper.py
Last active August 29, 2015 13:57
GCM wrapper in python using python requests. Allows sending message to multiple senders
import requests
import simplejson as json
import os
class GCMMultiRequest(object):
def __init__(self, gcm_url, headers):
self.gcm_url = gcm_url
self.headers = headers
self.session = requests.Session()
@lovesh
lovesh / tracker.js
Created April 23, 2014 07:46
code to integrate multiple services like kissmetrics, intercom, etc easily
/*
An array of services that you want to integrate. I didnt make it an object because these services are started and stopped in the order they appear in the array.
Here i have taken 3 examples- kissmetrcs, intercom and vero
Service properties:
enabled- Setting it to false doesnt start or track events for this service
start- This function is initialises the servcie. In this function i place the initialization code snipplet that the service provides me.
Takes an optional array which has the service names i want to start. If this parameter is ommited all services are started
stop- Some services like intercom have a shutdown function, so here goes that code
track- This is the tracking function that is used to track events with the service. Takes 2 parameters eventName and eventData
validate: Sometimes you dont want to send tracking for some accounts, maybe they are test accounts or you are logging into some account as admin.
@lovesh
lovesh / httpWrapper.scala
Last active August 29, 2015 14:01
Http request wrapper build with scalaj-http
import scalaj.http._
case class httpResponse(code: Int, body: String, url: String)
object httpRequest {
val options = Map(
"connTimeout" -> 5000,
"readTimeout" -> 5000
)
@lovesh
lovesh / dom.py
Last active August 29, 2015 14:06
playing with HTML DOM using lxml
import lxml.html
import urllib2
class DOM(object):
def __init__(self, url=None, html=None, utf8=False):
if url:
page = urllib2.urlopen(url)
html = page.read()
page.close()
@lovesh
lovesh / email_generator.py
Created October 8, 2014 03:59
Email generator according to the spreadsheet http://bit.ly/name2email
import itertools
name = 'FirstName MiddleName LastName'
company_domain = 'company.com'
name_parts = name.lower().split(' ')
fname, mname, lname = None, None, None
if len(name_parts) > 0:
fname = name_parts[0]
Verifying that +lovesh is my blockchain ID. https://onename.com/lovesh
@lovesh
lovesh / ujson_msgpack_benchmark.py
Created July 17, 2017 19:49
Comparison of serialisation and de-serialisation times on some plenum message
from random import shuffle
import ujson
import msgpack
import time
data = [
{"op":"REPLY","result":{"txnTime":1500296923,"rootHash":"ChEG5bpqvdKYhyoqSziJ5r7qtyRtkmjQYzErEW2DpuqC","type":"buy","auditPath":["3uAnG8h5rS5nwviEj49YTje8GA8tS19RsUP3MvSju9gG"],"signature":"5RGoVb8Rsc4H1jn2FZdueBAZ3kMzwJhEzLUjxtMhzXSBnQBuE438uj8Bg1b5eP82EYNKVNwFSarztQoDszid6n8w","reqId":1500316722698678,"identifier":"4AdS22kC7xzb4bcqg9JATuCfAMNcQYcZa1u5eWzs6cSJ","seqNo":33,"amount":10}},
{"op":"REPLY","result":{"txnTime":1500296923,"rootHash":"FMWErPC9tCMusWjwfgLkhKoibQbwWWoCYWjFj9TTmHuj","type":"buy","auditPath":["AbBrT7ScWCPdKkCue5SyWx8jCvUFwgDPBSumNtGmuLYG","7qB28FcEsR93j3CrmYpAG5r1JJZAVrNHWtMKcYHspgkY","7CGsMw6jBaq8Cm5dfb1BmoV1LHk4kVjRYV83r4B1xMw9"],"signature":"3xuFE9kEo8L991Z4ztLuwgyeUTTbTW6dauKLcMbbeFffK1hRj6ywTGz3m5nhzEk5LqQQsfXTd7rq6hHchnpUSxzg","reqId":1500316722693789,"identifier":"4AdS22kC7xzb4bcqg9JATuCfAMNcQYcZa1u5eWzs6cSJ","seqNo":29,"amount":28}},
{"op":"REPLY","result":{"txnTime":1500296923,"rootHash":"Dj2VKTBE
@lovesh
lovesh / rocks_int_comparator.py
Created July 17, 2017 22:35
# Add integer keys in rocksdb and get highest and lowest keys using a custom comparator
# Add integer keys in rocksdb and get highest and lowest keys using a custom comparator
import random
import string
from time import perf_counter
import rocksdb
from rocksdb.interfaces import Comparator
class IntegerComparator(Comparator):