Skip to content

Instantly share code, notes, and snippets.

Hugh Brown hughdbrown

Block or report user

Report or block hughdbrown

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@hughdbrown
hughdbrown / create_table_stmt.py
Created Sep 5, 2019
Make a postgresql CREATE TABLE statement by inferring types in a CSV file
View create_table_stmt.py
import os.path
from csv import DictReader
datafile = os.path.normpath(os.path.expanduser("~/workspace/DataRobot/tests/testdata/10k_diabetes.csv"))
with open(datafile) as handle:
reader = DictReader(handle)
fields = reader.fieldnames
rows = list(reader)
@hughdbrown
hughdbrown / maximum_area.py
Last active Aug 12, 2019
Find all connected areas and print size of largest
View maximum_area.py
from queue import Queue
from pprint import pprint
def neighbors(coord):
x, y = coord
return {
(x - 1, y),
(x + 1, y),
(x, y - 1),
(x, y + 1),
@hughdbrown
hughdbrown / absolute_import.py
Last active Aug 9, 2019
All your absolute_importing needs
View absolute_import.py
#!/usr/bin/env python
import os
class SedFile(object):
def __init__(self, filename):
self.filename = filename
self.modified = False
with open(self.filename, "r") as handle:
self.data = [line.rstrip() for line in handle]
@hughdbrown
hughdbrown / fixture-resolve.py
Created Jun 20, 2019
Script to resolve where a pytest fixture lives. I wish I were joking.
View fixture-resolve.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Resolve where some test fixture lives
Provides CLI:
* start -- location in tree of directories to start
* resolve -- resource to resolve
"""
from glob import glob
@hughdbrown
hughdbrown / fixture-resolve.py
Created Jun 20, 2019
Script to resolve where a pytest fixture lives. I wish I were joking.
View fixture-resolve.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Resolve where some test fixture lives
Provides CLI:
* start -- location in tree of directories to start
* resolve -- resource to resolve
"""
from glob import glob
@hughdbrown
hughdbrown / end_of_month.py
Created May 6, 2019
Generate end of month strings
View end_of_month.py
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"))
View gist:18d3a97144cca9bcfb7414dde41134bd
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 Nov 18, 2018
Dump directory of files as text with useful file attributes for backup
View fileattrs.py
#!/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 Oct 26, 2018
Decorator that can track usage through exceptions
View tracking_decorator.py
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 Oct 12, 2018
Try to find a meeting time that works for our team
View gist:964c984c19a2b81f88a30bf4c511e772
tzs = [
("GMT", 0),
("Boston", -4),
("Denver", -6),
("Singapore", +8),
("Kyiv", +3),
]
def time_in(city, offset):
You can’t perform that action at this time.