still struggling, howto use a list-element as a name ?

S

Stef Mientki

In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily be
transported to the pin-objects.

ad 2:
THAT's the problem: how do automate these lines "self.GND = self.pin[0]"

I'm also in for other solutions.

thanks,
Stef


class Power_Supply(device):
pinlist = {
0: ('GND', _DIG_OUT, _par2),
1: ('VCC', _DIG_OUT, _par33)
}

def __init__(self):
# store pin-names and pin-parameters in pins
for k in self.pinlist.keys():
self.pin[k].Name = self.pinlist[k][0]
self.pin[k].Value = self.pinlist[k][2]

# for some pins, we also want to be able to use logical names
# HOW TO USE SOMETHING like
# "self.pinlist[0] = self.pin[0]"
# INSTEAD OF
self.GND = self.pin[0]
self.VCC = self.pin[1]

# create a Power_Supply instance and
# test if pins can be referenced in
Power = Power_Supply()
netlist1 = ( Power.VCC, Power.pin[1], Power.GND, Power.pin[0] )
 
R

rzed

In the example below, "pin" is an object with a number of
properties. Now I want
1- an easy way to create objects that contains a number of these
"pin" 2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very
convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can
easily be transported to the pin-objects.

ad 2:
THAT's the problem: how do automate these lines "self.GND =
self.pin[0]"

I'm also in for other solutions.

thanks,
Stef


class Power_Supply(device):
pinlist = {
0: ('GND', _DIG_OUT, _par2),
1: ('VCC', _DIG_OUT, _par33)
}

def __init__(self):
# store pin-names and pin-parameters in pins
for k in self.pinlist.keys():
self.pin[k].Name = self.pinlist[k][0]
self.pin[k].Value = self.pinlist[k][2]

# for some pins, we also want to be able to use logical
names # HOW TO USE SOMETHING like
# "self.pinlist[0] = self.pin[0]"
# INSTEAD OF
self.GND = self.pin[0]
self.VCC = self.pin[1]

# create a Power_Supply instance and
# test if pins can be referenced in
Power = Power_Supply()
netlist1 = ( Power.VCC, Power.pin[1], Power.GND, Power.pin[0] )

I may be confused about what you're after, but wouldn't something
like this work? (I don't know what a _par2 object is; I've named
it something here.)

class Power_Supply(device):
def __init__(self):
self.pin = {
0:dict(Name='GND',Value=_DIG_OUT,something=_par2),
1:dict(Name='VCC',Value=_DIG_OUT,something=_par33),
}
for k in self.pin.keys():
self.__dict__[self.pin[k]['Name']] = self.pin[k]
 
S

Stef Mientki

class Power_Supply(device):
I may be confused about what you're after, but wouldn't something
like this work? (I don't know what a _par2 object is; I've named
it something here.)
_par2, is just (a reference to) a constant
class Power_Supply(device):
def __init__(self):
self.pin = {
0:dict(Name='GND',Value=_DIG_OUT,something=_par2),
1:dict(Name='VCC',Value=_DIG_OUT,something=_par33),
}
Why so complex, I need 10 or more parameters (or empty),
and then this becomes completely unreadable.
As this is part of the "user interface",
(I want that completely unknown with Python people,
write these lines),
I think my "pinlist" is much easier.

for k in self.pin.keys():
self.__dict__[self.pin[k]['Name']] = self.pin[k]
thanks "rzed" ?,
that is exactly what I was looking for:
self.__dict__[self.pinlist[k][0]] = self.pin[k]


cheers,
Stef
 
R

rzed

_par2, is just (a reference to) a constant
Why so complex, I need 10 or more parameters (or empty),
and then this becomes completely unreadable.
As this is part of the "user interface",
(I want that completely unknown with Python people,
write these lines),
I think my "pinlist" is much easier.

Whatever works for you. In your original example, you first
created "pinlist" (which is actually a Python dict), and then used
it to place values in an unspecified type named "pin" (which I
took to be another dict). I just combined the two steps.

You talk about the "pinlist" being easier ... I'm not sure what
you mean. Easier to create? Maybe. But if you are then going to
assign the values to names, then it doesn't strike me as easier to
go through the two-step process. But as I said, I may be confused
about what you are really trying to do.
for k in self.pin.keys():
self.__dict__[self.pin[k]['Name']] = self.pin[k]
thanks "rzed" ?,
that is exactly what I was looking for:
self.__dict__[self.pinlist[k][0]] = self.pin[k]

I'm glad it helped.
 
J

Jussi Salmela

Bruno Desthuilliers kirjoitti:
Stef Mientki a écrit :
In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily be
transported to the pin-objects.

ad 2:
THAT's the problem: how do automate these lines "self.GND = self.pin[0]"

