This file contains 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 f(x): | |
print 'func_code is a restricted attribute', x.func_code | |
f(f) # this works | |
# overwrite __builtins__ | |
old_builtins = __builtins__ | |
new_builtins = type('module', (type(__builtins__),), {})('__builtins__') |
This file contains 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
#!/usr/bin/env python3 | |
# using inspect | |
from inspect import currentframe, getframeinfo | |
class Foo: | |
def __init__(self, quux): | |
self.quux = quux | |
def bar(self): | |
return getframeinfo(currentframe()).function, self.quux |
This file contains 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
#!/usr/bin/env python3 | |
class TaggedMethods(type): | |
def __new__(cls, name, bases, body): | |
tags = {} | |
for name, attr in ((k,v) for k,v in body.items() if isinstance(v, Tag)): | |
tags.setdefault(attr.tag, []).append(name) | |
body[name] = attr.func | |
body['methods_by_tag'] = property(lambda s: {k:[getattr(s,a) for a in v] for k,v in tags.items()}) | |
return type.__new__(cls, name, bases, body) |
This file contains 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
# only works for kwargs | |
class autoinit(type): | |
def __call__(self, *args, **kwargs): | |
rv = super().__call__(*args, **kwargs) | |
for k,v in rv.__init__.__annotations__.items(): | |
setattr(rv, k, eval(v, kwargs)) | |
return rv | |
class Foo(metaclass=autoinit): | |
def __init__(self, x: 'x + y', y: 'y * 10', z): |
This file contains 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
cut -d ';' -f 2- ~/.zsh_history | \ | |
tr '|;' '\n' | \ | |
FS=' ' awk '{ print $1 }' | \ | |
cut -d '/' -f -1 | \ | |
awk '/^[a-zA-Z]/ { cmds[$0] += 1 } END { for (cmd in cmds) print cmd, cmds[cmd] }' | \ | |
awk '{ first = tolower(substr($1,1,1)); count = int($2); if (max[first] < count) { max[first] = count; cmds[first] = $1 } } END { for (f in cmds) print f, cmds[f], max[f] }' | \ | |
sort |
This file contains 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 functools import update_wrapper | |
from collections import Callable | |
from logging import info, basicConfig, INFO | |
basicConfig(level=INFO) | |
def dec(name, obj, meth): | |
def wrapped(*args, **kwargs): | |
rv = meth(*args, **kwargs) | |
info('%r:0x%x:%s.%r(*%r, **%r) -> %r', obj, id(obj), name, meth, args, kwargs, rv) |
This file contains 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 collections import deque | |
class CircularBuffer(deque): | |
def __init__(self, *args, **kwargs): | |
self.size = kwargs.pop('size', 3) | |
super(CircularBuffer, self).__init__(*args, **kwargs) | |
def append(self, item): | |
while len(self) >= self.size: | |
self.popleft() | |
super(CircularBuffer, self).append(item) |
This file contains 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 cumsum(xs): | |
xs = iter(xs) | |
rv = [next(xs)] | |
for x in xs: | |
rv.append(rv[-1] + x) | |
return rv | |
def cumsum(xs): | |
tot = 0 | |
for x in xs: |
This file contains 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
class BaseMeta(type): | |
def __new__(cls, name, bases, dict): | |
if 'foo' not in dict: | |
raise RuntimeError('class must have its own foo method!') | |
return type.__new__(cls, name, bases, dict) | |
class Base(metaclass=BaseMeta): | |
def foo(self): pass | |
class Derived(Base): |
This file contains 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
class FoobarMeta(type): | |
def __instancecheck__(self, obj): | |
return hasattr(obj, 'foo') and hasattr(obj, 'bar') | |
class Foobar(metaclass=FoobarMeta): | |
pass | |
class A: | |
def foo(self): | |
pass |
OlderNewer