Skip to content

Instantly share code, notes, and snippets.

View jiteshk23's full-sized avatar

Jitesh Khandelwal jiteshk23

  • Tower Research Capital India Pvt. Ltd.
  • Gurgaon, Haryana
  • X @jiteshk23
View GitHub Profile
jiteshk23 /
Last active May 3, 2020 06:22
Compress json with lots of small strings
def compress_json(obj):
Performs string interning for non-compile time small strings.
if isinstance(obj, (str, unicode)):
return intern(str(obj))
elif isinstance(obj, dict):
return { intern(str(k)) : compress_json(v) for k, v in obj.iteritems() }
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
return [ compress_json(e) for e in obj ]
jiteshk23 /
Created April 30, 2020 07:10
Python function to recursively calculate size of an object
import sys
def get_size(obj, seen=None):
"""Recursively finds size of objects"""
size = sys.getsizeof(obj)
if seen is None:
seen = set()
obj_id = id(obj)
if obj_id in seen:
return 0
jiteshk23 /
Created March 4, 2020 14:59
Decorator to run function in a seperate process and binding it to a core
import sys
import traceback
from functools import wraps
from multiprocessing import Process, Queue
def Processify(core):
'''Decorator to run a function as a process.
Be sure that every argument and the return value is *pickable*.
The created process is joined, so the code does not run in parallel.
jiteshk23 /
Last active December 15, 2018 07:57
How to setup clean up functions
import atexit
import signal
import time
def only_once(f):
global called # python 2
called = False
def wrapped(*args, **kwargs):
jiteshk23 / 0_reuse_code.js
Last active August 29, 2015 14:07
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console