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

rzed rzantow at gmail.com
Sat Jan 6 14:47:43 EST 2007


Stef Mientki <S.Mientki-nospam at mailbox.kun.nl> wrote in
news:b7908$459fef43$d443bb3a$26766 at 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]



More information about the Python-list mailing list