Last active
November 3, 2016 00:57
-
-
Save sjatkins/3545a4fd7951fdde5bb52ffe02642224 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from itertools import chain, cycle, izip, imap, islice | |
def fizzbuzz(count): | |
cycler = lambda mod_n, mod_label: cycle((mod_n - 1) * [''] + [mod_label]) | |
str_value = lambda (index, (fizz, buzz)): (fizz + buzz) or str(index + 1) | |
take = lambda n, iterable: islice(iterable, n) | |
data = take(count, enumerate(izip(cycler(3, 'fizz'), cycler(5, 'buzz')))) | |
return imap(str_value, data) | |
test_fb = lambda n: list(fizzbuzz(n)) | |
def make_readonly_prop(obj, name, attr_name=None): | |
""" | |
Makes a read only property on the object obj named name from the obj attribute | |
named attr_name. The attribute name defaults to name prepended with '_' | |
""" | |
if not attr_name: | |
attr_name = '_' + name | |
if hasattr(obj, attr_name) and not hasattr(obj, name): | |
setattr(obj.__class__, name, property(lambda x: getattr(obj, '_' + name))) | |
def make_ro_properties(obj, *prop_names): | |
for name in names: | |
make_readonly_prop(obj, name) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment