On 3/10/23 22:16, Thomas Passin wrote:
On 3/10/2023 7:07 PM, aapost wrote:
which does start to break down readability due to line length, as there isn't really an indention rule set for something uncommonly used.

but some renaming makes the pattern clearer

pids.update({"messages" :subprocess.Popen(["cmd1"])}) if not pids["messages"] else None, pids.update({"syslog" :subprocess.Popen(["cmd2"])}) if not pids["syslog"] else None, pids.update({"kern" :subprocess.Popen(["cmd3"])}) if not pids["kern"] else None, pids.update({"user" :subprocess.Popen(["cmd4"])}) if not pids["user"] else None,

I'd make the pattern in this example even more understandable and less error-prone:

def update_pids(target):
     cmd = ["tail", "-n", "1", "-f", f"/var/log/{target}"]
     pids.update({target: subprocess.Popen(cmd)}) if not \
         pids[target] else None

lambda x: ( # The Tk callback includes an event arg, doesn't it?
             update_pids('messages'),
             update_pids('syslog'),
             # etc
           )


So yeah, that's along the same lines. cmd could be in a function, or just outside the lambdas. Could also do from subprocess import Popen to shorten that, etc.

The .trace( on the tkinter Vars use events, so you have to do something like
lambda _a, _b, _c: stuff

But not in the above case. My focus these last couple week hasn't used any of those so the specifics aren't as fresh (still heading back in that direction), when required python will definitely let you know, lol.

The additional note in the above is, when taking the def route above, the thing you would have to consider is what scope is the dictionary pids?

Do you need to submit it to the lambda and subsequently the function such as
lambda pids=pids: (
    update_pids("messages", pids),
    update_pids("syslog", pids),

So that update_pids can access it? Or in your design do you have a separate management of it that def update_pids already has access to? (either is valid depending on design intent).

In the direction I am going, in trying to build some compound widgets, and I need to grab stuff from one widget and send it to another within the same widget group, so in cases where it goes to an external function, it has to be sent as an arg through the lambda whatever=whatever: ( function(whatever), )


--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to