Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

sort subfunction #737

Answered by ALERTua
melmager asked this question in Q&A
Discussion options

Logger: custom_components.pyscript.file.wall_box_start.get_new_price

Exception in <file.wall_box_start.get_new_price> line 22: daten.sort(key=sort_price) ^ TypeError: '<' not supported between instances of 'coroutine' and 'coroutine'

@service
def get_new_price():
 
 def sort_price(x):
 return x["price"]
 # snipp
 daten = [{'date': '2025-07-15T05:00:00.000Z', 'price': 29.36}, {'date': '2025-07-15T06:00:00.000Z', 'price': 28.58}] 
 daten.sort(key=sort_price)

and

def sort_price(x):
 return x["price"]
@service
def get_new_price():
 # snipp
 daten.sort(key=sort_price)

also fails

...

so how to do sub functions ?

bonus: how to check debug of integration ist on / off ?

You must be logged in to vote

lambda works fine:

@service
def get_new_price(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
 daten = [
 {'date': '2025-07-15T05:00:00.000Z', 'price': 29.36},
 {'date': '2025-07-15T06:00:00.000Z', 'price': 28.58}
 ]
 result = sorted(daten, key=lambda x: x['price'], reverse=False)
 log.debug(f"{__name__}:\n{str_.__class__.__name__} {str_}")

Replies: 2 comments

Comment options

lambda works fine:

@service
def get_new_price(trigger_type=None, var_name=None, value=None, old_value=None, context=None, **kwargs):
 daten = [
 {'date': '2025-07-15T05:00:00.000Z', 'price': 29.36},
 {'date': '2025-07-15T06:00:00.000Z', 'price': 28.58}
 ]
 result = sorted(daten, key=lambda x: x['price'], reverse=False)
 log.debug(f"{__name__}:\n{str_.__class__.__name__} {str_}")
You must be logged in to vote
0 replies
Answer selected by melmager
Comment options

The issue is that all functions in pyscript are async. Python's list sort() function requires key to be a regular function. You can accomplish that via a lambda (as @ALERTua did), or by adding @pyscript_compile as a decorator on your sort_price() function. See the 3rd bullet in this section of the docs.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /