-1

I am new to python and i am having a script which i try to run it with Python 3.5 and i got this error

priorities[i].sort(cmp=comparer)
TypeError: 'cmp' is an invalid keyword argument for this function

Here is the function that has the error from the script

def comparer(first, second):
    """Compare function for message priorities in maintenance mode"""
    return cmp(int(first["Priority"]), int(second["Priority"]))

def get_sequence(inputs, fcm):
"""Creates right sequence for the maintenance mode"""
flags = count_flags(inputs)
sequence = []
for index, where in [(0, '"Axis"="Elevator"'), 
                     (1, '"Axis"="Aileron"'),
                     (2, '"Axis"="Rudder" or "Axis"="HS-ACE"')]:
    if len(flags[index]) == 0:
        sequence += WRITE_SEQUENCE_MAINT_0.Get(where)
    elif len(flags[index]) == 1:
        sequence += WRITE_SEQUENCE_MAINT_1.Get(where)
    elif len(flags[index]) > 1:
        sequence += WRITE_SEQUENCE_MAINT_2.Get(where)
    else:
        raise NotImplementedError("This number of flags does not make sense")

priorities = [[], [], []]
for flag in flags[0] + flags[1] + flags[2]:
    lines = WRITE_PROCESSING_MSGPRIORITY.Get('"Message Enable Flag"="%s" and "FCM Instance"="%s"' % (flag, fcm))
    if len(lines) > 0:
        priorities[{"Elevator":0, "Aileron":1, "Rudder":2}[lines[0]["Axis"]]].append(lines[0])
for i in range(3):
    priorities[i].sort(cmp=comparer)

for message in sequence:
    if "Second Highest Priority" in message["Message"]:
        new_m = priorities[{"Elevator":0, "Aileron":1, "Rudder":2}[message["Axis"]]][1]
        message["Message"] = new_m["Message"]
        message["Lane"] = new_m["Lane"]
        message["EP Bit"] = new_m["EP Bit"]
    elif "Highest Priority" in message["Message"]:
        priorities_for_rudder = priorities[{"Elev":0, "Ail":1, "Rud":2}[message["Axis"]]]
        if len(priorities_for_rudder) > 0: # This rudder message does not exist
            new_m = priorities_for_rudder[0]
            message["Message"] = new_m["Message"]
            message["Lane"] = new_m["Lane"]
            message["EP Bit"] = new_m["EP Bit"]
        else:
            message["Message"] = "Rudder Filler Message"
            message["Lane"] = "COM"
            message["EP Bit"] = "0"
return sequence

This script was written in python 2.7.

4
  • 1
    cmp was removed from sort in python 3, you have to use key instead. here is a complete explanation Commented May 28, 2019 at 12:22
  • Also, what is comparer? You'll have to write a function equivalent for it using key argument. Commented May 28, 2019 at 12:23
  • docs.python.org/3/library/stdtypes.html#list.sort Commented May 28, 2019 at 12:23
  • @ParitoshSingh i updated the code Commented May 28, 2019 at 12:27

1 Answer 1

1

There is no longer a cmp argument for sorting in python 3.

Use a key, and a function that returns a single value to be mapped for comparison.

Instead of comparer, you can use this function.

def comparer_key(item):
    """Compare keys for message priorities in maintenance mode. Returns a key."""
    return int(item["Priority"])

And then,

for i in range(3):
    priorities[i].sort(key=comparer_key)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.