Skip to content

Instantly share code, notes, and snippets.

View jackton1's full-sized avatar

Tonye Jack jackton1

View GitHub Profile
@jackton1
jackton1 / django_filters_postgres_datetime_range.py
Last active March 6, 2018 16:16
Extending django_filters DateTimeFromToRangeFilter to support postgres datetime tstzrange fields with the available lookup names.
from django.contrib.postgres.forms import DateTimeRangeField
from django.core.exceptions import ImproperlyConfigured
from django_filters import DateTimeFromToRangeFilter as DateTimeFromToRangeGenericFilter
class DateTimeTZFromToRangeFilter(DateTimeFromToRangeGenericFilter):
field_class = DateTimeRangeField
lookup_names = {
'contained_by', 'contains', 'overlap', 'fully_lt', 'full_gt',
'not_lt', 'not_gt', 'adjacent_to',
@jackton1
jackton1 / drf_optimize.py
Last active October 6, 2023 22:37
Optimize Django Rest Framework model views queries.
from django.db import ProgrammingError, models
from django.db.models.constants import LOOKUP_SEP
from django.db.models.query import normalize_prefetch_lookups
from rest_framework import serializers
from rest_framework.utils import model_meta
class OptimizeModelViewSetMetaclass(type):
"""
This metaclass optimizes the REST API view queryset using `prefetch_related` and `select_related`
if the `serializer_class` is an instance of `serializers.ModelSerializer`.
@jackton1
jackton1 / serving-sphinx-docs-on-s3.md
Created July 3, 2018 11:51 — forked from monkut/serving-sphinx-docs-on-s3.md
Serving Sphinx Documentation on s3

Build your sphinx document

Assumes you already have a sphinx project installed and a project created

make html

By default this command will build documentation to _build/html

'use strict';
const co = require('co');
const EventEmitter = require('events');
const Promise = require('bluebird');
const AWS = require('aws-sdk');
const ssm = Promise.promisifyAll(new AWS.SSM());
const DEFAULT_EXPIRY = 3 * 60 * 1000; // default expiry is 3 mins
@jackton1
jackton1 / how-to-copy-aws-rds-to-local.md
Created July 25, 2018 20:28 — forked from syafiqfaiz/how-to-copy-aws-rds-to-local.md
How to copy production database on AWS RDS(postgresql) to local development database.
  1. Change your database RDS instance security group to allow your machine to access it.
    • Add your ip to the security group to acces the instance via Postgres.
  2. Make a copy of the database using pg_dump
    • $ pg_dump -h <public dns> -U <my username> -f <name of dump file .sql> <name of my database>
    • you will be asked for postgressql password.
    • a dump file(.sql) will be created
  3. Restore that dump file to your local database.
    • but you might need to drop the database and create it first
    • $ psql -U <postgresql username> -d <database name> -f <dump file that you want to restore>
  • the database is restored
@jackton1
jackton1 / gist:9b0f2b5e471b7fbf5a385736f200cda3
Created November 17, 2018 15:49 — forked from Bouke/gist:11261620
Multiple Python installations on OS X

Previous versions used homebrew to install the various versions. As suggested in the comments, it's better to use pyenv instead. If you are looking for the previous version of this document, see the revision history.

$ brew update
$ brew install pyenv
$ pyenv install 3.5.0
$ pyenv install 3.4.3
$ pyenv install 3.3.6
$ pyenv install 3.2.6
$ pyenv install 2.7.10

$ pyenv install 2.6.9

@jackton1
jackton1 / postgres-lookup.md
Last active February 20, 2024 04:33
Django Postgres range field lookups
@jackton1
jackton1 / restricted_fields.py
Created July 11, 2019 21:30
Restricting DRF serialized fields using only and defer.
from collections import OrderedDict
from rest_framework.fields import SkipField
from rest_framework.relations import PKOnlyObject
class RestrictedFieldsSerializerMixin(object):
"""
API Serializer mixin which provides support for restricting serialized data to only a subset of fields.
@jackton1
jackton1 / bench.py
Created July 30, 2020 23:03 — forked from 1st1/bench.py
immutables benchmark
import pyrsistent
import immutables
import time
I = 1_000_000
KEY = '5'
for N in [5, 10, 20, 30, 100, 200, 300, 400, 500, 1000]:
print('=============')
from datetime import datetime, timedelta
import boto3
from dateutil.tz import UTC
client = boto3.client('iam')
fifteen_days_ago = datetime.now(tz=UTC) - timedelta(days=15)
paginator = client.get_paginator('list_roles')