How best to reference parameters.
Ron Adam
rrr at ronadam.com
Wed Oct 26 11:00:16 EDT 2005
David Poundall wrote:
> However, what I really would like is something like...
>
> class c_y:
> def __init__(self):
> self.P1 = [0, 'OB1', 0 ]
> self.P2 = [0, 'OB1', 1 ]
> self.P3 = [0, 'OB1', 2 ]
> self.P4 = [0, 'OB1', 3 ]
>
> Because that way I can also hold binary loadings and data register
> (this is a PLC application) references which give me full information
> for handling the pump bits.
>
> However, this means that I have to access the pump status bits like
> this...
>
> Y.P4[0] = 1
> Y.P3[0] = 0
>
> Which takes me away from the clean code
>
> Y.P4 = 1
> Y.P3 = 0
>
> That I would like to have.
>
> Can anyone suggets a technique for parameter storage that may be able
> to give me what I want here ?
>
> Thanks in advance.
>
> David
How about the following?
(not tested)
class Pump(object):
def __init__(self, name, ptype, number):
self.status = 0
self.name = name
self.ptype = ptype
self.number = number
class C_Y(object):
def __init__(self, *plist):
index = []
for p in plist:
self.__dict__[p[0]] = Pump(*p)
index.append(p[0])
self.index = index
def showall(self):
out = []
for item in self.index:
out.append( "%s: %r\n"
% (item, self.__dict__[item] )
Then with a list formed as ....
pumplist = [ ('p1', 'ob1', 0),
('p2', 'ob1', 1),
('p3', 'ob1', 2),
...
]
You can then do...
c_y = C_Y(pumplist)
print c_y.p1.name --> 'p1'
print c_y.p1.status --> 0
print c_y.p1.ptype --> 'ob1'
print c_y.p1.number --> 0
c_y.p1.status = 1 # p1 on
c_y.p1.status = 0 # p1 off
print c_y.p2.status --> 0
print c_y.p2.ptype --> 'ob1'
print c_y.p2.number --> 1
etc...
print c_y.showall()
Cheers,
Ron
More information about the Python-list
mailing list