472,331 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

overloading for ladder logic

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?
Nov 7 '08 #1
2 2864
On Nov 7, 7:48*am, jim...@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?
This reminds me of a little project I wrote a long time ago to use
operator overloading to compute the overall resistance of a network of
resistors - I used - for series connections and | for parallel. You
can see the code here: http://pastebin.com/m1e89aae9

If you are going to design a mini-DSL using overloading, you'll first
have to choose which operators correspond to your syntax, working
within those offered by Python. For NOT, you have only have two unary
operators to choose from, ~ (__invert__) or - (__neg__). / is not
supported as a unary operator, only as binary division (__div__ or
__truediv__). For AND and OR, you have a wealth of binary operators
from which to pick. But first, think about any precedence of
operations. As I recall from my ladder diagramming days, logic was
strictly left-to-right, with no precedence for one operation over the
other (as opposed to common arithmetic operator precedence, in which
in 4+2*3 evaluates as 4+(2*3), as opposed to (4+2)*3, which would be
strict left-to-right evaluation). When you implement your DSL, you
will still be subject to Python's definitions for operator
precedence. So if you want strict left-to-right evaluation, then
choose two operators at the same level of precedence, such as + and -,
or * and /. On the other hand, if you want AND evaluated before OR
(which is typical precedence in programming), then pick operators from
two different levels of precedence.

-- Paul

Nov 7 '08 #2
On Nov 7, 7:48*am, jim...@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
Nov 7 '08 #3

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

Similar topics

17
by: Terje Slettebų | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I...
20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==,...
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why...
0
by: sachin | last post by:
How to read Ladder diagrams using C#? Is there any way to insert ladder diagrams in .rtf file? please guide
4
by: JP Wrye | last post by:
Hello All, I'm wondering if the following is possible. I'm explicitly overloading SqlParameter, but get the following error when I try to...
1
by: Jack Addington | last post by:
I have create a base class for a user control as well as a related base class for its logic (nonvisual). As part of the non-visual class I have a...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would...
35
by: josh | last post by:
Hi, I coded the following but It does not return what I expect, why? #include <iostream> using namespace std; class Other { public: int i;
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.