Renpy 8 TypeError: sort()

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 719
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Renpy 8 TypeError: sort()

#1 Post by Andredron »

Python 2.7 had a complex sort function that now crashes:

Code: Select all

    def mz_sortF(s1, s2):
        if s1.get_iz() < s2.get_iz():
            return 1
        elif s1.get_iz() > s2.get_iz():
            return -1
        else:
            if not s1.is_floor() and s2.is_floor():
                return 0
            elif s1.is_floor() and not s2.is_floor():
                return -1
            else:
                if s1.get_iy() < s2.get_iy():
                    return -1
                elif s1.get_iy() > s2.get_iy():
                    return 1
                else:
                    if s1.get_ix() < s2.get_ix():
                        return 1
                    elif s1.get_ix() > s2.get_ix():
                        return -1
                    else:
                        if s1.has("exit"):
                            return -1
                        elif s1.has("item"):
                            return -1
                        return 0

    def mz_sort(objects):
        objects.sort(mz_sortF)
        return objects
And this error pops up

Code: Select all

objects.sort(mz_sortF)
TypeError: sort() takes no positional arguments
Please tell me how to do this in python

And I can't find anything on google

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Renpy 8 TypeError: sort()

#2 Post by Ocelot »

https://docs.python.org/3/library/stdty ... #list.sort
sort(*, key=None, reverse=False)
[...]
sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):
[...]
The functools.cmp_to_key() utility is available to convert a 2.x style cmp function to a key function.
< < insert Rick Cook quote here > >

User avatar
Andredron
Miko-Class Veteran
Posts: 719
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Renpy 8 TypeError: sort()

#3 Post by Andredron »

Ocelot wrote: Fri Aug 12, 2022 5:10 am https://docs.python.org/3/library/stdty ... #list.sort
sort(*, key=None, reverse=False)
[...]
sort() accepts two arguments that can only be passed by keyword (keyword-only arguments):
[...]
The functools.cmp_to_key() utility is available to convert a 2.x style cmp function to a key function.
Thanks, the error is gone, but the function itself does not work. after sorting, all objects from the list disappeared completely

https://sun9-7.userapi.com/impg/q3cxO9I ... type=album



https://sun9-35.userapi.com/impg/_ePxlQ ... type=album



https://sun9-64.userapi.com/impg/r4RtHK ... type=album


in short, it doesn't work that way.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Renpy 8 TypeError: sort()

#4 Post by Ocelot »

Empty list is often sign of CPython internal error when operation on list. But in this case an exception should be thrown.
One reason for error might be non-symmetrical or non-transitive sorting function.

Code: Select all

            if not s1.is_floor() and s2.is_floor():
                return 0
            elif s1.is_floor() and not s2.is_floor():
                return -1
0 should be 1 here, or your function is non-symmetrical.

Code: Select all

                    else:
                        if s1.has("exit"):
                            return -1
                        elif s1.has("item"):
                            return -1
                        return 0
and that breaks symmetry even more.

if cmp(s1, s2) returns -1, then cmp(s2, s1) must return 1. Likewise if cmp(s1, s2) == 0 → cmp(s2, s1) should be 0 too. Now imagine what happens if for otherwise equal objects X has "exit", and Y does not:
mz_sortF(X, Y) returns -1, and mz_sortF(Y, X) returns 0.
< < insert Rick Cook quote here > >

User avatar
Andredron
Miko-Class Veteran
Posts: 719
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: Renpy 8 TypeError: sort()

#5 Post by Andredron »

Ocelot wrote: Fri Aug 12, 2022 6:03 am Empty list is often sign of CPython internal error when operation on list. But in this case an exception should be thrown.
One reason for error might be non-symmetrical or non-transitive sorting function.

Code: Select all

            if not s1.is_floor() and s2.is_floor():
                return 0
            elif s1.is_floor() and not s2.is_floor():
                return -1
0 should be 1 here, or your function is non-symmetrical.

Code: Select all

                    else:
                        if s1.has("exit"):
                            return -1
                        elif s1.has("item"):
                            return -1
                        return 0
and that breaks symmetry even more.

if cmp(s1, s2) returns -1, then cmp(s2, s1) must return 1. Likewise if cmp(s1, s2) == 0 → cmp(s2, s1) should be 0 too. Now imagine what happens if for otherwise equal objects X has "exit", and Y does not:
mz_sortF(X, Y) returns -1, and mz_sortF(Y, X) returns 0.
got it! I have ALL the code for the second python! so there is no int() for division. all objects stupidly flew off the screen, because the coordinates became relative.

Thank you very much functools.cmp_to_key() ! Got the code!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]