Skip to content

Instantly share code, notes, and snippets.

View cypreess's full-sized avatar

Kris Dorosz cypreess

View GitHub Profile
@cypreess
cypreess / pre-commit-black
Last active February 26, 2019 15:27
git pre-commit hook that automatically checks if black formatter should be applied and override files before a commit
#!/bin/bash
# Author: Kris Dorosz <cypreess@gmail.com>
# This git pre commit hook will apply black formatting before any commit and make sure all tracked files are reformatted
echo "Using" `black --version`
black --check . 2>/dev/null
if [ $? -ne 0 ]; then
echo "Reformatting:"
@cypreess
cypreess / metrics.py
Last active January 18, 2019 14:06
metrics.py
import statsd
from django.conf import settings
statsd_client = statsd.StatsClient(
settings.METRICS_STATSD_HOST, settings.METRICS_STATSD_PORT
)
def metric_counter(key, value=1):
if settings.METRICS_ENABLED:
#!/usr/bin/env python
# Author: Kris Dorosz
# Enter the root directory of your project and type `squash`
# This should squash all not tracked by git migrations and remove the old files
import delegator
from collections import defaultdict
import sys
from subprocess import call
import time
import datetime
class MeasureDuration:
def __init__(self, title: str = "Total time taken %s"):
self.start = None
self.end = None
self.title = title
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
@cypreess
cypreess / compassbearing.py
Last active July 23, 2018 11:52 — forked from jeromer/compassbearing.py
compass bearing between two points in Python
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))
@cypreess
cypreess / django_enum_choices.py
Created July 11, 2018 08:18
Django choices helper class
# 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...
@cypreess
cypreess / 12factors.py
Last active July 6, 2018 11:06
Lightweight helpers to read settings from environement variables
import os
import re
import sys
from functools import wraps
import logging
logger = logging.getLogger(__name__)
WARN_ON_MISSING = True
@cypreess
cypreess / pprint
Created June 28, 2018 10:41
Pretty printer
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):
@cypreess
cypreess / disable_migrations
Created June 28, 2018 10:32
Easily disable migratinons when testing in your django project, set `MIGRATION_MODULES = DisableMigrations()`in settings.py
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return None