473,837 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2954
On Nov 7, 7:48*am, jim...@gmail.co m 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.co m 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
4733
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 it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
34
6480
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 can't figure out how to overload the assignment operator. Here's what I'm trying to do. I've defined class Complex as class Complex { friend ostream &operator<<( ostream &, Complex & ); public: Complex( float = 0.0, float = 0.0 );
20
1851
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 >, <, ==, !=, >=, and <= operators using bool. I am having a bit of a problem in seeing what needs to happen here. I am just not sure how I do this. Will the overloading function recognize a < and a usual <? Do I do an IF (a.letters < b.letters){ return true }
39
2192
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 wasn't it introduced to the ANSI? 3. Is there any C implementation supporting this feature? I assume some of you will claim that there is no need in function overloading, so I would like to know your arguments too. Thanks,
0
1648
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
1290
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 convert to and from arrays. Cannot convert type 'EntityPersistence.DataFieldParameter' to 'System.Data.SqlClient.SqlParameter' Thanks,
1
1395
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 variable that references the base user object and a property to set it. class VisualBase : ...Control { ... } class LogicBase { private VisualBase _vObj; public VisualBase VObj {get;set;}
4
2630
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 really like to be able to use the succinct shell syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble with operator precedence that I do not know how to overcome.
35
2079
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
9827
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9678
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10609
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10263
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6987
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5838
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4468
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3120
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.