Skip to content

Instantly share code, notes, and snippets.

@adamJLev
adamJLev / holidays.py
Created November 18, 2013 21:42
Gets US holidays rrules, to be used with python-dateutil and exrules
from dateutil import rrule
from datetime import datetime
def get_schedule_holidays_rrules():
return [
rrule.rrule(rrule.YEARLY, dtstart=datetime.now(), bymonth=1, bymonthday=1), # New Years
rrule.rrule(rrule.YEARLY, dtstart=datetime.now(), bymonth=5, byweekday=rrule.MO(-1)), # Memorial
rrule.rrule(rrule.YEARLY, dtstart=datetime.now(), bymonth=7, bymonthday=4), # Independence
rrule.rrule(rrule.YEARLY, dtstart=datetime.now(), bymonth=11, byweekday=rrule.TH(4)), # Thanksgiving
rrule.rrule(rrule.YEARLY, dtstart=datetime.now(), bymonth=12, bymonthday=25), # Christmas
@adamJLev
adamJLev / mixins.py
Last active November 14, 2022 15:54
(DEPRECATED) Atomic transactions and Django Rest Framework
# NOTE This is probably no longer needed, now DRF does this
# automatically if you have ATOMIC_REQUESTS enabled.
# https://github.com/encode/django-rest-framework/pull/2887
from django.db import transaction
class AtomicMixin(object):
"""
Ensures we rollback db transactions on exceptions.
@adamJLev
adamJLev / fields.py
Created June 5, 2015 18:43
Stripe-style short alphanumeric UUID fields
from django.db.models import SubfieldBase
from django.core.exceptions import ImproperlyConfigured
from django_extensions.db.fields import ShortUUIDField
class XidField(ShortUUIDField):
"""
XID stands for external ID.
Randomly generated IDs (base62 encoded UUID) used for public display purposes.
The random ID gets a short textual prefix for improved legibility, like Stripe's IDs.
@adamJLev
adamJLev / mysql_fix_data_for_strict.py
Created December 22, 2015 19:37
Django + MySQL script to prepare data for Strict mode
from django.core.management import BaseCommand
from django.db import transaction
from django.db.models import DateTimeField, DateField
from django.db.models.loading import get_models
class Command(BaseCommand):
help = """
One-time use script prepping for SQL strict mode.
Iterates through all models and fixes invalid date/datetime fields.
@adamJLev
adamJLev / mp4-to-m4a.zsh
Created March 16, 2017 07:31
Fix for Spotify not playing some MP4 files
# Context https://community.spotify.com/t5/Desktop-Linux-Windows-Web-Player/Spotify-Does-not-Import-m4a-Files/td-p/1022795
# This ZSH script converts every .mp4 file in current folder to .m4a
# Note: you need to have ffmpeg installed
for file in $(echo **.mp4); do ffmpeg -i "$file" -acodec copy -movflags faststart "$file".m4a; done