I'm also in for other solutions.

I'm afraid I don't understand your design (nor the domain FWIW). ditto
<snip>

But the whole thing still looks awfully convulted and kludgy to me, and
I suspect serious design flaws... Why don't you try and explain your
real problem, instead of asking how to implement what you *think* is the
solution ?
ditto

Cheers,
Jussi
 
B

Bruno Desthuilliers

Stef Mientki a écrit :
In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very convenient
way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily be
transported to the pin-objects.

ad 2:
THAT's the problem: how do automate these lines "self.GND = self.pin[0]"

I'm also in for other solutions.

I'm afraid I don't understand your design (nor the domain FWIW). A few
comments anyway:
class Power_Supply(device):

Please reread my comments about naming convention in a previous thread...
pinlist = {

This is *not* a list, so naming it 'pinlist' is misleading. Also, why is
this defined here ?
0: ('GND', _DIG_OUT, _par2),
1: ('VCC', _DIG_OUT, _par33)
}

def __init__(self):
# store pin-names and pin-parameters in pins
for k in self.pinlist.keys():
self.pin[k].Name = self.pinlist[k][0]

What is 'self.pin' ? Where is it defined ? (NB : please try to post
*runnable* code).

And FWIW, if it's a container, why is it named 'pin', and not 'pins' ?

self.pin[k].Value = self.pinlist[k][2]

The appropriate way to use the for loop here is:
for k, v in self.pinlist.items():
self.pin[k].name = v[0]
# etc
# for some pins, we also want to be able to use logical names
# HOW TO USE SOMETHING like
# "self.pinlist[0] = self.pin[0]"
# INSTEAD OF
self.GND = self.pin[0]
self.VCC = self.pin[1]

you can build a 'reversed index':
# store pin-names and pin-parameters in pins
for k, v in self.pinlist.items():
self.pin[k].name = v[0]
self.pin[k].value = v[2]
self.reversed_index[v[0]] = self.pin[k]

and then use the __getattr__ hook:

def __getattr__(self, name):
return self.reversed_index[name]

But the whole thing still looks awfully convulted and kludgy to me, and
I suspect serious design flaws... Why don't you try and explain your
real problem, instead of asking how to implement what you *think* is the
solution ?
 
B

Bruno Desthuilliers

Stef Mientki a écrit :
> rzed wrote: (snip)


Why so complex, I need 10 or more parameters (or empty),

or empty ???
and then this becomes completely unreadable.

Named parameters are way much readables than a 10+ tuple. No one is
going to remember a 10+ list of params.
 
B

Bruno Desthuilliers

rzed a écrit :
(snip)
for k in self.pin.keys():
self.__dict__[self.pin[k]['Name']] = self.pin[k]

for pin self.pin.values():
self.__dict__[pin['name']] = pin
 
S

Stef Mientki

Jussi said:
Bruno Desthuilliers kirjoitti:
Stef Mientki a écrit :
In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very
convenient way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily
be transported to the pin-objects.

ad 2:
THAT's the problem: how do automate these lines "self.GND = self.pin[0]"

I'm also in for other solutions.

I'm afraid I don't understand your design (nor the domain FWIW). ditto
<snip>

But the whole thing still looks awfully convulted and kludgy to me,
and I suspect serious design flaws... Why don't you try and explain
your real problem, instead of asking how to implement what you *think*
is the solution ?
ditto

thank you all guys.
I'm just doing some exercises in Python,
and therefor I'm trying to rewrite some programs
I've written in other languages into Python.

The first program I tried to convert was a MatLab program,
which could almost be written identical in Python.
Even with my little experience in Python,
I can tell you that it could have been written in Python
in a much better way, but I deliberately did not,
to show other people who are familiar with MatLab.

The second example I'm now exercising with,
is a simulator of (electrical) circuits,
which I build in Delphi
http://oase.uci.kun.nl/~mientki/data_www/pic/jalss/jalss.html
 
S

Stef Mientki

Stef said:
Jussi said:
Bruno Desthuilliers kirjoitti:
Stef Mientki a écrit :
In the example below, "pin" is an object with a number of properties.
Now I want
1- an easy way to create objects that contains a number of these "pin"
2- an multiple way to access these "pin", i.e.
device.pin[some_index]
device.some_logical_name
ad 1:
a dictionary (as "pinlist" in the example) seems a very
convenient way (from a viewpoint of the device creator).
As you can see in the "__init__" section this dictionary can easily
be transported to the pin-objects.

ad 2:
THAT's the problem: how do automate these lines "self.GND =
self.pin[0]"

I'm also in for other solutions.

I'm afraid I don't understand your design (nor the domain FWIW). ditto
<snip>

