Skip to content

Instantly share code, notes, and snippets.

View treyhunner's full-sized avatar

Trey Hunner treyhunner

View GitHub Profile
@treyhunner
treyhunner / contract.py
Created April 9, 2018 05:25
Code taken from David Beazley's PyCon Israel 2017 keynote
"""
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 March 22, 2016 17:20
splitlines vs. split
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 November 3, 2023 08:13
Test performance of different dictionary merging functions in Python
"""
Results:
multiple_update: 33 ms
copy_and_update: 27 ms
dict_constructor: 29 ms
kwargs_hack: 33 ms
dict_comprehension: 33 ms
concatenate_items: 81 ms
union_items: 81 ms
@treyhunner
treyhunner / alter dict in ChainMap
Created February 20, 2016 01:09
Altering dictionary within 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 February 20, 2016 01:08
Removing items from a 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 February 7, 2016 19:21
List comprehensions lightning talk code copy-pasting examples

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 September 18, 2023 20:21
Test performance of different counting functions in Python
"""
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 April 22, 2015 02:40
example used to demonstrate promise chaining during 2015-04-21 SDJS BYOP night
var something = goGetSomething();
var anotherThing = goGetAnotherThing();
Promise.all([
something,
anotherThing
]).then(function() {
return doAnotherThing();
}).then(function() {
return lastThing();
@treyhunner
treyhunner / restaurants.md
Last active August 29, 2015 14:17
Some of my favorite restaurants

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
@treyhunner
treyhunner / readme.md
Created March 19, 2015 16:12
Front-end Code Playgrounds