Skip to content

Instantly share code, notes, and snippets.

View dutc's full-sized avatar

James Powell dutc

View GitHub Profile
@dutc
dutc / memoise.py
Created June 21, 2013 00:08
How short & how complete can we write a memoising decorator? I think the below is more complete than most formulations (which don't cache independent of arg-binding.) I think it also cuts straight to the core of what memoisation means. Three obvious deficiencies: [1] What do we do about instance and class methods? These can be meaningfully memoi…
from sys import version_info
assert version_info.major == 3 and version_info.minor >= 3, \
'requires PEP 362; Python 3.3 or later; python.org/dev/peps/pep-0362/'
from inspect import signature
class memoise(dict):
def __init__(self, func):
self.func, self.signature = func, signature(func)
def __missing__(self, key):
args, kwargs = key
@dutc
dutc / static-qualifier.c
Created June 25, 2013 17:19
gcc -std=c99 -Wall -pedantic -o /dev/null static-qualifier.c
#include <stdlib.h>
#include <stdio.h>
void f(int x[static 3]);
void f(int x[static 3]) {
printf("%d + %d + %d = %d\n", x[0], x[1], x[2], x[0] + x[1] + x[2]);
}
int main(int argc, char* argv[]) {
@dutc
dutc / mersenne.py
Created July 19, 2013 03:46
mersenne twister pseudo-random number generator
from __future__ import division
# ref: en.wikipedia.org/wiki/Mersenne_twister
def mersenne(seed = 1, period=397, length=624):
state, tm = [seed & 0xffffffff], lambda op, x: x ^ op(x)
for i in xrange(1,length):
state.append((0x6c078965 * (state[-1] ^ (state[-1] >> 30)) + i) & 0xffffffff)
while True:
for i in xrange(length):
y = (state[i] & 0x80000000) + (state[(i+1)%length] & 0x7fffffff)
@dutc
dutc / notes.md
Last active July 1, 2022 20:57
CPython workshop

themes

  1. CPython for greater understanding of the Python programming language (but "reference implementations always overspecify") Reading source to solve problems
  2. getting involved, contributing to the project

introduction

This workshop will cover the basics of the CPython runtime and interpreter. There is an enormous amount of material to cover, and I'll try to to rush through as much as I can.

diff -r 1e79ca2bc494 Doc/reference/compound_stmts.rst
--- a/Doc/reference/compound_stmts.rst Wed Nov 20 11:53:31 2013 -0800
+++ b/Doc/reference/compound_stmts.rst Wed Nov 20 20:23:32 2013 -0500
@@ -442,7 +442,7 @@
.. productionlist::
funcdef: [`decorators`] "def" `funcname` "(" [`parameter_list`] ")" ["->" `expression`] ":" `suite`
decorators: `decorator`+
- decorator: "@" `dotted_name` ["(" [`parameter_list` [","]] ")"] NEWLINE
+ decorator: "@" `expression` NEWLINE
dotted_name: `identifier` ("." `identifier`)*
@dutc
dutc / somecode.py
Created November 27, 2013 17:08
Making a mockery of open!
def test(filename, message):
with open(filename, 'w') as f:
f.write(message)
@dutc
dutc / 0-question
Last active July 1, 2022 20:57
larger principles of CPython debugging
> I'm able to follow most of your instructions in the tutorial. However,
> there are things that are not yet obvious to me. If, for instance, I
> want to find the implementation of a specific built-in, where do I go?
> The `dis()` decompilation of `id()`, which I want to study, isn't
> instructive. Using `find ... -print0 | xargs -0 grep ...` also doesn't
> seem to get me anything useful. But I don't even see "builtins" file or
> a section of http://docs.python.org/3.3/c-api/index.html dealing with
> built-ins at all. Where should I be looking; how should I generalize
> this type of search for the future?
@dutc
dutc / restricted-mode.py
Last active August 29, 2015 13:56
example of triggering restricted mode by touching __builtins__ (run this in the repl)
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__')
#!/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
#!/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)