Skip to content

Instantly share code, notes, and snippets.

View akaptur's full-sized avatar

Allison Kaptur akaptur

View GitHub Profile
@akaptur
akaptur / gist:9137899
Created February 21, 2014 16:45
Class variables vs. instance variables in python
>>> class Test(object):
... var = 7
... def __init__(self):
... self.ivar = 2
...
>>> t = Test()
>>> s = Test()
>>> t.__dict__
{'ivar': 2}
>>> t.var
@akaptur
akaptur / gist:6ca2347bf250bfd4f87a
Created July 25, 2014 19:59
error message from generator bug in byterun
FAIL: test_generator_from_generator2 (tests.test_functions.TestGenerators)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/afk/Dropbox/python/byterun_env/byterun/tests/test_functions.py", line 239, in test_generator_from_generator2
""")
File "/Users/afk/Dropbox/python/byterun_env/byterun/tests/vmtest.py", line 86, in assert_ok
self.assert_same_exception(vm_exc, py_exc)
File "/Users/afk/Dropbox/python/byterun_env/byterun/tests/vmtest.py", line 96, in assert_same_exception
self.assertEqual(str(e1), str(e2))
nose.proxy.AssertionError: AssertionError: "unsupported operand type(s) for *: 'NoneType' and 'NoneType'" != 'None'
@akaptur
akaptur / collections.py
Created July 30, 2014 14:01
Named tuples from Python's Lib/collections.py
################################################################################
### namedtuple
################################################################################
_class_template = '''\
class {typename}(tuple):
'{typename}({arg_list})'
__slots__ = ()
@akaptur
akaptur / gist:bb7981515824b79c96e3
Created September 10, 2014 18:48
bash caching, viewed with `type`
byterun_env ⚲ type python
python is /usr/local/bin/python
byterun_env ⚲ source bin/activate
(byterun_env)byterun_env ⚲ type python
python is /Users/afk/Dropbox/python/byterun_env/bin/python
(byterun_env)byterun_env ⚲ python
Python 2.7.6 (default, Feb 11 2014, 18:46:41)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
def write_chained_functions(total_functions):
with open('/usr/share/dict/words','r') as name_file:
fn_names = [line.strip() for line in name_file.readlines()]
first_name = fn_names[0]
last_name = fn_names[total_functions]
fn_names = iter(fn_names)
with open("empty_chain.py", 'w') as f:
name = next(fn_names)
following_name = next(fn_names)
for _ in range(total_functions):
def write_block(names, next_name, f):
for depth, func_name in enumerate(names):
f.write("\n" + " " * depth + "def %s():" % func_name)
f.write("\n" + " " * (depth + 1) + "%s()" % next_name)
for depth, func_name in reversed(list(enumerate(names[1:]))):
f.write("\n" + " " * (depth + 1) + "%s()" % func_name)
f.write("\n") # for readability ;)
@akaptur
akaptur / gist:be988fc5cc3ff41f66c3
Created September 29, 2014 19:17
full recursion limit checker
int
_Py_CheckRecursiveCall(char *where)
{
PyThreadState *tstate = PyThreadState_GET();
#ifdef USE_STACKCHECK
if (PyOS_CheckStack()) {
--tstate->recursion_depth;
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
return -1;
@akaptur
akaptur / frame_counter.py
Created September 29, 2014 19:21
frame counter
def depth_one(n):
def depth_two(n):
def countdown(n, times_called=[0]):
times_called[0] += 1
if n == 0:
return times_called
else:
try:
return countdown(n-1)
except RuntimeError:
test ⚲ ./testopt
aflag = 0, bflag = 0, cvalue = (null)
test ⚲ ./testopt -a -b
aflag = 1, bflag = 1, cvalue = (null)
test ⚲ ./testopt -c
Option -c requires an argument.
test ⚲ ./testopt -ab
aflag = 1, bflag = 1, cvalue = (null)
test ⚲ ./testopt -c foo
aflag = 0, bflag = 0, cvalue = foo
def countdown(n, times_called=[0]):
print times_called[0], " ", n, " "
times_called[0] += 1
if n == 1:
return times_called
else:
try:
return countdown(n-1)
except RuntimeError: