473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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] )
Jan 6 '07 #1
12 1301
Stef Mientki <S.**************@mailbox.kun.nlwrote in
news:b7***************************@news.speedlinq. nl:
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]
Jan 6 '07 #2
>class Power_Supply(device):
> pinlist = {
0: ('GND', _DIG_OUT, _par2),
1: ('VCC', _DIG_OUT, _par33)
}

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
Jan 6 '07 #3
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 ?
Jan 6 '07 #4
Stef Mientki <S.**************@mailbox.kun.nlwrote in news:182cf
$4**********************@news.speedlinq.nl:
>>class Power_Supply(device):
pinlist = {
0: ('GND', _DIG_OUT, _par2),
1: ('VCC', _DIG_OUT, _par33)
}

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.
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.

--
rzed
Jan 6 '07 #5
Stef Mientki a écrit :
rzed wrote:
(snip)
>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),
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.
As this is part of the "user interface",
(I want that completely unknown with Python people,
write these lines),
Jan 6 '07 #6
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

Jan 6 '07 #7
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
Jan 6 '07 #8
Jussi Salmela wrote:
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...lss/jalss.html


>
Cheers,
Jussi
Jan 6 '07 #9
Stef Mientki wrote:
Jussi Salmela wrote:
>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...lss/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
Jan 6 '07 #10
Bruno Desthuilliers <bd*****************@free.quelquepart.frwrote
in news:45*********************@news.free.fr:
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
D'oh! Of course! Thank you.

--
rzed
Jan 7 '07 #11
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
Jan 7 '07 #12
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.

pyvalue = True
pynegvalue = ~value
pyif negvalue: print "oops!"
....
oops!
pybool(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.

--
Gabriel Genellina

Jan 7 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
1
by: Wade Beasley | last post by:
I am still struggling with this problem of getting my 2 list boxes to show the initials items as being selected on the first time that I go to the Tab with the boxes on them. I am reading from my...
0
by: Jonathan Wilson | last post by:
Firstly, to get msvcrt.lib, install the .NET framework SDK. The version of msvcrt.lib included there is the exact same one as comes with Visual Studio ..NET 2003. There are some other things that...
6
by: Wijaya Edward | last post by:
Hi, How can I pass Array, Hash, and a plain variable in to a function at the same time. I come from Perl. Where as you probably know it is done like this: sub myfunc {
7
by: dmitrey | last post by:
howto make Python list from numpy.array? Thx, D.
8
by: dmitrey | last post by:
howto check does module 'asdf' exist (is available for import) or no? (without try/cache of course) Thx in advance, D.
11
by: briankind | last post by:
Hello i have these radio buttons and i wish to have the corresponding textboxes to be visible when the radiobutton is selected. any Help? snippets. thanks thanks in adv Bry
1
by: malick | last post by:
Hello it developers, I'm using PHPExcelReader which I downloaded from sourceforge (http://sourceforge.net/projects/phpexcelreader) To read excel xls files in php. In the file reader.php (...
4
by: dmitrey | last post by:
hi all, howto split string with both comma and semicolon delimiters? i.e. (for example) get from string "a,b;c" I have tried s.split(',;') but it don't work Thx, D.
1
by: dmitrey | last post by:
hi all, howto check is function capable of obtaining **kwargs? i.e. I have some funcs like def myfunc(a,b,c,...):... some like def myfunc(a,b,c,...,*args):... some like
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.