How best to reference parameters.

D

David Poundall

I am writing a scada package that has a significant amount of user
defined parameters stored in text files that I wish to cleanly access
in code. By way of an example, a few lines from the configuration file
would typically be ...

[Plant Outputs]
Y0 P1 Pump 1 Pressure
Y1 P2 Pump 2 Fluid Transfer Pump
Y2 P3 Pump 3 Vac Pump
Y3 P4 Pump 4 Vac Pump
Y4 P5 Pump 5 / Pump 1B
Y5 P6 Pump 6 / Pump 2B
Y6 M
Y7 D
Y10 E
Y11 F

I can read these values in as dictionary items and refernce them in
code like this...

Y['P4'] = 1 # Which will ultimately switch my pump on
Y['P3'] = 0 # Which will ultimately switch my pump off

but I would much rather reference the plant outputs like this ...

Y.P4 = 1
Y.P3 = 0

basically it makes it easier for third parties to code, also my IDE
(wing) is able to use intellisense to determine exactly which Y outputs
have been loaded during code configuration.

Now I can achieve this by loading the configuration parameters in a
class like this...

class c_y:
def __init__(self):
self.P1 = 0
self.P2 = 0
self.P3 = 0
self.P4 = 0

so I can do

Y = c_y()
Y.P4 = 1
Y.P3 = 0
etc

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
 
D

David Poundall

It looks like I am going to have to bite the bullet and use properties.
The following should do what I want.

class test:

def __init__(self):
self.__HB = 0
self.__VPG = 0

def _get_HB(self): return (self.__HB, 'MF1', 0)
def _set_HB(self, x): self.__HB = x
HB = property(_get_HB, _set_HB)

def _get_VPG(self): return (self.__VPG, 'MF1', 1)
def _set_VPG(self, x): self.__HB = x
VPG = property(_get_VPG, _set_VPG)

Usisg this I can set / and clear HB and VPG usig the syntax

t = test()
t.HB = 1
t.VPG = 0

and when I read them I get a tuple which holds the currcnt value in
position 0 and the other parameters I need in positions 1 and 2.
 
T

Tony Nelson

"David Poundall said:
I am writing a scada package that has a significant amount of user
defined parameters stored in text files that I wish to cleanly access
in code. By way of an example, a few lines from the configuration file
would typically be ...

[Plant Outputs]
Y0 P1 Pump 1 Pressure
Y1 P2 Pump 2 Fluid Transfer Pump
Y2 P3 Pump 3 Vac Pump
Y3 P4 Pump 4 Vac Pump
Y4 P5 Pump 5 / Pump 1B
Y5 P6 Pump 6 / Pump 2B
Y6 M
Y7 D
Y10 E
Y11 F

I can read these values in as dictionary items and refernce them in
code like this...

Y['P4'] = 1 # Which will ultimately switch my pump on
Y['P3'] = 0 # Which will ultimately switch my pump off

but I would much rather reference the plant outputs like this ...

Y.P4 = 1
Y.P3 = 0
...

d = {'a':1, 'b':2, 'c':3}

class foo:
def __init__(self, d):
self.__dict__.update(d)

f = foo(d)

print f.a, f.b, f.c

(retyped from memory)
________________________________________________________________________
TonyN.:' *firstname*nlsnews@georgea*lastname*.com
' <http://www.georgeanelson.com/>
 
D

David Poundall

Nice - I like that Tony. I had seen it around before but I didn't
catch on. Thanks for the clear example..
 
R

Ron Adam

David said:
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
 
R

Ron Adam

David said:
Sadly Ron, c_y can only see index and showall in your example.

Well, don't give up! The approach is sound and I did say it was
untested. Here's a tested version with a few corrections. :)

Cheers,
Ron




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, %r, %r\n"
% ( self.__dict__[item].name,
self.__dict__[item].status,
self.__dict__[item].ptype,
self.__dict__[item].number ) )
return ''.join(out)


pumplist = [ ('p1', 'ob1', 0),
('p2', 'ob1', 1),
('p3', 'ob1', 2) ]

c_y = C_Y(pumplist)

print c_y.p1.name
print c_y.p1.status
print c_y.p1.ptype
print c_y.p1.number
print

c_y.p1.status = 1
print c_y.p1.status
print

print c_y.showall()
 
D

David Poundall

Sorry Ron, my earlier reply was too brief I wasn't thinking straight
(so what else is new ;-) my apologies. The main reason for going down
the route I am looking at (thread 2) is that intellisense runs OK in my
IDE using that aproach.

In my application this will be important as I will be using the IDE
(Wing) as a high level user editor in a limited code area. The user
will need to see (hence the use of intellisense) which properties are
available for him to work with.

The second example you gave runs similar to the first, and
unfortunately the intellisense does not pick up available properties as
you go walk the dots. Not suprisingly really as the class is dynamic
in the way it operates. It only see's the index property and the
showall function.

The code as you wrote it works fine though, and it is already salted
away in my meager (but growing) codebank.

Many thanks for your reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top