still struggling, howto use a list-element as a name ? Sory, hit send button to early

Stef Mientki S.Mientki-nospam at mailbox.kun.nl
Sat Jan 6 17:56:43 EST 2007


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



More information about the Python-list mailing list