Skip to content

Instantly share code, notes, and snippets.

View mahmoud's full-sized avatar
Back on the grid!

Mahmoud Hashemi mahmoud

Back on the grid!
View GitHub Profile
@mahmoud
mahmoud / flatten_dict.py
Created August 20, 2012 08:55
An ok way to flatten nested dictionaries in Python (iteratively, not recursively). Even does basic circularity checks (untested at this point).
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]
@mahmoud
mahmoud / orange_junk.py
Created October 21, 2012 21:46
orange junk
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>
@mahmoud
mahmoud / PyPy Binary Deserialization progression
Created November 13, 2012 22:01
CPython 2.7 vs. PyPy 1.9 serialization performance. 64-bit Ubuntu 12.
1.24147391319
0.753793954849
0.652230978012
0.547729969025
0.530550003052
0.423093080521
0.413625001907
0.409635066986
0.385325908661
0.494672060013
@mahmoud
mahmoud / attr_names.py
Created November 18, 2012 00:00
Cleaned and decorrelated attribute names
['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',
@mahmoud
mahmoud / partial_perf.txt
Created November 19, 2012 09:45
partial() performance
#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
@mahmoud
mahmoud / pdw_unicode_str_init.py
Last active December 10, 2015 08:48
This must be the result of an optimization of some sort? If you inherit from str or unicode and override the __init__, the __init__ will get called, but it's subject to the exact same signature as the base str or unicode type. Probably just need to override __new__() since str/unicodes are supposed to be immutable anyhoo. (PS, have I always had …
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)
@mahmoud
mahmoud / json.ijs
Created January 2, 2013 05:16
J now has a more official JSON thing! Forget that other thing I posted, this is much clearer and more correct as you'll plainly see ;) (Disclaimer: I did not write this, nor do I know who did. I know a guy who likes J who showed it to me kthxbai)
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.
@mahmoud
mahmoud / tree_inorder_traversal.py
Last active December 14, 2015 22:38
numeric method for generating indices for in-order traversal of a tree stored in a list/array.
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
@mahmoud
mahmoud / avl_rebalance.py
Created March 15, 2013 08:09
AVL rebalancing
def get_bal(left, right): return left - right
node = stack[i]
cbal = get_bal(node[1], node[2])
if abs(cbal) < 2: break
@mahmoud
mahmoud / tuple_vs_list_compares.py
Last active December 15, 2015 00:39
the strange work of lists and tuples.
"""
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