This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework.authentication import SessionAuthentication | |
class CsrfExemptSessionAuthentication(SessionAuthentication): | |
def enforce_csrf(self, request): | |
return # To not perform the csrf check previously happening |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
def timedelta_to_string(timedelta): | |
total_seconds = timedelta.total_seconds() | |
if total_seconds < 0: | |
timedelta_str = '-' + str(datetime.timedelta() - timedelta) | |
else: | |
timedelta_str = '+' + str(timedelta) | |
return timedelta_str |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def import_name(name): | |
components = name.split('.') | |
mod = __import__( | |
'.'.join(components[0:-1]) | |
) | |
for m in components[1:]: | |
mod = getattr(mod, m) | |
return mod |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
def deep_search(needles, haystack): | |
found = {} | |
if type(needles) != type([]): | |
needles = [needles] | |
if type(haystack) == type(OrderedDict()) or type(haystack) == type(dict()): | |
for needle in needles: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DisableMigrations(object): | |
def __contains__(self, item): | |
return True | |
def __getitem__(self, item): | |
return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pprint(data, ident=2, start_ident=0, ident_char=' ', inside_dict=False, inside_list=False, line_length=80, | |
line_context=0): | |
if not inside_dict: | |
print(ident_char * start_ident, end='') | |
if len(repr(data)) + start_ident + line_context < line_length: | |
print(repr(data) + (',' if inside_dict or inside_list else '')) | |
return | |
elif isinstance(data, dict): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import sys | |
from functools import wraps | |
import logging | |
logger = logging.getLogger(__name__) | |
WARN_ON_MISSING = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example usage: | |
# class Car(models.Model): | |
# class Colors(ChoiceEnum): | |
# RED = 'red' | |
# WHITE = 'white' | |
# BLUE = 'blue' | |
# | |
# color = models.CharField(max_length=5, choices=Colors.choices(), default=Colors.RED) | |
# | |
# Querying requires an extra word to type though... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def calculate_initial_compass_bearing(pointA, pointB): | |
""" | |
Calculates the bearing between two points. | |
The formulae used is the following: | |
θ = atan2(sin(Δlong).cos(lat2), | |
cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def zstd_readline(filename, chunksize=zstd.DECOMPRESSION_RECOMMENDED_OUTPUT_SIZE): | |
with open(filename, 'rb') as fh: | |
with zstd.ZstdDecompressor().stream_reader(fh) as reader: | |
leftovers = '' | |
while True: | |
chunk = reader.read(chunksize).decode('utf-8') | |
if not chunk: | |
break |