Python Forum
Updating Code And Having Issue With Keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Updating Code And Having Issue With Keys
#1
Hello, I'm currently updating some code from a plug-in for Maya (the animation software) from Python 2 to Python 3 and noticed I was having some issues with keys... specifically I am getting an error message saying " dict_keys' object has no attribute 'sort' "

I wound up tracing this to code on line 191-192 which states:

shapes = CONTROL_SHAPES.keys()
shapes.sort()
I am struggling to figure out what is going on? I have looked on a few forums and the closest thin to an answer I have found so far was to retype 191 as:

shapes = list(CONTROL_SHAPES.keys())
However, that has not yielded any results for me. Does anyone here have any idea as to what part of this code might need to be updated or how I might go about it?
Reply
#2
In python2 dict.keys(), dict.values() and dict.items() return list objects. In python3 they return dict view objects that are lazily evaluated and thus don't have .sort() method.

I don't know what you mean by

(May-23-2023, 04:51 AM)Xileron Wrote: that has not yielded any results for me.
What you did is one option (i.e. turning the view object into a list and thus getting the same as in python2), but depending on what you do with shapes down the line there might be better approach, i.e. without constructing the whole list in memory
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This should work with Python 2.7 and 3.x:

shapes = sorted(CONTROL_SHAPES.keys())
buran and Larz60+ like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
I am merely updating some code that was stuck in Python 2. I'm happy to send the file over if you would like to see the scripts I'm referencing in greated detail if need be though.

Also, I tried the write shapes = sorted(CONTROL_SHAPES.keys()) however this has not worked out on my end.
Reply
#5
I meant that this had not worked for me. It had not fixed what was wrong with the code and I am still getting the same error message:

'dict_keys' object has no attribute 'sort'
Reply
#6
Try something along the lines of
shapes = list(CONTROL_SHAPES.keys())
Reply
#7
Wait, I apologize. @DeaD_EyE's approach actually was the solution. I found out I just needed to close and open Maya for it to register the changes in the code. Thanks for all the help
Reply
#8
(May-24-2023, 03:54 AM)Xileron Wrote: I found out I just needed to close and open Maya for it to register the changes in the code.
I guess your approach didn't work for the same reason
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(May-23-2023, 04:51 AM)Xileron Wrote: Hello, I'm currently updating some code from a plug-in for Maya (the animation software) from Python 2 to Python 3 and noticed I was having some issues with keys... specifically I am getting an error message saying " dict_keys' object has no attribute 'sort' "

I wound up tracing this to code on line 191-192 which states:

shapes = CONTROL_SHAPES.keys()
shapes.sort()
I am struggling to figure out what is going on? I have looked on a few forums and the closest thin to an answer I have found so far was to retype 191 as:

shapes = list(CONTROL_SHAPES.keys())
However, that has not yielded any results for me. Does anyone here have any idea as to what part of this code might need to be updated or how I might go about it?

The issue you're facing is related to changes in Python 3's dictionary behavior. In Python 3, the keys() method of a dictionary returns a dict_keys object, which is a view object that provides a dynamic view of the dictionary's keys. The dict_keys object does not have a sort() method because it does not support in-place sorting.

To resolve this issue and sort the keys, you can convert the dict_keys object to a list using the list() function, and then call the sort() method on the list. Here's an updated version of your code that should work in Python 3:

shapes = list(CONTROL_SHAPES.keys())
shapes.sort()

Alternatively, you can use the sorted() function, which returns a new sorted list without modifying the original dict_keys object. Here's how you can achieve the same result using sorted():

shapes = sorted(CONTROL_SHAPES.keys())

Both of these approaches will give you a sorted list of keys from the CONTROL_SHAPES dictionary in Python 3.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating nested dict list keys tbaror 2 1,295 Feb-09-2022, 09:37 AM
Last Post: tbaror
  NameError issue with daughter's newb code MrGonk 2 1,462 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Calculator code issue using list kirt6405 4 2,299 Jun-11-2021, 10:13 PM
Last Post: topfox
  updating certain values in dict. with relation to their keys malevy 17 5,349 Nov-27-2019, 02:37 PM
Last Post: buran
  Issue with code for auto checkout nqk28703 2 2,183 Nov-01-2019, 09:33 AM
Last Post: nqk28703
  code issue sandy 1 1,762 Mar-14-2019, 07:16 PM
Last Post: micseydel
  Visual Studio Code - PEP8 Lambda Issue Qui_Ten 1 2,741 Jan-28-2019, 08:17 AM
Last Post: buran
  Wine / liquor dispenser project code issue onlinegill 0 2,151 Nov-20-2018, 10:41 PM
Last Post: onlinegill
  issue with updating list every iteration of a loop ftrillaudp 2 3,083 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  Issue using cv2.cv in code Huzefa95s 7 6,887 Oct-28-2018, 06:11 AM
Last Post: Huzefa95s

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020