Skip to content

Instantly share code, notes, and snippets.

Allison Kaptur akaptur

Block or report user

Report or block akaptur

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View sys.settrace example
>>> import sys
>>> def stuff():
... print("calling stuff!")
...
>>> def printer(frame, event, arg):
... print(frame, event, arg)
... return printer # return itself to keep tracing
...
>>> sys.settrace(printer) # register the tracing function
>>> stuff()
View spinner2.py
def countdown(n, times_called=[0],):
print(times_called[0], " ", n, " ")
# print "frame: %s" % id(inspect.currentframe())
times_called[0] += 1
if n == 1:
return times_called
else:
try:
return countdown(n-1)
except RuntimeError:
View spinner.py
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:
View gist:fd24335d81328bbd8de8
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
View frame_counter.py
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:
@akaptur
akaptur / gist:be988fc5cc3ff41f66c3
Created Sep 29, 2014
full recursion limit checker
View gist:be988fc5cc3ff41f66c3
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 / gist:d8348c21e93d61ec9397
Created Sep 29, 2014
recursion limit checker
View gist:d8348c21e93d61ec9397
# 2.7
if (tstate->recursion_depth > recursion_limit) {
--tstate->recursion_depth;
PyErr_Format(PyExc_RuntimeError,
"maximum recursion depth exceeded%s",
where);
return -1;
# 3.4
View nested_writer.py
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 ;)
View chained_writer.py
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):
@akaptur
akaptur / gist:bb7981515824b79c96e3
Created Sep 10, 2014
bash caching, viewed with `type`
View gist:bb7981515824b79c96e3
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.
>>>
You can’t perform that action at this time.