[Python-ideas] write to cell
Aaron Brady
castironpi at comcast.net
Mon Jan 21 01:14:19 CET 2008
Decorator 'FreeVarArr', free variable array, returns an array of its
decoratee (decorated function), with free variables set by name to a range
of values.
Produced & attached.
import new
def cell_values( *cells ):
return [ cell.cell_contents for cell in cells ]
def CellMake( *opts ):
dumbopts= [ 'opt%i'% i for i in range( len( opts ) ) ]
dumbvars= ', '.join( dumbopts )
innermakefmt= "def innermake( %s ):\n\tdef toget(): %s\n\treturn toget"
exec innermakefmt% ( dumbvars, dumbvars )
innerfunc= innermake( *opts )
freevars= list( innerfunc.func_code.co_freevars )
freevarindices= map( freevars.index, dumbopts )
return tuple( [ innerfunc.func_closure[i] for i in freevarindices ] )
def FreeVarArr( **kwargs ):
vars= kwargs.keys()
iters= [ kwargs[var] for var in vars]
def newf( wfunc, closure ):
return new.function( wfunc.func_code, wfunc.func_globals,
wfunc.func_name, wfunc.func_defaults, closure )
def recall( wfunc ):
freevars= list( wfunc.func_code.co_freevars )
freeindices= [ freevars.index( var ) for var in vars ]
iterzip= zip( *[ iters[i] for i in freeindices ] )
closures= [ CellMake( *opts ) for opts in iterzip ]
funcs= [ newf( wfunc, closure) for closure in closures ]
return funcs
return recall
if __name__== '__main__':
print CellMake( 2, 3, 'abc' )
a= CellMake( 2, 3, 'abc' )
def who():
some, someelse, moreelse, moreover= None, None, None, None
@FreeVarArr( some= range(8,14), someelse= range(2,19), moreelse=
['a']*90, moreover= ['What!']*90 )
def what():
"doc string"
print someelse, moreelse, moreover
assert moreelse== 'a'
assert moreover== 'What!'
print "this prints", some #assign 'some' to range(8,14) in
successive array functions
print "John says:", some+ someelse, moreover+ ' '+ moreelse
return some
return what
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: funcarray.py
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20080120/1371cf9f/attachment.ksh>
More information about the Python-ideas
mailing list