-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Making Troposphere More Pythonic #1320
Comments
@DrLuke , @xyvark , @grkking, @markpeek It built a thin wrapper layer on top of troposphere using And it comes with a feature called auto-reference/associate. You don't need to remember the detailed properties and the reference syntax to reference each other, you just use this, it automatically does the work for you: associate(lambda_func, iam_role)
associate(lambda_func, subnet1)
associate(lambda_func, subnet2)
associate(lambda_func, security_group) So it gives you auto suggestions on type and properties name. |
@MacHu-GWU Please don't hijack issues like this just to advertise your own project. It's off-topic. |
@DrLuke OK, I mean this is the easiest way I can think of to make it more pythonic. |
I would like to propose an update to the auto-generated classes of troposphere, specifically to improve their usability in modern IDEs. Additionally this allows for easy value validation, which can be defined during code generation, removing the need to manually edit generated code after the fact.
These changes are part of PR #1301 and related to issue #1295.
The current state
Currently the following class code is generated (python 2):
Subproperties are added via the class-dict
props
. It tells the type this property should have, as well as if it's required. Using this data, the init method can read the**kwargs
to populate the values or you can set the attributes after object initialization.The problems with this are:
__init__
doesn't contain all properties in the signature. Autocompletion thus won't show you all possible kwargs you could set.My proposal
As I am currently rewriting the generator in python 3, it only make sense to rework this into more pythonic class code. Instead of moving the class instantiation to run-time, I do all the heavy work at generation-time, producing static classes that don't require voodoo going on behind the curtains.
The generated code becomes a whole lot longer, but bear with me.
Properties
The generator creates properties for every subproperty. This means we get a getter and setter function, where validation can happen. This also gives autocompleters the opportunity to suggest attributes while writing code, making the use of the classes easier. Instead of constantly checking the documentation for the correct name of each property, you let the autocompleter do the work.
Validation
Each setter has built-in type checking. For lists and dicts each value is checked for the correct type, additionally for dicts the keys are checked if they're strings. Additionally a validator can be specified for each subproperty, allowing actual content checking. If the validation fails, an exception is thrown.
Validators are defined with a JSON-File, with the property name as the key. For each subproperty the function to be called is specified, as well as any number of kwargs to be passed to it. Additionally
self
is also passed to the validator, in case a value's validity is dependent on other values.Example validator definition for the above code:
Validators are imported with
from troposphere.validators import *
. This allows developers to either use generic validators likeregex
or easily add validators tailored for a single property. Properties are addressed in exactly the same format as in the specification JSON to reduce any confusion as much as possible, things already are complicated enough as they are. Passing parameters as kwargs also makes any custom formatting for each validator unnecessary, further reducing complexity in the generator.Feedback
I'd love to get some feedback and constructive criticism for this. I understand it's a quite significant change to the inner workings of troposphere, but this is a good opportunity to improve it.
The text was updated successfully, but these errors were encountered: