In the sample program below, I want to send a random method to a class
instance.
In other words, I don't know which method to send until run-time. How
can I send ch, which is my random choice, to the myclass instance?
Thanks,
Bob=
####
import random
class myclass(object):
def meth1(self):
print 'meth1'
def meth2(self):
print 'meth2'
c=myclass()
meths=['meth1', 'meth2']
ch=random.choice(meths)
c.ch()
Thanks for your responses, everybody. Turned out to be easiest to use
Arnaud's Getattr suggestion. Below is a simplification of what I
ended up with; the trick appears in the algday class.
"""
I have dictionaries of OHLC tuples (open, high, low, close market
prices) for two hypothetical stocks (HUN and TEN); the dictionaries
are keyed to dates.
For each day that we have data, we want to be able to evaluate an
algorithm of the form:
(mysum 'HUN' 'open' 'TEN', close)
in other words, to add the opening value of HUN to the closing value
of TEN.
Stock names and market values are stored in two lists: namelist and
valuelist. Namelist contains the stock symbols (HUN, TEN). Valuelist
contains dictionaries keyed to dates. Associated in the dictionary
with
each date is a tuple containing OHLC prices. Note that even in this
sample
we have to allow for days when one stock has market values
and the other stock doesn't, as in the case of 20080424.
We use a class algday, which, given an algorithm and day, has a method
calc() which returns the result of applying that algorithm to the day.
This class (algday) uses another class, namedayval, which, given a
stock name and a
OHLC valuename, returns the value.
=Bob Moore
Thanks to Arnaud Delobelle, Aaron Brady, and Ben Finney.
"""
import random
class namedayvals(object):
def __init__(self,name,date):
self.name=name
self.date=date
self.index=namelist.index(name)
self.valuelist=valuelist[self.index]
self.dayswithvals=self.valuelist.keys()
def open(self):
if self.date not in self.dayswithvals:
return 'NA'
else:
return self.valuelist[self.date][0]
def high(self):
if self.date not in self.dayswithvals:
return 'NA'
else:
return self.valuelist[self.date][1]
def low(self):
if self.date not in self.dayswithvals:
return 'NA'
else:
return self.valuelist[self.date][2]
def close(self):
if self.date not in self.dayswithvals:
return 'NA'
else:
return self.valuelist[self.date][3]
class algday(object):
def __init__(self,myop,sec1,meth1,sec2,meth2,date):
self.myop=myop
self.sec1=sec1
self.meth1=meth1
self.sec2=sec2
self.meth2=meth2
self.date=date
def show(self):
print 'algday',
self.myop,self.sec1,self.meth1,self.sec2,self.meth2,self.date
def calc(self):
s1=namedayvals(self.sec1, self.date)
x1=getattr(s1,self.meth1)()
s2=namedayvals(self.sec2, self.date)
x2=getattr(s2,self.meth2)()
if self.myop=='mysum':
out=mysum(x1,x2)
elif self.myop=='myless':
out=myless(x1,x2)
elif self.myop=='mydiv':
out=mydiv(x1,x2)
elif self.myop=='mytimes':
out=mytimes(x1,x2)
else:
out='NA'
return out
def mydiv(num1, num2):
if num1=='NA' or num2=='NA':
temp='NA'
else:
if num2==0:
temp=0.0
else:
temp=float(num1)/float(num2)
return temp
def myless(num1,num2):
if num1=='NA' or num2=='NA':
temp='NA'
else:
temp=num1-num2
return temp
def mytimes(num1,num2):
if num1=='NA' or num2=='NA':
temp='NA'
else:
temp=num1*num2
return temp
def mysum(num1,num2):
if num1=='NA' or num2=='NA':
temp='NA'
else:
temp=num1*num2
return temp
#####
namelist=[]
valuelist=[]
namelist.append('HUN')
valuelist.append(dict({'20080422': (100.1,100.2,100.3,100.4),
'20080423': (101.1,101.2,101.3,101.4),
'20080424': (102.1,102.2,102.3,102.4)}))
namelist.append('TEN')
valuelist.append(dict({'20080422': (10.1,10.2,10.3,10.4),
'20080423': (11.1,11.2,11.3,11.4)}))
operatorlist=['mydiv','mysum','myless','mytimes']
valuenames=['open','high','low','close']
daylist=['20080422','20080423','20080424']
while 1:
rop=random.choice(operatorlist)
rn1=random.choice(namelist)
rv1=random.choice(valuenames)
rn2=random.choice(namelist)
rv2=random.choice(valuenames)
print rop,rn1,rv1,rn2,rv2
calcs=[]
for date in daylist:
a=algday(rop,rn1,rv1,rn2,rv2,date)
calcs.append([date,a.calc()])
print 'daily calcs=', calcs
print 'Control-C to stop'