-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
preserving the order of Traitlets as defined in the class #148
Comments
I may be wrong, but I have a feeling this isn't likely to be something that would be included in vanilla Traitlets. However, #131 concerns generating a docstring in |
This could be done very easily in Python 3 by implementing a prepare on MetaHasTraits that returns an OrderedDict. In Python 2 I don't think there's a straightforward way to do this.
|
At the moment we are trying to support both 2 and 3 so that may be an issue, at least for a couple years. Alphabetical is easy to do, but isn't usually what the user wants. They usually want to see related parameters close together, and typically more frequently changed parameters higher in the list. All this relates to our need to serialize and de-serialize the parameter information to and from files that users want to view and edit (this is a very common interface in astronomy for running applications). |
If you want to support Py2 then you'd have to construct your traitlets ahead of time and then dynamically add them to the body of the class, ala:
This might be a reasonable thing to do if you're defining all your traited classes ahead of time, and you don't expect users to have to extend your traited classes. If you're expecting your users to be participants in the class hierarchy I expect that it wouldn't be with the added confusion and complexity. |
How do you plan on controlling order with traits inherited from other classes? It seems like you need a system of priority and grouping that is independent of the defined order of traits on any particular class to resolve that. I think your best bet is to just |
Adding a ordering keyword is pretty clumsy and I don't think it would be acceptable. Well, there's always the source code to self analyze :-) |
It could also be a dictionary defined on the class which is keyed on trait names, and whose values are their order. Then you could run through that dictionary and |
Any idea of how much work to add an alternate class HasOrderedTraits? |
@perrygreenfield in Python 3, it's pretty trivial. As an example, here's an implementation that provides an
Running that script produces this as output:
The only thing that would probably still need some fleshing out here would be merging these attributes in subclasses. In Python 2, there's no way to accomplish this that I'm aware of, because by the time our metaclass gets access to the class body, it's already a dict with no implied order. If you wanted to replicate this behavior in Python 2, you'd have to require to user to supply an ordering, which seems like the whole thing you're trying to avoid here. |
Thanks for taking the time to explain. It saved me quite a bit of time trying to distill that aspect. |
Closing given that support for Python 2 ends in 2020, the solution proposed by @ssanderson for Python<3.6, and that this is no longer an issue in Python>=3.6 due to PEP-520. |
For our application, it would be extremely useful to retain the order of the traits as defined internally. I.e., we would like to determine the order the traits were defined in the class. Any way of supporting this other than our making our own modified copy of traitlets? (The motivation for this is so that tools that can print out or otherwise save this information in a readable state can retain logical groupings of traits as defined in the class).
The text was updated successfully, but these errors were encountered: