question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Reusable Functions

See original GitHub issue

I tend to reuse the same logic for various rooms, the only difference is the entities that get watched and acted on. This is also VERY useful for sharing “apps” that are configurable.

However, I can’t seem to find a nice way to make code reusable in pyscript.

For instance, this was the closest I could come, but it still doesn’t work:

def follow_me(leader=None,followers=[]):
    log.error('creating trigger for {}'.format(leader))

    @state_trigger("True or {}".format(leader))
    def follow_me_trigger():
        log.error('follow me triggered for {}'.format(leader))
        value = state.get(leader)
        if value == 'on':
            for item in followers:
                homeassistant.turn_on(entity_id=item)
        else:
            for item in followers:
                homeassistant.turn_off(entity_id=item)

    return follow_me_trigger

follow_me('input_boolean.test_1', ['input_boolean.test_2'])
follow_me('input_boolean.test_3', ['input_boolean.test_4'])

Is there a recommended way to do this already? If not, would you consider adding such a feature?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
craigbarrattcommented, Sep 21, 2020

I just pushed a commit 8834dc1 that should allow trigger closures to work. Closures were also broken, so I fixed those with an earlier commit.

The way it works is very similar to what you wrote. However, the function trigger is destroyed if the function itself no longer has any reference to it in any scope. So in your example, you will want to assign the return value of follow_me to a variable:

f1 = follow_me('input_boolean.test_1', ['input_boolean.test_2'])
f2 = follow_me('input_boolean.test_3', ['input_boolean.test_4'])

If you re-assign, or delete, f1 or f2 then that trigger function will be destroyed. That’s consistent with the current behavior, which only worked in the global context - if you redefine a function in the global context, the old one is destroyed.

You could put all the closure functions into a list if that is easier - all that matters is there is some reference to the function for it to survive, eg:

trigger_funcs = [
    follow_me('input_boolean.test_1', ['input_boolean.test_2']),
    follow_me('input_boolean.test_3', ['input_boolean.test_4']),
]

or

trigger_funcs = []
for i in range(1, 4, 2):
    trigger_funcs.append(follow_me(f'input_boolean.test_{i}', [f'input_boolean.test_{i+1}'])

Caveat - the cumulative changes were pretty significant, and it’s only minimally tested. But please try the master version and see if it works for your use case.

1reaction
craigbarrattcommented, Sep 18, 2020

Ah - great point. There isn’t a good way to do this currently. I do like your use of a closure, and as you note that doesn’t work currently. Let me think about how to best support this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Feature] Reusable Functions #5186 - appsmithorg/appsmith
Currently, we can create and reuse API calls between various widgets. The ability to create generic functions bound to a page or app...
Read more >
Write More Code & Create Reusable Functions with Our ...
This new feature enables users to write reusable JavaScript variables and functions as JS Objects. These JS Objects can be used anywhere ...
Read more >
Functions — reusable blocks of code - Learn web development
Another essential concept in coding is functions, which allow you to store a piece of code that does a single task inside a...
Read more >
Chapter 4. Code Reuse: Functions and Modules
Reusing code is key to building a maintainable system. And when it comes to reusing code in Python, it all starts and ends...
Read more >
Reusable Functions In Postman - YouTube
A quick hack to reusing functions in Postman and keeping your code DRY. ... Send HTTP requests from scripts Postman. Valentin Despa.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found