Skip to content

Instantly share code, notes, and snippets.

@sjatkins
Last active November 3, 2016 00:57
Show Gist options
  • Save sjatkins/3545a4fd7951fdde5bb52ffe02642224 to your computer and use it in GitHub Desktop.
Save sjatkins/3545a4fd7951fdde5bb52ffe02642224 to your computer and use it in GitHub Desktop.
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