Advertisement
s243a

mdict_test

Nov 25th, 2018
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.34 KB | None | 0 0
  1. #import s243a.monad.mdict
  2. import inspect
  3.  
  4. def hprint(x):
  5.     print('entering hprint')
  6.     print(x)  
  7. def hset(d,**kwords): #Not sure if I use this function
  8.     for k,v in kwords:
  9.         d[k]=v    
  10. def hdo(*lines,**kw):
  11.     args=kw.get('args',None),
  12.     d=kw.get('d',{})
  13.     ops=kw.get('ops',None)
  14.     #print("nlines="+str(len(lines)))
  15.     #print('kw='+str(kw))
  16.     #print('line='+str(lines))
  17.     #print('hdo:')
  18.     #print('args='+str(args))
  19.     #print('d='+str(d))
  20.     #print('ops='+str(ops))
  21.     #print(lines)
  22.     out=args
  23.     #Is=range(0,len(lines)-1)
  24.     #print("Is="+str(Is))
  25.     for i in range(0,len(lines)):
  26.         line=lines[i]
  27.         #print("hdo i="+str(i))        
  28.         out=assignKey(line, #typically ('var',fn) or fn. 'var' is a value to assign the output of fn
  29.                       args=out, #the output of the last function which is the input to the next function
  30.                       d=d, # a dictionary object used for assiging function results
  31.                       ops=ops) # can be used to over-ride operations. Defualt operation is function compostion
  32.         #print("hdo i="+str(i))
  33.         #print("hdo.out="+str(out))
  34.         #print("hdo.d="+str(d))
  35.     return out
  36.     #return lambda: hseq(d,lines(len(lines)))
  37. def assignKey(line,     #typically ('var',fn) or fn. 'var' is a value to assign the output of fn
  38.             args=None,  #the output of the last function which is the input to the next function
  39.             d={},      # a dictionary object used for assiging function results
  40.             ops=None): # can be used to over-ride operations. Defualt operation is function compostion
  41.   out=args
  42.   #print("line="+str(line))
  43.   #print(type(line))
  44.   if isinstance(line,tuple) or \
  45.      isinstance(line,list):
  46.     if len(line)>1:
  47.       if hasattr(line[1], '__call__'): #TODO and line[0] is string
  48.         out=m_add_out_and_dict(line[1],args=out,d=d,ops=ops)
  49.         d[line[0]]=out
  50.         return line[1]
  51.   elif hasattr(line,'__call__'):
  52.     out=m_add_out_and_dict(line,args=out,d=d,ops=ops)
  53.     return out
  54.   raise ValueError('Bad input value or not yet implemented') #Maybe some other cases?
  55. #dev
  56. def add_out_and_dict(fn,args,d):
  57.   #print("fn="+str(fn))
  58.   #print("args="+str(args))
  59.   #print("d="+str(d))
  60.   spec=inspect.getargspec(fn)
  61.   dict_i=-1
  62.   #args_i={}  
  63.   for i in range(0,len(spec.args)):
  64.       if spec.args[i]=='d':
  65.         dict_i=i
  66.         continue
  67.       #We may add more to this loop
  68.   if (len(spec)==1):
  69.     if dict_i == -1:
  70.         return lambda: fn(args)
  71.     else:
  72.         return lambda: fn(d)
  73.   else:
  74.       if dict_i==0:
  75.         return lambda: fn(d,args)    
  76.       elif dict_i==1:
  77.         return lambda: fn(args,d)    
  78.       else:
  79.         return lambda: fn(args,d=d)      
  80.   #print(spec)
  81.   #print("spec len="+str(len(spec.args)))
  82.   #if len(spec.args)==0:
  83.   #  return fn
  84.   #elif len(spec.args)==1:
  85.   #  return lambda: fn(args) if args is not None \
  86.   #    else lambda: fn(d)
  87.   #elif len(spec.args)==2:
  88.   #  return lambda: fn(args,d)
  89. #def lambda_asgn(fn,args,spec=inspect.getargspec(fn)):
  90. #    if spec is None:
  91.      
  92. def m_add_out_and_dict(fn,args,d={},ops=None):
  93.   if ops is None: #This is
  94.     return add_out_and_dict(fn,args,d)
  95.   else:
  96.     if hasattr(ops,'op'):
  97.       ops.op(args,fn)
  98.     elif hasattr(ops,'bind'):
  99.       ops.bind(args,fn)
  100.     elif hasattr(ops,'next'):
  101.       ops.next(args,fn)
  102.  
  103. #out=hdo( ('x',(lambda d,o=[]: hset(d,{'x':1}))),    #x=1 and return x
  104. #               lambda d,o=[]: hprint(d['x']),       #print previously returned value
  105. #         ('y', lambda d,o=[]: hset(d,{'y':2})),     #y=2 and don't return anything (kind of like a let statment in Haskel)
  106. #         lambda d,o=[]: hprint(d['x'].__call__(d)+
  107. #                    d['y'].__call__(d))             #print previously assiigned x and y
  108. #        )
  109. out=hdo(   ('x', lambda d,o=[]: 1),          #x=1 and return x
  110.            lambda d,o=[]: hprint(d['x']()),    #print previously returned value
  111.            ('y', lambda d,o=[]: 2),          #y=2 and don't return anything (kind of like a let statment in Haskel)
  112.            lambda d,o=[]: hprint(str(d['x']()+\
  113.                                      d['y']())
  114.                                 )                #print previously assiigned x and y
  115.        )
  116. print("printing out")
  117. print(out)
  118. print("calling out")
  119. out2=out()
  120. print("printing out 2")
  121. print(out2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement