Python Forum
list of lists iterate only sends the last value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list of lists iterate only sends the last value
#1
Hey, im new to python.

Im using python3 with raspberrypi3 and GPIO. i have this code which supposed to iterate on list of lists :

#json file goes to python as s.settings
    "gpio": [
    {
        "pin":21,
        "type": "input",
        "services": ["gps_mon"],
        "status": false
    },
    {
        "pin": 27,
        "type": "input",
        "services": ["ntp_mon"],
        "status": false
    }]
And this is the code :
 serviceCallbackList = s.settings["watchdog"]["services"] #gets all watchdog services

    def button_callback(flagInPin):
        s.print_info("Button was pushed from pin", flagInPin)
        for item in serviceCallbackList:   # checks if the services i set in the GPIO list is 
            if item in flagInService:      # in the watchdog services and delete the key if so.
                del serviceCallbackList[flagInService]

    for n in s.setting["gpio"]:
        try:
            if n["type"] == "input":
                flagInPin = n["pin"]
                flagInService = n["services"]
                GPIO.setup(flagInPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
                GPIO.add_event_detect(flagInPin, GPIO.RISING, callback=button_callback)
        except Exception as e:
            print("No Input settings", e)
My problem is that "GPIO.add_event_detect" has to send to button_callback() function the
flagInPin value ( which works) but i need it to have the flagInService to. because it will send me the last value which not match the pin that was activated, only the last value the loop went through, and i cannot send GPIO.add_event_detect with another value.

Any ideas?
I just want function to know that flagInPin has to match with flagInService that it comes from the same list.

Thanks.
Reply
#2
If I understand it correctly, you need to pass the flagInService variable to the callback. It seems to me that you can do it by using the 'partial' operation

from functools import partial
def button_callback(flagInService, flagInPin):
    ...
then
...
    GPIO.add_event_detect(flagInPin, GPIO.RISING, callback=partial(button_callback, flagInService))
Another issue is that in button_callback(), you're deleting items from serviceCallbackList while iterating on this list. It doesn't work in python. You could avoid this by writing
for item in list(serviceCallbackList): # make a copy
    ...
also shouldn't it be del serviceCallbackList[item] ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to make bot that sends instagram auto password reset code kraixx 2 1,355 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  List all possibilities of a nested-list by flattened lists sparkt 1 912 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,064 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,057 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,611 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,113 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,853 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,073 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 3,391 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 11,090 Apr-13-2021, 11:59 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020