But the whole thing still looks awfully convulted and kludgy to me,
and I suspect serious design flaws... Why don't you try and explain
your real problem, instead of asking how to implement what you
*think* is the solution ?
ditto

thank you all guys.
I'm just doing some exercises in Python,
and therefor I'm trying to rewrite some programs
I've written in other languages into Python.

The first program I tried to convert was a MatLab program,
which could almost be written identical in Python.
Even with my little experience in Python,
I can tell you that it could have been written in Python
in a much better way, but I deliberately did not,
to show other people who are familiar with MatLab.

The second example I'm now exercising with,
is a simulator of (electrical) circuits,
which I build in Delphi
http://oase.uci.kun.nl/~mientki/data_www/pic/jalss/jalss.html


Sorry, pressed send button too soon, so here it continuous:

In this exercise, I don't attempt to write "beautiful" Python code,
but the first thing is to write a simple "user-interface" for non-Pythians.
I understand that standardization about naming conventions is important,
but the purpose here is to serve the user, who has to write and
unerstand this,
therefore IORLW is in the domain always written in capitals,
spaces here makes it lot easier to compare the different actions,
in this domain we're used to 2 spaces etc.

def IORLW(S): S.Accu_ZERO ( S.Accu | S.lit [S.PC] )
def ANDLW(S): S.Accu_ZERO ( S.Accu & S.lit [S.PC] )
def XORLW(S): S.Accu_ZERO ( S.Accu ^ S.lit [S.PC] )
def SUBLW(S): S.AddLW (~S.Accu + 1 )
def ADDLW(S): S.AddLW ( S,Accu )


So to come back to my last question:
this is what the user has to do:
define devices with pins, and the actions what todo when input signals
changes.

class LED (device):
pinlist ={
# pin name type init-value other-parameters
1: ('Cathode', _DIG_IN, [], _par2),
2: ('Anode', _DIG_OUT, [], _par33)
}

Status = {True:('On'), False:('Off')}

def execute (self):
old = self.On
self.On = (~self.Cathode.Value & self.Anode.Value) > 0
if self.On <> old : print self.Name, self.Status[self.On]

And now this looks quit familiar,
although I miss the "with" statement of Delphi to make it even more
readable ;-)
like this

class LED (device):
pinlist ={
# pin name type init-value other-parameters
1: ('Cathode', _DIG_IN, [], _par2),
2: ('Anode', _DIG_OUT, [], _par33)
}

Status = {True:('On'), False:('Off')}

def execute (self):
with self:
old = On
On = ( ~Cathode.Value & Anode.Value) > 0
if On <> old : print Name, Status[On]


thanks again for all your wonderfull help,
cheers,
Stef Mientki
 
J

Jussi Salmela

Stef Mientki kirjoitti:
In this exercise, I don't attempt to write "beautiful" Python code,
but the first thing is to write a simple "user-interface" for non-Pythians.
I understand that standardization about naming conventions is important,
but the purpose here is to serve the user, who has to write and
unerstand this,
therefore IORLW is in the domain always written in capitals,
spaces here makes it lot easier to compare the different actions,
in this domain we're used to 2 spaces etc.

<snip>
> thanks again for all your wonderfull help,
cheers,
Stef Mientki

I can now understand the reason for the strange-looking function names
etc. and think it's reasonable. The application domain is totally alien
to me, though, so further explanations won't help me because a lack of
the necessary electronic knowledge.

Furthermore, as became evident from your web pages, your application is
quite extensive which makes your task more difficult in formulating
questions because things connected to other things are hard to extract
for review.

Anyway, happy hacking with Python.

Cheers,
Jussi
 
G

Gabriel Genellina

Stef Mientki ha escrito:
class LED (device):
pinlist ={
# pin name type init-value other-parameters
1: ('Cathode', _DIG_IN, [], _par2),
2: ('Anode', _DIG_OUT, [], _par33)
}

Status = {True:('On'), False:('Off')}

def execute (self):
old = self.On
self.On = (~self.Cathode.Value & self.Anode.Value) > 0
if self.On <> old : print self.Name, self.Status[self.On]

I don't know of what type are those values (certainly the're not []
because ~[] won't work). But note that using ~ with apparently logical
values doesn't work as expected. The operators &,|,^,~ are meant to be
used on integers, and work bit by bit. The operators and, or, xor, not
operate on logical, or boolean, values.

py> value = True
py> negvalue = ~value
py> if negvalue: print "oops!"
....
oops!
py> bool(negvalue)
True

If you want to express the condition "The led is ON when the value of
Anode is > 0 and the value of Cathode is < 0" that would be
self.On = self.Anode.Value>0 and self.Cathode.Value<0

but since I don't know the types of values involved I'm not sure if
this expression is right.
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top