Showing 8 changed files
- dupkeydict.py
- put_str.py
- pydit
- unittest_skeleton.py
- unsorted/pythonsnippets_rds_UE.py
- unsorted/pythonsnippets_test.py
- unsorted/pythonsnippets_y0cblF
- unsorted/pythonsnippets_zPnrM5
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,63 @@ |
1 |
+import collections |
|
2 |
+ |
|
3 |
+class dkeydict(collections.Mapping): |
|
4 |
+ def __init__(self, items=None): |
|
5 |
+ #: caching only |
|
6 |
+ self.dict = {} |
|
7 |
+ |
|
8 |
+ items = items or [] |
|
9 |
+ self.list = items.items() if hasattr(items, 'items') else items |
|
10 |
+ def __iter__(self): return iter(self.list) |
|
11 |
+ def __len__(self): return len(self.list) |
|
12 |
+ |
|
13 |
+ def __getitem__(self, key): |
|
14 |
+ result = [] |
|
15 |
+ |
|
16 |
+ #: which item to get |
|
17 |
+ count = 0 |
|
18 |
+ counter = 0 |
|
19 |
+ |
|
20 |
+ #: how many? |
|
21 |
+ num = 1 |
|
22 |
+ |
|
23 |
+ #: which ones to get? |
|
24 |
+ skip = 0 |
|
25 |
+ idx = 0 |
|
26 |
+ |
|
27 |
+ if isinstance(key, tuple) and 1 < len(key) < 5: |
|
28 |
+ if len(key) == 2: |
|
29 |
+ key, count = key |
|
30 |
+ elif len(key) == 3: |
|
31 |
+ key, count, num = key |
|
32 |
+ else: |
|
33 |
+ key, count, num, skip = key |
|
34 |
+ |
|
35 |
+ capturing = False |
|
36 |
+ for k,v in reversed(self.list): |
|
37 |
+ if k == key: |
|
38 |
+ if len(result) == num: break |
|
39 |
+ |
|
40 |
+ if counter >= count: capturing = True |
|
41 |
+ counter += 1 |
|
42 |
+ |
|
43 |
+ if idx != skip: |
|
44 |
+ capturing = False |
|
45 |
+ idx += 1 |
|
46 |
+ else: |
|
47 |
+ capturing = True |
|
48 |
+ idx = 0 |
|
49 |
+ |
|
50 |
+ if capturing: result.append(v) |
|
51 |
+ |
|
52 |
+ |
|
53 |
+ |
|
54 |
+ elif isinstance(key, slice): |
|
55 |
+ return self.list[slice] |
|
56 |
+ else: |
|
57 |
+ if key in self.dict: return self.dict[key] |
|
58 |
+ else: |
|
59 |
+ result = reversed([v for k,v in self.list if k == key]).next() |
|
60 |
+ self.dict[key] = result |
|
61 |
+ |
|
62 |
+ return result |
|
63 |
+ |
5 | 5 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,71 @@ |
1 |
+import code |
|
2 |
+import StringIO |
|
3 |
+ |
|
4 |
+oraw_input = raw_input |
|
5 |
+class Tee(object): |
|
6 |
+ __buffer = StringIO.StringIO() |
|
7 |
+ @classmethod |
|
8 |
+ def raw_input(cls, *a, **kw): |
|
9 |
+ result = oraw_input(*a, **kw) |
|
10 |
+ print >>cls.__buffer, result |
|
11 |
+ return result |
|
12 |
+ |
|
13 |
+ def __init__(self, fil_): |
|
14 |
+ self.fil_ = fil_ |
|
15 |
+ |
|
16 |
+ def __getattr__(self, name): |
|
17 |
+ return getattr(self.fil_, name) |
|
18 |
+ |
|
19 |
+ def read(self, *a): |
|
20 |
+ result = self.fil_.read(*a) |
|
21 |
+ print 'hello!!!' |
|
22 |
+ print >>sys.stdout.fil_, result |
|
23 |
+ self.__buffer.write(result) |
|
24 |
+ return result |
|
25 |
+ |
|
26 |
+ def write(self, data): |
|
27 |
+ self.__buffer.write(data) |
|
28 |
+ self.fil_.write(data) |
|
29 |
+ |
|
30 |
+ def flush(self): |
|
31 |
+ self.__buffer.flush() |
|
32 |
+ self.fil_.flush() |
|
33 |
+ |
|
34 |
+ def get_data(self): |
|
35 |
+ self.__buffer.seek(0,0) |
|
36 |
+ return self.__buffer.read() |
|
37 |
+ |
|
38 |
+__builtins__.raw_input = Tee.raw_input |
|
39 |
+import sys |
|
40 |
+stdin = sys.stdin = Tee(sys.stdin) |
|
41 |
+stdout = sys.stdout = Tee(sys.stdout) |
|
42 |
+stderr = sys.stderr = Tee(sys.stderr) |
|
43 |
+ |
|
44 |
+ |
|
45 |
+try: |
|
46 |
+ a = code.InteractiveConsole() |
|
47 |
+ a.interact() |
|
48 |
+except BaseException: |
|
49 |
+ sys.stderr = sys.stderr.fil_ |
|
50 |
+ raise |
|
51 |
+else: |
|
52 |
+ sys.stderr = sys.stderr.fil_ |
|
53 |
+finally: |
|
54 |
+ sys.stdin = sys.stdin.fil_ |
|
55 |
+ sys.stdout = sys.stdout.fil_ |
|
56 |
+ __builtins__.raw_input = oraw_input |
|
57 |
+ |
|
58 |
+data = stdin.get_data().split('\n') |
|
59 |
+out = [] |
|
60 |
+counter = 0 |
|
61 |
+for line in data: |
|
62 |
+ if line.startswith('>>> ') or line.startswith('... '): |
|
63 |
+ line = line.split(' ', 1)[-1] |
|
64 |
+ counter += 1 |
|
65 |
+ line = 'l%s = %s' % (counter, line) |
|
66 |
+ else: |
|
67 |
+ line = 'repr(l%s) == """%s"""' % (counter, line) |
|
68 |
+ out.append(line) |
|
69 |
+ |
|
70 |
+print '\n'.join(out) |
|
71 |
+exec '\n'.join(out) |