''' Created on Jan 7, 2016 @author: Brett Paufler Copyright Brett Paufler This has some nice code. Essentially throw away. But Review First. fold Not recurse length appears to be infinite Why closure_tally simple incrementor closure as a test This has turned into a long worksheet lots that could be killed along the way ''' def prin(x): print x #a = [prin(a) for a in range(1,6)] #print a def prinf(str_to_format, *args): print str_to_format % args #prinf("This Num: %d", 25) class Struct(): '''C style Struct object offering dot notation. s = Struct(a=1, b=2) print s.a, s.b #1, 2 s.a += 1 print s.a, s.b #2, 2 ''' def __init__(self, **entries): self.__dict__.update(entries) def __repr__(self): args = ['%s=%s' % (k, repr(v)) for k, v in vars(self).items()] return 'Struct(%s)' % ', '.join(args) ''' a = Struct(test=3, next=4) print a print a.test a.foo = 'yes' print a.foo print vars(a) print dir(a) print dir(Struct) ''' from operator import add, sub, mul def my_reduce(func, r, xs): '''Reduces passed list with given func. func = any function operator.add, operator.sub nice choices x = starting value 0 is a good choice for this aList = list (of int's presumably)''' ''' #version #1 #head, tail extraction x, xs = aList.pop(False), aList #print fold, func, x, h, t, aList #print id(fold), id(func), id(x), id(h), id(t), id(aList) #Guard / Recursion split if not t: return func(x, h) else: return fold(func, func(x, h), t) ''' x = xs.pop(False) return my_reduce(func, func(r, x), xs) if xs else func(r, x) ''' aL = range(1,1000) print id(aL) print my_reduce(add, 0, range(1,1000)) #499500 print my_reduce(sub, 0, range(1,1000)) #-499500 print my_reduce(mul, 1, range(1,1000)) ''' def closure_tally(x=0): '''Closure which returns a tally instance. x initializes value of tally ''' x = [x] #Mutable container type required for closure def tally(y=1): '''Increments counter by passed value.''' x[0] += y return x[0] return tally ''' f = closure_tally(10) print f(5) print f(5) print f(-10) print dir(f) print vars(f) print type(f.__closure__[0]) print f.__closure__[0].cell_contents ''' class Tally(): '''Class as Closure, so why Closure.''' def __init__(self, start=0): self.x = start def tally(self, n=1): self.x += n return self.x ''' t = Tally() n = Tally(10) for _ in range(10): print t.tally(1), n.tally(1) ''' m = 0 def module_closure(m_in=m, n=1): '''Works as closure in this module and if called from another.''' global m m += n return m ''' print m for _ in range(10): print module_closure(m, 1) m = 1000 print m for _ in range(10): print module_closure(m, 1) ''' def paren(func): text = func if isinstance(func, str) else func() return '%s %s %s' % ('(', text, ')') def bracket(func): def wrapper(): text = func if isinstance(func, str) else func() return '%s %s %s' % ('(', text, ')') return wrapper def hello(): print 'this' return 'Hello World' @bracket @bracket @bracket def hello_b(): return 'Hello World' #print hello() #print bracket(hello) #print bracket(hello()) #print bracket(hello)() #print bracket(hello() + hello()) #print bracket(hello())() #print #print paren(hello) #print paren(paren(hello)) #print #print hello_b() def setA(obj): print 'setA Fires' obj._a = 'A is Set Once, I said, ' #return 25 class X(): def __init__(self): self._x = None self.z = None self._a = None @property def x(self): if not isinstance(self._x, int): if isinstance(self.z, int): self._x = self.z return self._x @property def a(self): if not self._a: setA(self) return self._a# if self._a else def __hash__(self): return hash(self._x) ''' x = X() print hash(x) print hash(X) print hash(X()) ''' ''' x = X() print x._x, x.x, x.z x.z = None print x._x, x.x, x.z x.z = 'text' print 't:', x._x, x.x, x.z x.z = 0 print '0:', x._x, x.x, x.z x.z = 100 print x._x, x.x, x.z #x.x = 9 #print x.x print x.a, x.a, x.a ''' ''' print Ellipsis print type(Ellipsis) for d in dir(Ellipsis): print d #, Ellipsis[d] #print d.__init__ e = Ellipsis print e.__hash__() print hash(e) ''' ''' a = [] a.append(a) a = a.pop() print a a.append(a) a.append(a) a.append(a) #print repr(a) #print eval(repr(a)) ''' import inspect from collections import OrderedDict x = 0 or 1 print x x = False or True print x x = None x = x if not isinstance(x, type(None)) else 3 print x x = 1 or 0 print x x = False or True print x x = None x = x if not isinstance(x, type(None)) else 3 print x #NoneType #print 'herer' #(x,) = [[],2,4][0] #print x #(x,) = [1,2,3][0] #(x,) = x