Skip to content

Instantly share code, notes, and snippets.

View gauravvjn's full-sized avatar
🍿
Regex & Chill

Gaurav Jain gauravvjn

🍿
Regex & Chill
View GitHub Profile
View import_all_items_from_all_modules_in_a_package.py
def import_all_items_from_all_modules_in_a_package():
"""
Put and call this function in the __init__.py file of the package.
This function dynamically loads all non-private classes, functions & variables from all modules
with in the current package. this is equivalent to below
```
from .abc import *
from .xyz import *
.
@gauravvjn
gauravvjn / fibonacci_list_tail_recursion.py
Created May 31, 2018 06:44
Get a list of N fibonacci numbers using tail recursion
View fibonacci_list_tail_recursion.py
"""
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
@gauravvjn
gauravvjn / flatten_list.py
Created May 1, 2018 13:23
flatten a python list of lists
View flatten_list.py
# 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):
a.append(y)
return a
else:
@gauravvjn
gauravvjn / manage.py
Last active September 6, 2018 08:06
Dynamic env variables load from .env file while executing manage.py <command>
View manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.settings.base")
from django.core.management import execute_from_command_line
# ---------- 5 lines ------------
@gauravvjn
gauravvjn / dict_filter_using_map_dict.py
Created April 19, 2018 10:31
Source nested Dictionary filtering based on mapping dict whose all keys are present in Source dictionary in the same structure
View dict_filter_using_map_dict.py
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 ------------------------
@gauravvjn
gauravvjn / create_sha256_signature.py
Last active February 7, 2022 23:58
Create HMAC SHA256 signature/encryption/encode
View create_sha256_signature.py
"""
https://gist.github.com/Azadehkhojandi/50eaae4cf20b21faef186f2c8ee97873
"""
import hmac
import hashlib
import binascii
def create_sha256_signature(key, message):
@gauravvjn
gauravvjn / admin.py
Last active August 17, 2023 20:27
Use JSONField properties in Django admin filter Raw
View admin.py
"""
More details on the implementation and usage can be found at
https://www.pyscoop.com/django-jsonfield-attributes-in-admin-filter/
"""
from django.contrib import admin
from django.core.exceptions import ImproperlyConfigured
class JSONFieldFilter(admin.SimpleListFilter):
View swagger_utils.py
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