This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten_dict(root, prefix_keys=True): | |
dicts = [([], root)] | |
ret = {} | |
seen = set() | |
for path, d in dicts: | |
if id(d) in seen: | |
continue | |
seen.add(id(d)) | |
for k, v in d.items(): | |
new_path = path + [k] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.7.3 (default, Aug 1 2012, 05:14:39) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
(PythonConsole) | |
>>> in_classifiers | |
[<EarthClassifier instance at 0xa6637570>] | |
>>> | |
Running script: | |
Traceback (most recent call last): | |
File "<console>", line 1, in <module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.24147391319 | |
0.753793954849 | |
0.652230978012 | |
0.547729969025 | |
0.530550003052 | |
0.423093080521 | |
0.413625001907 | |
0.409635066986 | |
0.385325908661 | |
0.494672060013 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
['bl_count', | |
'd_all_img_count', | |
'd_blockquote_count', | |
'd_caption_word_kurtosis', | |
'd_caption_word_kurtosis_trimmed', | |
'd_caption_word_mean', | |
'd_caption_word_mean_trimmed', | |
'd_caption_word_median', | |
'd_caption_word_median_abs_dev', | |
'd_caption_word_rel_std_dev', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#super basic | |
$ python -m timeit -s 'def inc(x=1): return x+1' 'inc()' | |
10000000 loops, best of 3: 0.0785 usec per loop | |
$ python -m timeit -s "from functools import partial | |
> def inc(x=1): return x+1 | |
> inc = partial(inc, 1)" 'inc()' | |
10000000 loops, best of 3: 0.0922 usec per loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestStr(str): | |
def __init__(self, a, b=True): | |
if b: | |
print(b'using custom init for sure') | |
super(TestStr, self).__init__(a) | |
>>> core.TestStr('hi') | |
using custom init for sure | |
'hi' | |
>>> core.TestStr('hi', False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coclass 'json' | |
enc_json=: 3 : 0 | |
select. t=. 3!:0 y | |
case. 2;131072 do. | |
if. y-:'json_true' do. 'true' | |
elseif. y-:'json_false' do. 'false' | |
elseif. y-:'json_null' do. 'null' | |
elseif. do. | |
'"', '"',~ jsonesc utf8^:(131072=t) ,y | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_jumps(height): | |
if height <= 2: | |
return [] | |
max_jump = height - 1 | |
jumps = [] | |
j = 3 | |
for j in range(2, max_jump): | |
jumps = jumps + [j] + jumps | |
jumps.extend([max_jump] + jumps) | |
return jumps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_bal(left, right): return left - right | |
node = stack[i] | |
cbal = get_bal(node[1], node[2]) | |
if abs(cbal) < 2: break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
comparing tuples to tuples is faster than comparing lists to lists, | |
but more interestingly, comparing a tuple to a list is apparently | |
faster than lists to lists. | |
""" | |
$ python -m timeit "[1, 1] < [1, 2]" | |
10000000 loops, best of 3: 0.186 usec per loop | |
$ python -m timeit "[1, 1] < (1, 2)" | |
10000000 loops, best of 3: 0.129 usec per loop |