-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add Attribute support for Part #128
Conversation
Nice! 🎉 @rnestler Since you initiated the entity classes, maybe you could take a look at this? Some random comments from my side:
|
Yes I understand, I guess the only real "foolproofness" is that we infer the Regarding the unsupported Another way of doing things would be to introduce already prefixed unit enums for all types, e.g. class Unit(EnumValue):
def get_name(self) -> str:
return 'unit'
class CapacitanceUnit(Unit):
PICOFARAD = 'picofarad'
NANOFARAD = 'nanofarad'
MICROFARAD = 'microfarad'
MILLIFARAD = 'millifarad'
FARAD = 'farad'
class CurrentUnit(Unit):
PICOAMPERE = 'picoampere'
NANOAMPERE = 'nanoampere'
MICROAMPERE = 'microampere'
MILLIAMPERE = 'milliampere'
AMPERE = 'ampere'
KILOAMPERE = 'kiloampere'
MEGAAMPERE = 'megaampere' and combining them with automatic # Type automatically gets infered by CurrentUnit
attr = Attribute("Max Current", Value("500"), CurrentUnit.MILLIAMPERE) Or if we really want to ditch the automatic type/unit inference: # No protection against mistakes like Type.CURRENT with CapacitanceUnit.NANOFARAD
attr = Attribute("Max Current", Value("500"), Type.CURRENT, CurrentUnit.MILLIAMPERE) Let me know what you think! 👍 |
To be honest, I'd go with the simplest approach: attr = Attribute("Max Current", Value("500"), Type.CURRENT, CurrentUnit.MILLIAMPERE) This variant: attr = Attribute("Max Current", Value("500"), CurrentUnit.MILLIAMPERE) is also not bad IMO, but somehow I don't like that it will implicitly assume type And compared to the currently implemented variant: attr = Attribute("Max Current", Value("500"), AttributeType.CURRENT, MetricPrefix.MILLI) the simplest approach of passing type+unit has the advantage that the actual unit is visible in the code (just No very strong opinion though 😉 |
I agree with your point. 👌 I reworked the API for simplicty and conciseness. We can now use the simplest approach, e.g. max_current_attr = Attribute("Max Current 2", Value("500"), AttributeType.CURRENT, CurrentUnit.MILLIAMPERE)
features_attr = Attribute("Features 2", Value("Suction Cap"), AttributeType.STRING, None) # unitless or a more type-safe and very concise approach (recommended), e.g. max_current_attr = CurrentAttribute("Max Current 1", Value("500"), CurrentUnit.MILLIAMPERE)
features_attr = StringAttribute("Features 1", Value("Suction Cap")) # unitless Both are equivalent and generate the the same output! All this while still being super easy to extend for future Attribute types, it is just a matter of adding the right Feeling pretty confident with this one 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few things:
Tests are missing. The example you provided looks like a good test case.
Maybe moving these new classes into common.py since all other common classes are there too?
I think having a separate attribute
module is fine as well. Maybe we could even split up the common
module in the future to have a bit more structure.
Maybe we should consider using string literal types? https://mypy.readthedocs.io/en/stable/literal_types.html
This would make it a bit easier in the API IMO:
AttributeType = Literal['voltage', 'string', 'current', ...]
CurrentUnit = Literal['microampere', 'milliampere', ...]
max_current_attr = Attribute("Max Current 2", Value("500"), 'current', 'milliampere')
max_current_attr = CurrentAttribute("Max Current 1", Value("500"), 'milliampere')
But maybe the "strongly typed" way with Enums provides better auto completions?
Edit: Also regarding literal types I noticed that we couldn't use the EnumValue
base class anymore, so it's probably not feasible.
@rnestler Thank you for your review! 😄 I added a few tests to the PR.
👍
I like the ergonomics of the string literal types, but as you said we would lose the max_current_attr = CurrentAttribute("Max Current 1", "500", CurrentUnit.MILLIAMPERE) instead of max_current_attr = CurrentAttribute("Max Current 1", Value("500"), CurrentUnit.MILLIAMPERE) Let me know if we can proceed with these changes! 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When CI is fixed this should be good to go. Thanks a lot!
Co-authored-by: Raphael Nestler <[email protected]>
a6759c7
to
ba28cbb
Compare
I squashed some commits which were just fix ups and merged 🙂 |
UPDATE: API was reworked, please scroll down for more info
This PR aims to integrate
Attribute
Entities into thePart
API for generators. The API is small, simple and flexible while leaving little room for mistakes by the generator dev (like wrongUnit
forType
, e.g.Type.CURRENT
forUnit.HENRY
) and should tie seamlessly into the existing Entities/Expressions. Furthermore, it could provide a basis for discussion about this feature in general 💪Need for this feature arose from #127 (comment)
Example:
Output:
As always any feedback welcome! 😄