pyscript icon indicating copy to clipboard operation
pyscript copied to clipboard

not implemented ast ast_starred

Open D4RGAN opened this issue 4 years ago • 3 comments

Hi I was trying to make a list of times for the @time_trigger which i could easily change with another function. So for example instead of writing @time_trigger("once(17:15:05)","once(16:00:00)","once(08:35:45)") i would have a list l = ["once(17:15:05)","once(16:00:00)","once(08:35:45)"] then i could use this list in my @time_trigger like this @time_trigger(l). This did not work because a String was wanted so after some googling I learned about unpacking and thought I found my solution (my programming knowledge is mostly from the internet, so I probabaly lack alot of basic understanding, sorry).

But when I tried @time_trigger(*l) I got the error "Exception in </config/pyscript/Test.py> line 47: @time_trigger(*l) ^ NotImplementedError: file.Test: not implemented ast ast_starred".

If I understand this unpacking is not implemented or am I doing somehting wrong? And would there be another solution to my problem?

Thank you for the help

D4RGAN avatar Dec 05 '21 16:12 D4RGAN

The @time_trigger decorator arguments are only parsed once when the function is defined. So, while the arguments to @time_trigger can be variables or expressions, they are only evaluated at the time the function is defined.

To make @time_trigger dynamic, you need to redefine the function with the new @time_trigger. That can be done using inner functions or closures. You can read about that here in the docs.

craigbarratt avatar Dec 05 '21 18:12 craigbarratt

@D4RGAN how did you implement your solution to this?

worgarside avatar Jul 08 '22 19:07 worgarside

@worgarside I did as craigbarratt told me and made a function factory. Looks like this:

time_triggers = []

def time_trigger_factory(input_datetime,func_name):
    #log.error(f"Creating a time trigger for {func_name} at {input_datetime} [HH:MM:SS]")
    @time_trigger(f"once({input_datetime})")
    def func_trig():
        globals()[func_name]()
    time_triggers.append(func_trig)

Initially I wanted one function with multiple triggers but I ended up creating multiple (same) functions which all do the same but have different timestamps. Maybe it would have been possible to alter @time_trigger(f"once({input_datetime})") so that it creates multiple triggers at once, but somehow I did it the other way around. But it still works, so I'm fine with it=)

D4RGAN avatar Jul 09 '22 14:07 D4RGAN

I pushed 5058fed to fix the expansion of *list in decorator function arguments. This should allow your original code to work.

craigbarratt avatar Jan 01 '23 08:01 craigbarratt

@D4RGAN could you please update or close the issue? Thank you

ALERTua avatar Sep 22 '23 09:09 ALERTua

@ALERTua yes of course. Sorry I tought the issue would be resolved without my doing.

D4RGAN avatar Sep 22 '23 09:09 D4RGAN