Skip to content

Instantly share code, notes, and snippets.

Trey Hunner treyhunner

Block or report user

Report or block treyhunner

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
@treyhunner
treyhunner / choice_enum.py
Created Apr 9, 2018
Enum for use with Django ChoiceField
View choice_enum.py
from enum import Enum, EnumMeta
class ChoiceEnumMeta(EnumMeta):
def __iter__(self):
return ((tag, tag.value) for tag in super().__iter__())
class ChoiceEnum(Enum, metaclass=ChoiceEnumMeta):
"""
@treyhunner
treyhunner / contract.py
Created Apr 9, 2018
Code taken from David Beazley's PyCon Israel 2017 keynote
View contract.py
"""
Code based on talk by David Beazley's PyCon Israel keynote in 2017
Watch the talk at https://www.youtube.com/watch?v=Je8TcRQcUgA
Usage::
from contract import Base, PositiveInteger
dx: PositiveInteger
@treyhunner
treyhunner / splits.py
Created Mar 22, 2016
splitlines vs. split
View splits.py
import re
import unittest
def splitlines(string, keepends=False):
lines = re.findall('(.*\n|.+$)', string)
return lines if keepends else [x.rstrip('\n') for x in lines]
def split(string):
@treyhunner
treyhunner / time_dict_merge.py
Last active Jun 22, 2019
Test performance of different dictionary merging functions in Python
View time_dict_merge.py
"""
Results:
multiple_update: 57 ms
copy_and_update: 46 ms
dict_constructor: 56 ms
kwargs_hack: 45 ms
dict_comprehension: 45 ms
concatenate_items: 166 ms
union_items: 163 ms
@treyhunner
treyhunner / alter dict in ChainMap
Created Feb 20, 2016
Altering dictionary within ChainMap
View alter dict in ChainMap
>>> from collections import ChainMap
>>> user_context = {'name': "Trey", 'website': "http://treyhunner.com"}
>>> global_context = {'name': "Anonymous User", 'page_name': "Profile Page"}
>>> context = ChainMap({}, user_context, global_context)
>>> context['name']
'Trey'
>>> user_context['name'] = "Guido"
>>> context['name']
'Guido'
@treyhunner
treyhunner / remove from ChainMap
Created Feb 20, 2016
Removing items from a ChainMap
View remove from ChainMap
>>> from collections import ChainMap
>>> user_context = {'name': "Trey", 'website': "http://treyhunner.com"}
>>> global_context = {'name': "Anonymous User", 'page_name': "Profile Page"}
>>> context = ChainMap({}, user_context, global_context)
>>> context['page_name'] = "About Page"
>>> context['page_name']
'About Page'
>>> del context['page_name']
>>> context['page_name']
'Profile Page'
@treyhunner
treyhunner / doubled_numbers.md
Created Feb 7, 2016
List comprehensions lightning talk code copy-pasting examples
View doubled_numbers.md

If we have a for loop that converts one list to another list using a transformation for each element:

doubled_numbers = []
for n in numbers:
    doubled_numbers.append(n * 2)

We can make that into a list comprehension by copy-pasting the assignment, the append value, and the for clause:

@treyhunner
treyhunner / time_count_functions.py
Last active Dec 27, 2018
Test performance of different counting functions in Python
View time_count_functions.py
"""
Test performance of these counting methods:
- count_if_else: Set to 1 if not yet seen and increment otherwise
- count_if: Set to 0 if not yet seen, then increment regardless of containment
- count_exception: Attempt to increment and set to 1 if KeyError caught
- count_setdefault: Set default value to 0, then increment
- count_fromkeys: Create dict with necessary keys set to 0, then increment each
- count_set_and_comprehension: Create dict of items and counts using a set
- count_defaultdict: Increment count, automatically setting unseen values to 0
@treyhunner
treyhunner / after.js
Created Apr 22, 2015
example used to demonstrate promise chaining during 2015-04-21 SDJS BYOP night
View after.js
var something = goGetSomething();
var anotherThing = goGetAnotherThing();
Promise.all([
something,
anotherThing
]).then(function() {
return doAnotherThing();
}).then(function() {
return lastThing();
@treyhunner
treyhunner / restaurants.md
Last active Aug 29, 2015
Some of my favorite restaurants
View restaurants.md

Japanese

[Kula Sushi][]

  • Uber-modern gamified conveyor belt sushi
  • Eat 100% nigiri. It's really cheap there. Rolls are just okay.

[Chopstix][] (katsu and curries)

  • Katsu Don
You can’t perform that action at this time.