Skip to content

Instantly share code, notes, and snippets.

def example():
import sys
from twisted.python import log
log.startLogging(sys.stdout)
logger = EventHierarchyLogger("myapp", log)
logger.msg("startup")
# in another module...
logger = logger.specialize("rpc")
logger.msg("got-frag", fragger="dreid", fragee="radix")
@radix
radix / coollog.py
Created June 15, 2013 06:15
coollog.py
import sys
class Logger(object):
def __init__(self, system):
self.system = system
def log(self, format=None, **keywords):
@radix
radix / gist:6071626
Created July 24, 2013 15:29
inlineCallbacks tracebacks
>>> from twisted.internet.defer import inlineCallbacks
>>> @inlineCallbacks
... def a():
... yield b()
...
>>> @inlineCallbacks
... def b():
... yield c()
...
>>> def c(): 1 / 0
@radix
radix / gist:6071653
Created July 24, 2013 15:31
basic generator tracebacks
>>> def g1():
... for x in g2():
... yield x
...
>>> def g2():
... yield 1
... yield 2
... 1 / 0
...
>>> for x in g1(): pass
@radix
radix / ersid benchmark tool
Created October 10, 2013 23:37
benchmark tool for ersid using treq
from __future__ import print_function
import sys
import time
from twisted.internet.task import cooperate, react, Cooperator
from twisted.internet.defer import gatherResults
import treq
@radix
radix / immutable.py
Last active January 4, 2016 06:39
Immutable metaclass
from collections import namedtuple
def immutable(fields):
"""
Returns a metaclass that makes fields on your instances immutable, while
still allowing __init__ to initialize attributes.
You should be able to enable immutability on most normal classes by just
adding
@radix
radix / stupid_immutable_dict.py
Last active January 4, 2016 07:09
Stupid Immutable Dict
def imdict(dct):
"""Construct an immutable dict, based on a normal one.
"""
# Warning: It's possible for others to get at these closed-over variables
# by inspecting frames, and then modifying them. Suggestions welcome (other
# than implementing a new hash table with tuples from scratch)
items = dct.items()
lookup = dict([(item[0], index) for index, item in enumerate(items)])
radix@伝説.local:~$ mkvirtualenv cryptotest
New python executable in cryptotest/bin/python
Installing setuptools, pip...done.
(cryptotest)
radix@伝説.local:~$ cd ~
(cryptotest)
radix@伝説.local:~$ python t.py
Traceback (most recent call last):
File "t.py", line 1, in <module>
from cryptography.hazmat.backends import openssl, commoncrypto, default_backend
@radix
radix / keybase.md
Created June 1, 2014 17:32
keybase.md

Keybase proof

I hereby claim:

  • I am radix on github.
  • I am radix (https://keybase.io/radix) on keybase.
  • I have a public key whose fingerprint is FCBD FCAC C60C 0542 8B25 2B52 50E2 9207 87F0 E626

To claim this, I am signing this object:

@radix
radix / idris-bang-notation
Created June 9, 2014 16:52
idris bang notation
hello : { [STDIO] } Eff IO ()
hello = do
putStr "Name? "
putStrLn ("Hello " ++ trim !getStr ++ "!")