for thing in ['cat', 1, 'dog', 2, range(0, 3), 0.03]: assert thing == id(thing) print(thing, id(thing)) print_h4('Function composition') res = compose_hybrid('composition is neat!', f=dictify, g=listify) print(res) print_h4('Random funcs') print(listify('foo')) print(dictify('foo')) print_h4('Higher order function composition') f2 = compose_hybrid_hof(f=listify, g=dictify) print(f2('composition yay!')) print_h4('Function composition on a "stream" or incoming set') res = [compose_hybrid(str(x), f=fooify, g=bazify) for x in range(4)] print(res) print_h4('Messing around...') res = ''.join(res) for x in range(4): res += compose_hybrid(x, f=fooify, g=bazify) print(res) res = '' for x in range(10): # Just to make things interesting... res += compose_hybrid(random_dna(), f=delimit, g=prefix) print(res) composed = f_g(f, g, 4) print_simple('Traditional composed example:', composed, newline=False)
def _handle(data): """Quick handler to help with nested tuple comprehensions -- arguments are not easily passed using `map`, so this deals with passing them as *args, since the map function won't do it automatically.""" return prnt(*data) if DEBUG: with Section('Python comprehensions'): prnt( 'Dictionary comprehension', { 'data': { 'human_to_robot': {dm.random_dna(): txt.random_binary(4) for _ in range(4)}, 'robot_to_human': {txt.random_binary(4): dm.random_dna() for _ in range(4)} } }) prnt('List comprehension', [x**2 for x in range(10) if x % 2 == 0]) prnt('List comprehension - nested', [[x**y for x in range(1, 4) if x % 2 == 0] for y in range(1, 8)]) wtf = [[_nested(min=x), _nested(max=y)] for x, y in enumerate(range(1, 10))] print_h2('List comprehensions - triple nested') ppr(wtf)
def __enter__(self): print('Saving data: {}'.format(self.content)) def __exit__(self, exception_type, exception_value, traceback): map(self._print, self.content) if DEBUG: with Section('Context managers (aka "with")'): print_h2('[decorator] with 1 - database save wrapper, simplified') with orm_save(FakeDatabase, data={'my': 'fake-data'}) as saved: db, data = saved for k, data in data.iteritems(): print('Yielded results: {}'.format(data)) print_h2('[decorator] with 2 - database save wrapper, simplified') with orm_save2(foo='bar', bim='bam') as stuff: for data in stuff: print('Yielded results: {}'.format(data)) print_h2('[decorator] nested context managers - save to db + show html') stuff = { 'dna': dm.random_dna(max=20), 'faketrix': dm.random_binary_matrix()} with orm_save2(**stuff) as data, to_list(**data) as html: print('Saved all data, and converted to html: {}'.format(html)) print_h2('[class] context manager - save to db + show html') with FakeDatabaseSaveHandler(stuff) as data: pass