-
-
Notifications
You must be signed in to change notification settings - Fork 659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python 3: work with different dictionary walking methods #9067
Comments
Hi, Additional notes:
Thanks. |
Hi, Actually, upon further reading, it appears Six module does not have an equivalent of dict.items/keys/values for Python 2 (returning lists). So it appears this must be one of those issues that should be solved as part of a master pull request that switches everything to Python 3 at once; this changes if we can find a workaround that preserves compatibility for a while. Thanks. |
There is a pretty straight forward workaround. Instead of
I'm not sure what you mean here. dict.items() isn't used within the config manager. iteritems is defined within AggregatedSection, however since AggregatedSection isn't a dictionary by itself, you can safely rename that iteritems methods to items, I think. Something that ought to be tested, though. |
Hi, regarding Config Manager, yep, thanks for correcting me on where to look for these methods. As for coercing an iterator into a list, perhaps one way is to just call list() constructor for now when encountering dict.items/keys/values, with Six module being used in other places. I hope we can find a more consistent solution for readability purposes. Thanks.
From: Leonard de Ruijter <[email protected]>
Sent: Thursday, December 13, 2018 11:47 PM
To: nvaccess/nvda <[email protected]>
Cc: Joseph Lee <[email protected]>; Author <[email protected]>
Subject: Re: [nvaccess/nvda] Python 3: work with different dictionary walking methods (#9067)
Actually, upon further reading, it appears Six module does not have an equivalent of dict.items/keys/values for Python 2 (returning lists). So it appears this must be one of those issues that should be solved as part of a master pull request that switches everything to Python 3 at once; this changes if we can find a workaround that preserves compatibility for a while.
There is a pretty straight forward workaround.
Instead of dict.iteritems(), use six.iteritems(dict)
Instead of dict.items(), use list(dict.items())
The latter will result into a list in python 3. In python 2, it will just pass a list to the constructor of list, which copies it. Not very efficient, but it will certainly suffice.
Alternatively, we can just use list(six.iteritems(dict))
2. The pull request regarding this feature won't apply to config.conf (Config Manager) because dict.iteritems and dict.items are used for different purposes unless @LeonarddeR says it can be safely merged somehow.
I'm not sure what you mean here. dict.items() isn't used within the config manager. iteritems is defined within AggregatedSection, however since AggregatedSection isn't a dictionary by itself, you can safely rename that iteritems methods to items, I think. Something that ought to be tested, though.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#9067 (comment)> , or mute the thread <https://github.com/notifications/unsubscribe-auth/AHgLkIyJRRfcn-TtYehFaMqWwWJU8l9Mks5u41dzgaJpZM4ZS05_> .
|
Hi, In the end, native methods will be used with config manager untouched. Thanks. |
…s a dictionary. Re nvaccess#9067.
Hi, For future reference: whenever one changes iter* to ones without, make sure to check the type of the class where the method is defined in (hence skipping ConfigManager class). Also, staring from today's commits, explanatory comments will be added (see #7105 comment from Reef for details). Thanks. |
…s and added explanatory comments. Re nvaccess#9067.
…cess#7105. Comment from Reef Turner (NV Access): add explanatory comment, prefixing the comment with '# Py3 review required'. Because some changes are based on another issue (nvaccess#9067, for example), prefix the review comment with issue number references as appropriate.
…ems call inside a list call. Re nvaccess#9067.
…on 3: wrap dict.keys inside a list call along iwth some explanantory comments. Re nvaccess#9067.
…hon 3: add explanatory comment on dict.values, along with wrapping one function inside a list call. Re nvaccess#9067.
…t call. Re nvaccess#9067. There are some parts of the source code that uses dict.items/keys/values. With conversion to Python 3, they will need to be wrapped inside a list call because of iteration versus list retrieval differences. These include command sequence in speech manager, UIA event keys, and others.
…alues -> dict.values. Re nvaccess#9067.
…all. Re nvaccess#9067. Modules such as UIA handler, settings dialogs and others use dict.items/keys/values. This means in Python 2, it returns a list, whereas it returns an iterator in Python 3. Therefore wrap these inside a list call to preserve semantics (also include notes for some of these).
…t.iterkeys -> dict.keys. Re nvaccess#9067.
…alues -> dict.values. Re nvaccess#9067.
…itervalues -> dict.values. Re nvaccess#9067.
…rvalues -> dict.values. Re nvaccess#9067.
… -> dict.values. Re nvaccess#9067.
…on 3 (#9671) * Various modules/Python 3: wrap dict.items/keys/values inside a list call. Re #9067. Modules such as UIA handler, settings dialogs and others use dict.items/keys/values. This means in Python 2, it returns a list, whereas it returns an iterator in Python 3. Therefore wrap these inside a list call to preserve semantics (also include notes for some of these). * NVDAObjects/Python 3: dict.iteritems -> dict.items. Re #9067. * IAccessible handler/Python 3: dict.iteritems -> dict.items. Re #9067. * UIA handler and utilities/Python 3: dict.iteritems -> dict.items, dict.iterkeys -> dict.keys. Re #9067. * Add-on handler/Python 3: dict.itervalues -> dict.values. Re #9067. * App module handler/Python 3: dict.iteritems -> dict.items, dict.itervalues -> dict.values. Re #9067. * Base object/Python 3: dict.iteritems -> dict.items. Re #9067. * Braille display detection/Python 3: dict.iteritems -> dict.items. Re #9067 * Braille display drivers/Python 3: dict.iteritems -> dict.items, dict.itervalues -> dict.values. Re #9067. * Character processing/Python 3: dict.iteritems -> dict.items, dict.itervalues -> dict.values. Re #9067. * Config/Python 3: dict.iteritems -> dict.items. Re #9067. * Global plugin handler/Python 3: dict.iteritems -> dict.items. Re #9067. * GUI/Python 3: dict.iteritems -> dict.items, dict.itervalues -> dict.values. Re #9067. * Input core/Python 3: dict.iteritems -> dict.items. Re #9067. * Installer/Python 3: dict.iteritems -> dict.items. Re #9067. * Oleacc/Python 3: dict.iteritems -> dict.items. Re #9067. * Speech/Python 3: dict.iteritems -> dict.items. Re #9067. * Speech XML/Python 3: dict.iteritems -> dict.items. Re #9067. * Synth drivers/Python 3: dict.iteritems -> dict.items, dict.itervalues -> dict.values. Re #9067. * Table utils/Python 3: dict.iteritems -> dict.items. Re #9067. * Text infos offsets/Python 3: dict.iteritems -> dict.items. Re #9067. * Touch tracker/Python 3: dict.itervalues -> dict.values. Re #9067. * VK codes/Python 3: dict.iteritems -> dict.items. Re #9067. * Watchdog/Python 3: dict.iteritems -> dict.items. Re #9067.
Fixed in #9671 |
Hi,
Continuing the journey toward Python 3:
Is your feature request related to a problem? Please describe.
When walking through a dictionary, Python 2 provides two iteration methods, whereas Python 3 only provides one. Specifically, Python 2 provides a method to return items/keys/values as a list or as an iterator, whereas Python 3 exposes the latter only.
Different parts of NVDA rely on behavioral differences between these two methods. For example, when looking up keys for input management (inputCore), iteritems is used, whereas when looking up voice names in synthesizers (part of synth settings ring), a list of values is used.
Describe the solution you'd like
There are two solutions:
Describe alternatives you've considered
None
Additional context
Not only this affects NVDA Core, parts of add-ons that iterate through a dictionary must be rewritten to handle behavior differences between Python 2 and 3. Add-ons community will be encouraged to adopt the solution from Core once a pull request for this item is merged into master branch.
Thanks.
The text was updated successfully, but these errors were encountered: