Skip to content

Instantly share code, notes, and snippets.

View curiousest's full-sized avatar

Douglas Hindson curiousest

View GitHub Profile
@curiousest
curiousest / compare_pandas_dataframes.py
Created November 25, 2021 17:12
Compare pandas dataframes
# Because I couldn't get this to do what I wanted:
# https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.compare.html
import pandas as pd
from typing import Optional, List
def compare_dataframes(df1, df2, drop_indexes=False, ignore_columns: Optional[List]=None):
'''Returns two dataframes: the rows that only show up in each dataframe'''
if drop_indexes:
df1 = df1.reset_index(drop=True)
@curiousest
curiousest / reducefieldsmixin.py
Created June 3, 2017 11:24
DRF reduce serializer fields mixin
class ReduceFieldsMixin:
def get_fields(self, *args, **kwargs):
fields = super().get_fields(*args, **kwargs)
query_params = getattr(self.context.get('request', {}), 'query_params', {})
requested_fields = query_params.get('fields', None)
if requested_fields:
requested_fields = requested_fields.split(',')
fields = {key: value for key, value in fields.items() if key in requested_fields}
return fields
@curiousest
curiousest / postgres_autonumbering.py
Created May 24, 2017 11:17
Postgres table id prefix
def set_up_autonumbering(engine, table, prefix):
conn = engine.connect()
try:
logging.info("Creating autonumber sequence for {}".format(table))
conn.execute("CREATE SEQUENCE {}_ids".format(table))
except:
logging.info("Autonumber sequence already created".format(table))
try:
class PerRequestFieldsSerializerMixin():
def get_field_names(self, *args, **kwargs):
field_names = super().get_field_names(*args, **kwargs)
request_field_names = getattr(
getattr(kwargs.get('context', {}).get('view', None), 'request', None),
'query_params', {}
).get('fields', None)
if request_field_names is not None:
request_field_names = request_field_names.split(',')
return list(set(field_names) ^ set(request_field_names))
@curiousest
curiousest / read_postgres_sqlalchemy.py
Created November 23, 2016 15:20
Use automap_base to use an existing db with sqlalchemy
from sqlalchemy import MetaData, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.sql import select
from sqlalchemy import func
engine = create_engine('postgres://db_user@db_host:5432/db_name', client_encoding='utf8')
Session = sessionmaker(bind=engine)
session = Session()
@curiousest
curiousest / gist:ed3e595148f48ca427e3
Created October 15, 2015 11:28
Memory efficient Django Queryset Iterator
import gc
# inspiration: https://djangosnippets.org/snippets/1949/
def queryset_memreduce_iterator(queryset, field='pk', queryset_filter='pk__gt',
ordering_function=lambda x, y: x < y,
chunksize=1000):
'''''
Iterate over a Django Queryset ordered by field (kwarg)
jitsu deploy
info: Welcome to Nodejitsu curiousest
info: jitsu v0.12.10-1, node v0.8.21
info: It worked if it ends with Nodejitsu ok
info: Executing command deploy
warn:
warn: The package.json file is missing required fields:
warn:
warn: Subdomain name
warn: