overloading for ladder logic

Aaron Brady castironpi at gmail.com
Fri Nov 7 12:45:08 EST 2008


On Nov 7, 7:48 am, jim... at gmail.com wrote:
> I am trying to simulate the execution of some PLC ladder logic in
> python.
>
> I manually modified the rungs and executed this within python as a
> proof of concept, but I'd like to be able to skip the  modification
> step.  My thought was that this might be able to be completed via
> overloading, but I am not sure if (or how) it could be done.
>
> overloadings:
>     + ==> OR
>     * ==> AND
>     / ==> NOT
>
> Example original code:
>      A=/B+C*D
> translates to:
>     A=not B or C and D
>
> I tried
>     def __add__ (a,b):
>         return (a or b)
>
> which gives me this:
>
>     >>> x=False
>     >>> y=True
>     >>> x+y
>         1
>     >>> x=True
>     >>> x+y
>         2
>
> How can this be done?

Here is an example, but Paul is right.  There's no way to customize
precedence.

>>> class Opand:
...     def __add__( self, other ):
...             return self.val or other.val
...     def __init__( self, val ):
...             self.val= val
...
>>> a= Opand( True )
>>> b= Opand( False )
>>> a+ b
True
>>> a= Opand( False )
>>> a+ b
False



More information about the Python-list mailing list