Skip to content

Instantly share code, notes, and snippets.

View hughdbrown's full-sized avatar

Hugh Brown hughdbrown

View GitHub Profile
@hughdbrown
hughdbrown / end_of_month.py
Created May 6, 2019 04:11
Generate end of month strings
from datetime import datetime, timedelta
for year in range(2006, 2020):
for month in range(1, 13):
# Get the last day of the previous month
d = datetime(year, month, 1) - timedelta(1)
print(d.strftime("%Y-%m-%d"))
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Index
client = Elasticsearch()
idx = client.indices.stats()
for key in idx['indices'].keys():
if key.startswith('unittest'):
print(key)
Index(key, using=client).delete()
@hughdbrown
hughdbrown / fileattrs.py
Last active November 18, 2018 19:26
Dump directory of files as text with useful file attributes for backup
#!/usr/bin/env python
import os
from datetime import datetime
from hashlib import sha1
OMITTED_FILES = set([".DS_Store"])
def sha1file(filename):
@hughdbrown
hughdbrown / tracking_decorator.py
Created October 26, 2018 15:23
Decorator that can track usage through exceptions
def a(throw_please=False):
_ret = None
_exc = None
try:
print("try")
if throw_please:
b = []
b[1] = 3
_ret = 4
except Exception as exc:
@hughdbrown
hughdbrown / gist:964c984c19a2b81f88a30bf4c511e772
Last active October 12, 2018 21:25
Try to find a meeting time that works for our team
tzs = [
("GMT", 0),
("Boston", -4),
("Denver", -6),
("Singapore", +8),
("Kyiv", +3),
]
def time_in(city, offset):
@hughdbrown
hughdbrown / validation_notes.txt
Created October 2, 2018 16:31
How the DataRobot mixins work
ParamValidationMixin
public_api/rest_generics/controllers.py
- uses flask.request.view_args: http:/x.com/project/<pid>
- derived from flask.MethodView, so REST methods are generated automatically
- http://flask.pocoo.org/docs/0.12/views/#method-based-dispatching
- values are accessible through dict named self.params
- ParamValidationMixin.param_validator_class
QueryValidationMixin
public_api/rest_generics/filters.py
@hughdbrown
hughdbrown / flask_request_notes.txt
Created October 1, 2018 22:20
Notes on how flask request paramets map to an HTTP request
from flask import request
request.view_args
@app.route("/data/<section>")
def data(section):
assert section == request.view_args['section']
For URL Query parameter, use request.args
search = request.args.get("search")
page = request.args.get("page")
@hughdbrown
hughdbrown / lc-timeit.py
Created June 21, 2018 17:54
Compare timing of list comprehension versus naive
def foo(n):
a = []
for i in range(n):
a.append(i * 2)
return a
def bar(n):
return [i * 2 for i in range(n)]
>>> import timeit
@hughdbrown
hughdbrown / lc-disasm.py
Created June 21, 2018 17:34
List comprehension disasm vs. naive disasm
>>> from dis import dis
>>> def foo(n):
... a = []
... for i in range(n):
... a.append(i * 2)
... return a
...
>>> def bar(n):
... return [i * 2 for i in range(n)]
...
from operator import itemgetter
from pprint import pprint
data = [
{'a': a, 'b': b}
for a in reversed(range(4))
for b in reversed(range(10, 18, 2))
]
pprint(data)