question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate parameterized __init__ for meta model classes

See original GitHub issue

I 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):

image

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 Nonefor the string attribute).

What do you think?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
aranegacommented, Apr 13, 2017

Oups, really sorry about the push…

If the shadowing is a result of the name attribute, it is not an issue. The name, nsURI, eClassifiers…etc comes directly from the ecore metamodel which defines these EAttributes/EReferences for EPackage, 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 + metaclass
  • EPackage -> Python package + module

For example using the library metamodel:

>>> import library  # We import the equivalent of the 'library' EPackage
>>> library.name
'library'
>>> library.nsURI
'http://emf.wikipedia.org/2011/Library'
>>> library.eClassifiers
{'Book': library.library.Book,
 'BookCategory': BookCategoryEOrderedSet([ScienceFiction=0, Biographie=1, Mistery=2]),
 'Employee': library.library.Employee,
 'Library': library.library.Library,
 'Writer': library.library.Writer}
>>> library.Book.title  # We access the Book 'title' EAttribute
<EStructuralFeature title: EString(str)>
>>> book = Book(title='test')  # We create an instance
>>> book.title  # We access the value of the attribute using the classic way
'test'
>>> book.eGet(library.Book.title)  # But we also can access it using reflection
'test'
>>> book.eGet('title')  # Also works, but in the call above, you have more meta-details about your 'title'
'test'

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.

1reaction
moltobcommented, Apr 11, 2017

I am on it. Expect PR within the next couple of days.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found