Skip to content

Instantly share code, notes, and snippets.

Regex & Chill

Gaurav Jain gjain0

Regex & Chill
  • Jaipur
Block or report user

Report or block gjain0

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
gjain0 /
Created May 31, 2018
Get a list of N fibonacci numbers using tail recursion
Fibonacci Number list using tail recursion
def fib_list(n, a, b, r):
if n == 0: return [1]
elif n < 0: return []
r.append(a + b)
fib(n-1, b, a + b, r)
return r
gjain0 /
Created May 1, 2018
flatten a python list of lists
# source: flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
def flatten(x):
if type(x) == list:
a = []
for l in x:
for y in flatten(l):
return a
gjain0 /
Last active Sep 6, 2018
Dynamic env variables load from .env file while executing <command>
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.settings.base")
from import execute_from_command_line
# ---------- 5 lines ------------
gjain0 /
Created Apr 19, 2018
Source nested Dictionary filtering based on mapping dict whose all keys are present in Source dictionary in the same structure
def filter_dict(src_dict, mapping_dict):
for k, v in list(src_dict.items()):
if k not in mapping_dict:
del src_dict[k]
elif isinstance(v, dict) and isinstance(mapping_dict[k], dict):
src_dict[k] = filter_dict(v, mapping_dict[k])
return src_dict
# ------------------------ TEST ------------------------
gjain0 /
Last active Oct 3, 2019
Create HMAC SHA256 signature/encryption/encode
import hmac
import hashlib
import binascii
def create_sha256_signature(key, message):
gjain0 /
Created Oct 31, 2017
Use JSONField properties in Django admin filter Raw
# You have a model something like this
from django.contrib.postgres.fields import JSONField
class MyModel(models.Model):
jsonfield = JSONField() # {"name": "Gaurav", "age": "25", "address": {"country": "India", "city": "Jaipur"}}
# few more fields...
# And in admin you want to create filter for jsonfield properties/keys
# for e.g. in above case we want to show filter for age and country
import yaml
from rest_framework.compat import coreapi, urlparse
from rest_framework.schemas import SchemaGenerator
from rest_framework_swagger import renderers
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import exceptions
from rest_framework.permissions import AllowAny
You can’t perform that action at this time.