Browse code
...
Showing 8 changed files
- pythonsnippets1_0002.py
- pythonsnippets_0009.py
- pythonsnippets_0020.py
- pythonsnippets_0034.py
- pythonsnippets_0037.py
- pythonsnippets_0044.py
- pythonsnippets_0046.py
- pythonsnippets_0048.py
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,16 +0,0 @@ |
1 |
-import functools |
|
2 |
-class piecewise_function(object): |
|
3 |
- def __init__(self, fallback): |
|
4 |
- self._conds = [] |
|
5 |
- self._fallback = fallback |
|
6 |
-# self.__call__ = functools.wrap(fallback)(self.__call__) |
|
7 |
- def add_piece(self, cond): |
|
8 |
- def _inner(f): |
|
9 |
- self._conds.append((cond,f)) |
|
10 |
- return self |
|
11 |
- return _inner |
|
12 |
- def __call__(self, *args, **kwargs): |
|
13 |
- result = self._fallback |
|
14 |
- for cond, func in self._conds: |
|
15 |
- if cond(*args, **kwargs): result = func |
|
16 |
- return result(*args, **kwargs) |
... | ... |
@@ -2,6 +2,6 @@ from itertools import*;import sys;j=str.join;c=sys.stdin.read();print j('\n',map |
2 | 2 |
|
3 | 3 |
from itertools import*;import sys;j=str.join;c=sys.stdin.read();print j('\n',[j(' ',[b,j('',islice(repeat('*'),len([x for x in c if x==b])))])for b in map(chr,range(97,122))]) |
4 | 4 |
|
5 |
-from itertools import*;import sys;j=str.join;c=sys.stdin.read();print j('\n',[j(' ',[b,j('','*'*c.count(b))])for b in map(chr,range(65,123))]) |
|
5 |
+import sys;j=str.join;c=sys.stdin.read();print j('\n',[j(' ',[b,j('','*'*c.count(b))])for b in map(chr,range(65,123))]) |
|
6 | 6 |
|
7 |
-module Main where import Data.Char;main=getContents>>=(\b->putStrLn$(map chr[65..122]>>=(\c->c:' ':(take(length$filter(\x->x==c)b)$repeat '*')++"\n"))) |
|
8 | 7 |
\ No newline at end of file |
8 |
+module Main where import Data.Char;main=getContents>>=(\b->putStrLn$(map chr[65..122]>>=(\c->c:' ':(take(length$filter(\x->x==c)b)$repeat '*')++"\n"))) |
8 | 0 |
deleted file mode 100644 |
... | ... |
@@ -1,21 +0,0 @@ |
1 |
-class LazyLoad(object): |
|
2 |
- def __init__(self, __func_, *args, **kwargs): |
|
3 |
- self.__func = __func_ |
|
4 |
- self.__args = list(args) |
|
5 |
- self.__kwargs = kwargs |
|
6 |
- def __get__(self, inst, cls): |
|
7 |
- tb = cls.__dict__ |
|
8 |
- nm = [x for x in tb.keys() if id(tb[x]) == id(self)][0] |
|
9 |
- setattr(inst, nm, self.__func(*self.__args, **self.__kwargs)) |
|
10 |
- return getattr(inst, nm) |
|
11 |
- |
|
12 |
-def func(a,b,c): |
|
13 |
- print a,b,c |
|
14 |
- return a+b,c |
|
15 |
- |
|
16 |
-class a(object): |
|
17 |
- b = LazyLoad(func, 1,2,3) |
|
18 |
- c = LazyLoad(func, 2,3,4) |
|
19 |
- d = LazyLoad(func, 3,4,5) |
|
20 |
- __e = LazyLoad(func, 4,5,6) |
|
21 |
- def __str__(self): return str(self.__e) |
|
22 | 0 |
\ No newline at end of file |
... | ... |
@@ -23,7 +23,7 @@ class IterDict(dict): |
23 | 23 |
def __delitem__(self, name): |
24 | 24 |
if name in self.keylist: |
25 | 25 |
del self.keylist[self.keylist.index(name)] |
26 |
- dict.__delitem__(self, name) |
|
26 |
+ dict.__delitem__(self, name) |
|
27 | 27 |
|
28 | 28 |
|
29 | 29 |
import itertools |
... | ... |
@@ -45,11 +45,11 @@ class PIDmanager(object): |
45 | 45 |
def put_task(self, task): |
46 | 46 |
pid = self.pid_generator.next() |
47 | 47 |
self.queue[pid] = task |
48 |
- print pid |
|
48 |
+ print pid, |
|
49 | 49 |
if self.tasks == None: |
50 |
- self.queue.reset() |
|
50 |
+ self.queue.reset() |
|
51 | 51 |
self.tasks = self.queue.keylistiter |
52 |
- self.tasks.next() |
|
52 |
+ self.tasks.next() |
|
53 | 53 |
@classmethod |
54 | 54 |
def run_task(self): |
55 | 55 |
if self.tasks != None: |
... | ... |
@@ -57,20 +57,20 @@ class PIDmanager(object): |
57 | 57 |
task = self.queue[pid] |
58 | 58 |
if hasattr(task, '__iter__') and isinstance(task[0], CONT): |
59 | 59 |
_, func, args, kwargs, cleanup = task |
60 |
- result = [] |
|
60 |
+ result = [] |
|
61 | 61 |
self.queue[pid] = func(*args, **kwargs) |
62 | 62 |
cleanup() |
63 |
- return self.queue[pid] |
|
63 |
+ return self.queue[pid] |
|
64 | 64 |
else: |
65 | 65 |
del self.queue[pid] |
66 |
- return task |
|
67 |
- @classmethod |
|
68 |
- def run_all(self): |
|
69 |
- try: |
|
70 |
- while 1: self.run_task() |
|
71 |
- except StopIteration: |
|
72 |
- self.queue.reset() |
|
73 |
- self.tasks == None |
|
66 |
+ return task |
|
67 |
+ @classmethod |
|
68 |
+ def run_all(self): |
|
69 |
+ try: |
|
70 |
+ while 1: self.run_task() |
|
71 |
+ except StopIteration: |
|
72 |
+ self.queue.reset() |
|
73 |
+ self.tasks == None |
|
74 | 74 |
|
75 | 75 |
|
76 | 76 |
def print_many(gen, total=2000, count=0): |
... | ... |
@@ -79,9 +79,9 @@ def print_many(gen, total=2000, count=0): |
79 | 79 |
return CONT(), print_many, (gen,), dict(total=total, count=count+1), cleanup |
80 | 80 |
|
81 | 81 |
|
82 |
-def cleanup(): print |
|
82 |
+def cleanup(): print '', |
|
83 | 83 |
|
84 | 84 |
|
85 | 85 |
PIDmanager.put_task( (CONT(), print_many, (itertools.cycle('a'),), dict(total=10), cleanup) ) |
86 | 86 |
PIDmanager.put_task( (CONT(), print_many, (itertools.cycle('b'),), dict(total=10), cleanup) ) |
87 |
-PIDmanager.run_task() |
|
88 | 87 |
\ No newline at end of file |
88 |
+PIDmanager.run_task() |
... | ... |
@@ -1,8 +1,8 @@ |
1 |
- def process_chunk(self, chunk): |
|
2 |
- buf = chunk.split(self.__delim) |
|
3 |
- out = [] |
|
4 |
- while buf: |
|
5 |
- line = buf.pop(0) |
|
6 |
- if self.__re.search(line): |
|
7 |
- out.append(line) |
|
8 |
- return self.__delim.join(out) |
|
9 | 1 |
\ No newline at end of file |
2 |
+ def process_chunk(self, chunk): |
|
3 |
+ buf = chunk.split(self.__delim) |
|
4 |
+ out = [] |
|
5 |
+ while buf: |
|
6 |
+ line = buf.pop(0) |
|
7 |
+ if self.__re.search(line): |
|
8 |
+ out.append(line) |
|
9 |
+ return self.__delim.join(out) |
10 | 10 |
deleted file mode 100644 |
... | ... |
@@ -1,54 +0,0 @@ |
1 |
-import functools |
|
2 |
- |
|
3 |
-def memoize(func): |
|
4 |
- _cache = {} |
|
5 |
- class NULL:pass |
|
6 |
- NULL = NULL() |
|
7 |
- @functools.wraps(func) |
|
8 |
- def _inner(*args, **kwargs): |
|
9 |
- _kwargs = tuple( |
|
10 |
- (k, tuple(v) if hasattr(v, '__iter__') else v) for k,v in kwargs |
|
11 |
- ) |
|
12 |
- value = _cache.get((args, _kwargs), NULL) |
|
13 |
- if value is NULL: |
|
14 |
- value = func(*args, **kwargs) |
|
15 |
- _cache[(args,_kwargs)] = value |
|
16 |
- return value |
|
17 |
- _inner.__enter__ = lambda *_: _inner |
|
18 |
- _inner.__exit__ = lambda *_: _cache.clear() |
|
19 |
- _inner.reset = _cache.clear |
|
20 |
- _inner.cache = _cache |
|
21 |
- _inner.orig = func |
|
22 |
- return _inner |
|
23 |
- |
|
24 |
-class return_if(object): |
|
25 |
- def __init__(self, map): |
|
26 |
- self._map = dict( |
|
27 |
- (tuple(k) if hasattr(k, '__iter__') else (k,),v) for k,v in map.iteritems() |
|
28 |
- ) |
|
29 |
- self._func = lambda x:x |
|
30 |
- def _inner(self, *args): |
|
31 |
- if args in self._map: return self._map[args] |
|
32 |
- else: return self._func(*args) |
|
33 |
- def __call__(self, func): |
|
34 |
- self._func = func |
|
35 |
- return self._inner |
|
36 |
- |
|
37 |
- |
|
38 |
-@memoize |
|
39 |
-@return_if({1: 1}) |
|
40 |
-def fact(n): |
|
41 |
- return n*fact(n-1) |
|
42 |
- |
|
43 |
- |
|
44 |
-@return_if({(1,1): 2, (2,2): 3, (3,3):4}) |
|
45 |
-def sum(a,b): |
|
46 |
- return a+b |
|
47 |
- |
|
48 |
- |
|
49 |
-@memoize |
|
50 |
-@return_if({1: 1,2: 1}) |
|
51 |
-def fib(n): |
|
52 |
- if n < 1: raise ValueError, "not n >= 1" |
|
53 |
- else: |
|
54 |
- return fib(n-1) + fib(n-2) |
|
55 | 0 |
\ No newline at end of file |
56 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,50 +0,0 @@ |
1 |
- |
|
2 |
-def f(h): |
|
3 |
- def g(n): |
|
4 |
- if n <= 1: return 1 |
|
5 |
- else: return n*h(n-1) |
|
6 |
- |
|
7 |
-#def e(h): |
|
8 |
-# def |
|
9 |
- |
|
10 |
-def Y(f): |
|
11 |
- def g(h): |
|
12 |
- def i(x): |
|
13 |
- return f(h(h))(x) |
|
14 |
- return i |
|
15 |
- return g(g) |
|
16 |
- |
|
17 |
- |
|
18 |
-@Y |
|
19 |
-def fib(f): |
|
20 |
- def _inner(n): |
|
21 |
- if n in range(2): return 1 |
|
22 |
- else: return f(n-1) + f(n-2) |
|
23 |
- return _inner |
|
24 |
- |
|
25 |
- |
|
26 |
-import functools |
|
27 |
- |
|
28 |
-def bounce(func): |
|
29 |
- @functools.wraps(func) |
|
30 |
- def _inner(arg): |
|
31 |
- result = func(arg) |
|
32 |
- return result(fact) |
|
33 |
- return _inner |
|
34 |
- |
|
35 |
-class return_if(object): |
|
36 |
- def __init__(self, map): |
|
37 |
- self._map = dict( |
|
38 |
- (tuple(k) if hasattr(k, '__iter__') else (k,),v) for k,v in map.iteritems() |
|
39 |
- ) |
|
40 |
- self._func = lambda x:x |
|
41 |
- def _inner(self, *args): |
|
42 |
- if args in self._map: return self._map[args] |
|
43 |
- else: return self._func(*args) |
|
44 |
- def __call__(self, func): |
|
45 |
- self._func = func |
|
46 |
- return self._inner |
|
47 |
-@bounce |
|
48 |
-@return_if({1: lambda _:1}) |
|
49 |
-def fact(n): |
|
50 |
- return lambda x: n*x(n-1) |