Generate parameterized __init__ for meta model classes
See original GitHub issueI am trying to use the generated classes. Obviously I am noticing things… 😃
When instantiating meta-model classes manually, I would greatly appreciate to have the ability to specify some attributes directly in the class call. So instead of writing
c = mm.MyClass()
c.feature1 = '...'
c.feature2 = '...'
I’d like to write
c = mm.MyClass(feature1='...', feature2='...')
On top of it I’d like to see code completion for this, as in this screenshot (FModel
is a metaclass with features name
, imports
, interfaces
and so on):
Is that something you planned to do or would be willing to PR-accept? If I’d tackle this in the generator, I’d probably generate initializers like this:
class FModel(EObject, metaclass=MetaEClass):
name = EAttribute(eType=EString)
imports = EReference(upper=-1, containment=True)
interfaces = EReference(upper=-1, containment=True)
typeCollections = EReference(upper=-1, containment=True)
def __init__(self, name=None, *, imports=(), interfaces=(), typeCollections=()):
super().__init__()
self.name = name or None
for e in imports:
self.imports.append(e)
for e in interfaces:
self.interfaces.append(e)
for e in typeCollections:
self.typeCollections.append(e)
For attributes the default value would be taken from the actual default (here None
for the string attribute).
What do you think?
Issue Analytics
- State:
- Created 6 years ago
- Comments:15 (15 by maintainers)
Top Results From Across the Web
JPA Static Metamodel Generator
Hibernate Static Metamodel Generator is an annotation processor based on JSR_269 with the task of creating JPA 2 static metamodel classes.
Read more >40.2 Using the Metamodel API to Model Entity Classes
Generate static metamodel classes by using the persistence provider's annotation processor · Obtain the metamodel class by doing one of the following: Call...
Read more >How to generate the JPA entity Metamodel? - Stack Overflow
Select your project in the Package Explorer · Go to Properties -> JPA dialog · Select source folder from Canonical metamodel (JPA 2.0)...
Read more >JPA Criteria API - Using Metamodel to create type safe queries
Here we have another option, JPA allows to automatically generate metamodel class for each entity class. If entity is T then it's metamodel...
Read more >Reference — Factory Boy stable documentation
PostGeneration : this class allows calling a given function with the generated object as argument. post_generation() : decorator performing the same functions ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oups, really sorry about the push…
If the shadowing is a result of the
name
attribute, it is not an issue. Thename
,nsURI
,eClassifiers
…etc comes directly from the ecore metamodel which defines theseEAttributes/EReferences
forEPackage
, they are not constant per say and tries to keep the EMF name. The logic of the static code generator is to give an instance level as well as a meta level in once. To do this, the mapping applied is:EClass
-> Python class + metaclassEPackage
-> Python package + moduleFor example using the library metamodel:
This ‘layer’ eases reflection/generic algorithms and allows you to easily work/deal with the meta level when the static code is generated.
So, as these
EAttribute
values defined in the module will never be used in the generated metaclasses the shadowing in the attributes init of the metaclasses cannot occurs as they does not represent data that would be handled by the metaclasses or their instances (however, you’re right this is not very Pythonic).Anyway, thanks again for this awsome new feature in the generator. I think I will prepare a new release tomorrow in the morning and communicate about it on monday.
I am on it. Expect PR within the next couple of days.