ADR suggestion: name, EasyDynamicsList and ComponentCollection
#186
Replies: 2 comments 2 replies
-
|
Following a lengthy discussion we had IRL about the root of these requirements, from my understanding: The user needs to have unique names to correctly refer components in components collection. I think that the user would make uniquely looking names anyway, unless they really really want to complicate their life. I suggest that you just let those who want to make things difficult for themselves to do so. There will be immediate feedback that they can fix with another rename. IMHO It's not worth to introduce complicated logic to guard weird user agency if it does not lead to erroneous state. It is similar to the following standard lib code: If i see someone doing that and then complaining that Stuff below is just me trying to rationalize the argument above, feel free to ignore it. I include it. We essentially have few cases, when that it important: When user creates the collection and tries to put non-uniquely named components into it - the visual indication on, say, legend of a plot would give the user feedback that they can't actualy refer to them and the user can now just rename one component to make it usable. It was their own decision to make that weird and they can easily fix that. The second one resembles the first one This is again weird agency, but easy to fix by the user anway based on visual indication of, what essentially is The last one is an edge case. Raising exception on the rename requires complicated logic. Essentialy, for the sake of ensuring something that the user whould naturally want and make themselves anyway. Without the guard, the rename might make a hard-to-read state somewhere else but only if the user initially makes a complex composition of components in the first place. My expectation would be, that if I'd create a complex composition of components, I would want to name the components such that I could easily categorically differentiate between them, meaning that even if I use g2 in different places, I wouldn't want to rename it to g1, where g1 is not present. It would be weird user agency, but with clear feedback when they need it and easily fixable. |
Beta Was this translation helpful? Give feedback.
-
|
So the main problem is really: I would object to Python list
collection['gaussian'].width = 3.0will silently return the first component only, if there is more than one 'gaussian' What I would suggest is to not implement name-locking or global tracking. No component should need to know which collections contain it. Instead, why not just be permissive with names but balk/raise on the actual query? g1 = Gaussian(name="g")
g2 = Gaussian(name="g")
col = ComponentCollection([g1, g2])
col["g"] # raises AmbiguousNameError
col[0] # works
col[1] # worksWe could access the components with additional helper methods like collection[0] # index lookup
collection["gaussian1"] # name lookup if unambiguous
collection.names # diagnostic
collection.duplicate_names() # diagnostics for dupesThis makes the implementation MUCH less complex/error prone with no locking or having the components know their parents/containers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
General
I want to be able to select components (and other things, but this is about components) by their name instead of their index. For example
(Obviously this particular example isn't very useful, but it's just to show the idea).
unique_namedoes not work for this, since it is not copied. So I need a name that is unique only to the list, also as discussed in easyscience/core#194.To do this, I need to create
EasyDynamicsList(which inherits fromEasyList) usingnameinstead ofunique_namefor the lookup, and letComponentCollectioninherit from it.A draft is found in #185
NameMixinI now have three classes that need
name:EasyDynamicsBase(inheriting fromNewBase),EasyDynamicsModelBase(inheriting fromModelBase) andEasyDynamicsList(inheriting fromEasyList). They all needname, so I make aNameMixin.People should in general be allowed to assign their components a new name. However, since
namehas to be unique within a list, they should not be able to assign it to a name that is already in the list. The difficulty here is that a component can in principle be a member of an arbitrary number of lists.Checking that a new name doesn't clash with all lists is a pain to implement and not worth it. Instead, I will lock the name if it's in a list, and unlock it if it's removed from the list.
The lock status will be an integer and not a boolean. If an object is added to two lists, the lock value will therefore be 2. If the object is removed from one list the lock value is reduced by 1. The name can only be changed if the lock value is 0.
EasyDynamicsListInherits from
EasyList.Needs to overwrite the
insert,append,extend,__setitem__with validation thatnameis unique to the list and locking the name, andpop,__delitem__with unlocking the name._get_keywill also be overwritten to use name.ComponentCollectionInherits from
EasyDynamicsListandEasyDynamicsModelBase(forunitandname).protected_typeswill beModelComponent. Some methods need to be updated, and a lot of code needs to be updated to handle the new API.Beta Was this translation helpful? Give feedback.
All reactions