Navigation Menu

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 / str_re_timeits.py
Created February 18, 2015 20:31
Timeits!
# "in" faster than re search for single character searches
# PS also held true for longer strings
$ python -m timeit -s 'import re; _re = re.compile("[{]",re.UNICODE); target = "wwoooo{ooo"' '_re.search(target)'
10000000 loops, best of 3: 0.159 usec per loop
$ python -m timeit -s 'target = "wwoooo{ooo"' '"{" in target'
10000000 loops, best of 3: 0.0269 usec per loop
#####
@mahmoud
mahmoud / duck_type_format.py
Last active August 29, 2015 14:15
Duck-typing and string formatting. Yet another example of percent formatting's superiority over newstyle .format()
class MyType(object):
def __init__(self, func):
self.func = func
def __int__(self):
return int(self.func())
def __float__(self):
return float(self.func())
@mahmoud
mahmoud / git_wtf
Created February 25, 2015 00:26
wtf git
~/work/support$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
~/work/support$ cd ..
~/work$ cd support
~/work/support$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
def main():
assert gen_matrix(4, 3) == [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
print_matrix(gen_matrix(4, 6))
# 1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7
def gen_matrix(width, height):
ret = [range(1 + width * ih, 1 + width * (ih + 1)) for ih in range(height)]
@mahmoud
mahmoud / mro_items_pypy.py
Created April 10, 2015 05:05
mro_items on pypy
046 >>> sorted([k for k, v in mro_items(int) if not (callable(v) or isinstance(v, type(int.__int__)))])
Expected:
['__class__', '__doc__', '__doc__', 'denominator', 'imag', 'numerator', 'real']
Got:
['__class__', '__doc__', '__doc__', '__new__', '__new__', '__subclasshook__', 'denominator', 'imag', 'numerator', 'real']
@mahmoud
mahmoud / feedback_scores.md
Created April 13, 2015 02:02
musings on feedback scores

Feedback scores should be based on simple +1/-1 semantics are by far the most prevalent among consumer sites(eBay/Amazon/YouTube/Reddit/StackOverflow) and seem effective. However, I think the score displayed should be twofold: all-time cumulative, as well as weighted. With the rise of the "trending" classification system, a user should be able to quickly discern whether a score is recent.

Another alternative in this vein would be to have "rounds" of scoring. That is, cumulative score is permanent, and while it can be impressive, in all cases it appears to stagnate. But scores within the round are more dynamic. The thrill of a new round, experiments with strategies, and end-run pushes for big finishes are all there.

One disadvantage of both arrangements is the potential for accidental alienation of experienced users. Precious few users use a site forever, but overaccentuating the passage of time can certainly accelerate losing the old guard. Every site is different, and careful assessment of this risk is cr

@mahmoud
mahmoud / unicode_encoding_kwarg_issue
Created April 21, 2015 04:43
str/unicode encoding kwarg causes exceptions
The encoding keyword argument to the Python 3 str() and Python 2 unicode() constructors is excessively constraining to the practical use of these core types.
Looking at common usage, both these constructors' primary mode is to convert various objects into text:
>>> str(2)
'2'
But adding an encoding yields:
>>> str(2, encoding='utf8')
@mahmoud
mahmoud / versions_of_shame.py
Created May 17, 2015 19:22
version hall o shame
(Pdb) pp [x['info']['name'] for x in pkg_info_map.pkg_infos.values() if x.get('info', {}).get('downloads', {}).get('last_day', 0) > 5000 and x['info']['version'].startswith('0')]
[u'alembic',
u'carbon',
u'cffi',
u'cmd2',
u'colorama',
u'cryptography',
u'eventlet',
u'extras',
u'Flask',
@mahmoud
mahmoud / json_template.py
Created May 18, 2015 22:18
Quick JSON Template demo
import json
json_template = """\
{"test_list": [],
"test_obj": {"test_num": 0.0}}
"""
obj_dict = json.loads(json_template)
obj_dict['test_obj']['test_num'] = 1.0
def close_range(start, step=1, count=None):
i, orig_step = 0, step
if count is None:
count = start / step
while i < count:
yield start + (i * step)
step *= -1
if step != orig_step:
i += 1
return