This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def atom(a): | |
| if a == 0: return -1 | |
| else: return a | |
| def composition(a): | |
| return ('composed', atom(a)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import copy | |
| from pyrsistent import PRecord, field | |
| NUM_FIELDS = 20 | |
| def make_pfoo(attr_names): |