Coder Social home page Coder Social logo

Comments (5)

TheFern2 avatar TheFern2 commented on August 16, 2024

Hi @horus61 I've formatted your code properly. A few comments, don't use list in your code as list is a python keyword, use something like my_list or better yet name it accordingly like sensors or temp_sensors note the plural. Also don't forget to add a sleep in the while loop or your cpu will go 100% on usage. Also prob stay away from naming variables with one letter.

Here's some history on a similar topic #230
How I would solve it:

prev_ints = run_script()
while True:
    y = run_script()
    print(y)

    # compare lists here
    # if different, update prev_ints
    #   do something
    # else if equal, don't do anything

    sleep(3)

Luckily for us we have the Zen of Python https://peps.python.org/pep-0020/

There should be one-- and preferably only one --obvious way to do it.

And we have 30,000 ways to do a list comparison lol https://www.digitalocean.com/community/tutorials/how-to-compare-two-lists-in-python

I would probably use list comprehension for your use case since we do care about the order of the items in the list.

l1 = [10, 20, 30, 40, 50]
l2 = [50, 75, 30, 20, 40]

res = [x for x in l1 + l2 if x not in l1 or x not in l2]

print(res)
if not res:
    print("Lists l1 and l2 are equal")
else:
    print("Lists l1 and l2 are not equal")

If you need to know when an individual index change, then you could use zip:
https://docs.python.org/3/library/functions.html#zip

prev_ints = run_script()
did_change = False
while True:
    y = run_script()
    print(y)

    for val_y, val_p in zip(y, prev_ints):
        if val_y != val_p:
            print(f"The values {val_y} {val_p} aren't equal")
            did_change = True

    if did_change:
        prev_ints = y
        did_change = False

     sleep(3)

Here's a few videos I would watch on these topics:

from pylogix.

horus61 avatar horus61 commented on August 16, 2024

Thank you Fernando for your help and I bother you again for further advice. I would like to identify the elements of the list that have changed since the previous reading according to their index because I would like to create an alarm report.
Thanks for your cooperation, greetings from Italy. Nino
Sorry for my bad English.

from pylogix.

TheFern2 avatar TheFern2 commented on August 16, 2024

You're welcome, your English is great! If you take the previous example that compares each value, you just have to create a new list, no need to get the index. Take a look below.

# Initialization vars before loop is started
prev_ints = run_script()
did_change = False
changed_items = []  # <- New list
while True:
    y = run_script()
    print(y)
    # Loop through all values and compare them
    for val_y, val_p in zip(y, prev_ints):
        # Compare new value with prev value
        if val_y != val_p:
            changed_items.append(val_y)  # <- Add item to list
            print(f"The values {val_y} {val_p} aren't equal")
            did_change = True

    if did_change:
        prev_ints = y
        did_change = False
        # send report logic here  # <- New line

     sleep(3)

from pylogix.

horus61 avatar horus61 commented on August 16, 2024

HI. Thanks for the advice you give me. I wanted to show you the raspberry pi application that I would like to create to monitor the variables on the plc to integrate an alarm message with the sending of sms. This is an example with a variable simulator. I'm a newbie and it certainly doesn't look nice and you would do it more professionally.
But it represents what I would like to do. I thank you and greet you.

P2TestMainFern.txt

from pylogix.

TheFern2 avatar TheFern2 commented on August 16, 2024

@horus61 I recommend you check out our discord, someone there might be able to further guide you. The best advice I can give you is to start small. Build each step of what you are trying to accomplish one by one on their own and then mash them together in the end. Also if you seek help in the discord your might want to translate function names and variables to English, otherwise is hard to follow the code.

For example:

  • Read plc tags print them to the console
  • Compare tags in a while loop
  • Create fake alarm message to sms
  • put all 3 programs together for end result

from pylogix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.