Skip to content

Instantly share code, notes, and snippets.

View sirpengi's full-sized avatar

shu.chen sirpengi

View GitHub Profile
[shuchen@archy stuff]$ python2.7 a.py
module A being imported!
module B being imported!
module A being imported!
hollering from A
hollering from A
import string
import sys
class count_dict(dict):
"""
stub object used to simplify tracking
the number of letters encountered.
this expects to be initialized with an uppercase letter yo.
"""
(py27)[shuchen@archy Downloads]$ cat test.py
from docutils.core import publish_string
import pstats, cProfile
fn = 'foo.rst'
profile_fn = fn + '.prof'
with open(fn) as fh:
data = fh.read()
def gogogo():
>>> a = lambda x: 1000
>>> a
<function <lambda> at 0xb77f88b4>
>>> a(1)
1000
>>> (id(a(1)), id(a(1)))
(143414124, 143414124)
>>> (id(a(1)), id(a(2)))
(143414124, 143414124)
>>> def return_1000(): return 1000
@sirpengi
sirpengi / 17x17_yes.txt
Created February 14, 2012 00:17
N-colorable grid checker
22133421413223443
24211233443411322
31344411143241232
41231323412144134
31112324234443221
13324314412212323
31424223321414413
43121113244323412
44334242312321211
12221344143334241
2011-10-15 05:05:59 dxq hi friends
2011-10-15 16:51:41 dxq hi friends
2011-10-26 04:26:09 dxq hi friends
2011-10-28 04:40:37 dxq hi friends
2011-10-29 03:47:19 dxq hi friends
2011-11-02 19:44:43 sixthgear hi friends
2011-11-04 02:44:14 dxq hi friends
2011-11-08 02:47:33 dxq hi friends
2011-11-08 23:50:36 dxq hi friends
2011-11-11 00:56:49 dxq hi friends
@sirpengi
sirpengi / decorateeverything.py
Created March 23, 2012 02:26
decorate everything!
def DecorateEverything(decorator):
"""Function that returns a metaclass that decorates
all class callables (except those starting with __)
with `decorator`."""
class DEMeta(type):
def __init__(cls, name, bases, dct):
new_dct = {}
for k, v in dct.iteritems():
if hasattr(v, "__call__") and not k.startswith('__'):
setattr(cls, k, decorator(v))
def one(iterable, condition=None):
"""True if only one value in the iterable evaluates to True."""
if condition is None:
condition = bool
gen = (i for i in iterable if condition(i))
try:
gen.next()
except StopIteration:
return False
try:
@sirpengi
sirpengi / output
Created June 16, 2012 05:46
mah output
4 5
5
0
6
8
1
1
0
0
0
print "\n".join(map(lambda x:((str(x[0]) if x[1]%5 else '') if (x[0]%3) else 'fizz')+('' if (x[1]%5) else 'buzz'),zip(range(1,1001),range(1,1001))))