Skip to content

Instantly share code, notes, and snippets.

@radix
radix / gist:cbae889cc41e912987ac
Created June 9, 2014 19:55
updated io vs effect explanation
## IO Monad for Python
Effects are vaguely analogus to IO monads. The Effect class can be compared
to the IO type, which tags (or wraps) your result type, and
`Effect.on_success` is somewhat like the bind function (`>>=`), indicating
that the function passed is to be called with the result of the effect.
Haskell's "Either" can be thrown in to handle `.on_success` vs `.on_error`.
But Effect is a little more than just the IO monad, since Effects make
available the intent as *transparent data*. By transparent, I specifically
def get_request(method, url, **kwargs):
"""Return a Request wrapped in an Effect."""
return Effect(Request(method=method, url=url, **kwargs))
def request_with_reauth(get_request, method, url, auth=None,
headers=None, **kwargs):
"""Create a request which will reauthenticate upon 401 and retry."""
def handle_reauth(result, retries):
@radix
radix / wrapper.py
Created June 11, 2014 17:55
wrapper combinator
def wrap(f, g):
"""
'The wrapper combinator'
Given f, any function, and g, a function which accepts a callable as its
first argument, return a function:
λ(*args, **kwargs): g(f, *args, **kwargs)
This allows g to "wrap" f, so that g is responsible for calling f. f must expect a
radix@伝説:~$ python3
Error processing line 1 of /Users/radix/Library/Python/2.7/lib/python/site-packages/virtualenvwrapper-4.2-py2.7-nspkg.pth:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site.py", line 167, in addpackage
exec(line)
File "<string>", line 1, in <module>
ImportError: No module named 'new'
Remainder of file ignored
@radix
radix / immutablebag.py
Last active August 29, 2015 14:06
immutablebag.py
"""
An immutable bag/multiset implementation for Python.
"""
import six
if six.PY2:
from collections import Container, Iterable, Sized, Hashable
else:
from collections.abc import Container, Iterable, Sized, Hashable
@radix
radix / converge_lb_state.py
Created October 20, 2014 23:24
converge lb state
def _converge_lb_state(desired_lb_state, current_lb_state, ip_address):
desired = {
(lb_id, config.port): config
for lb_id, configs in desired_lb_state.items()
for config in configs}
current = {
(node.lb_id, node.config.port): node
for node in current_lb_state}
desired_idports = set(desired)
current_idports = set(current)
@radix
radix / foolin.py
Created December 20, 2014 18:41
Example of using an Effect to add scope to nested effects
from __future__ import print_function
from characteristic import attributes
from effect import Effect, perform, ConstantIntent, base_dispatcher, sync_performer
from effect.dispatcher import TypeDispatcher, ComposedDispatcher
@attributes(['name'])
class MyThing(object):
"""Intent."""
@radix
radix / gist:50da5f27404c22ee01f6
Created January 7, 2015 17:40
functions under test
def atom(a):
if a == 0: return -1
else: return a
def composition(a):
return ('composed', atom(a))
@radix
radix / wtf-tracebacks.py
Last active August 29, 2015 14:14
WTF, Tracebacks.
"""
Re-raising with tracebacks in Python is confusing.
First, I was surprised that when you re-raise with an explicitly specified
traceback, you don't get that exact traceback from sys.exc_info() after
catching it. Then I realized Python adds more information upon
re-raising. Okay, that's great!
But then I noticed that the re-raising frame is left out of the traceback!
That's *super* confusing! Note how in the "new" traceback, there's a frame in
@radix
radix / pyr-record-benchmark.py
Created February 4, 2015 20:26
Pyrsistent PRecord vs POPO copy+mutate
from __future__ import print_function
import copy
from pyrsistent import PRecord, field
NUM_FIELDS = 20
def make_pfoo(attr_names):