Discussion:
[Modeling-users] problems with inheritance
Lorenzo Gil Sanchez
2004-08-17 13:13:05 UTC
Permalink
I have a pymodel written in a file and after creating the database and
the python classes (mdl_generate_DB_schema.py and
mdl_generate_python_code -B) I try to run some basic code and I get this
exception:

Traceback (most recent call last):
File "model_person.py", line 33, in ?
from Person.Customer import Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/Customer.py", line
2, in ?
from MDL.Customer import Customer as MDL_Customer_Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/MDL/Customer.py",
line 5, in ?
from Person.Person import Person
ImportError: No module named Person

It looks like the problem arises when using Inheritance. In my model,
the Customer entity inherits from the Person entity. The python code
generated by modeling tryies to import Person when defining Customer and
it can't find it.

I think a possible solution would be to modify the __init__.py file of
the package modeling is generating so it imports all the domain classes
generated.

Here is my pymodel and test file:

from Modeling.PyModel import *

Attribute.defaults['usedForLocking']=1
AString.defaults['externalType']='TEXT'
AString.defaults['width'] = 0
AString.defaults['usedForLocking']=1

Association.defaults['delete']=['nullify', 'nullify']

Entity.defaults['properties'] = [
APrimaryKey('id', isClassProperty=0, isRequired=1, doc='PK')
]

_connDict = {'database': 'people.sqlite'}
model = Model('Person', adaptorName='SQLite', connDict=_connDict,
version='0.1')

model.entities = [
Entity('Person',
properties = [AString('name'), AString('surname')],
),
Entity('Customer',
properties = [AString('phone')],
parent='Person',
)
]

model.build()

if __name__ == '__main__':
from Modeling.EditingContext import EditingContext
from Person.Person import Person
from Person.Customer import Customer

ec = EditingContext()
p = Person()
p.setName('Erny')
ec.insert(p)

c = Customer()
c.setName('Lorenzo')
c.setPhone('93923924')
s***@numericable.fr
2004-08-17 22:07:22 UTC
Permalink
I think that the pb. here is that the root package, named
after the model's package nale (here: 'Person' AFAIK) isn't
in the python path.

Could you check whether you get the same error with
/home/lgs/proyectos/pruebas-modeling/
in your PYTHONPATH (before '.', in case you have in the py-path),
and report back? Thanks.

(I can't check it here right now, despite the fact that you
send us the whole code, sorry).

-- Sébastien.
Post by Lorenzo Gil Sanchez
I have a pymodel written in a file and after creating the
database and the python classes (mdl_generate_DB_schema.py
and mdl_generate_python_code -B) I try to run some basic code
and I get this
File "model_person.py", line 33, in ?
from Person.Customer import Customer
File
"/home/lgs/proyectos/pruebas-modeling/Person/Customer.py",
line 2, in ?
from MDL.Customer import Customer as MDL_Customer_Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/MDL/Customer.py",
line 5, in ?
from Person.Person import Person
ImportError: No module named Person
It looks like the problem arises when using Inheritance. In
my model, the Customer entity inherits from the Person
entity. The python code generated by modeling tryies to
import Person when defining Customer and it can't find it.
I think a possible solution would be to modify the
__init__.py file of the package modeling is generating so it
imports all the domain classes generated.
[sample code snipped]
Lorenzo Gil Sanchez
2004-08-18 07:39:01 UTC
Permalink
Post by s***@numericable.fr
I think that the pb. here is that the root package, named
after the model's package nale (here: 'Person' AFAIK) isn't
in the python path.
Could you check whether you get the same error with
/home/lgs/proyectos/pruebas-modeling/
in your PYTHONPATH (before '.', in case you have in the py-path),
and report back? Thanks.
I still get the same error:

[***@localhost pruebas-modeling]$ export
PYTHONPATH=.:/opt/lib/python2.3/site-packages/

[***@localhost pruebas-modeling]$ python model_person.py
Traceback (most recent call last):
File "model_person.py", line 33, in ?
from Person.Customer import Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/Customer.py", line
2, in ?
from MDL.Customer import Customer as MDL_Customer_Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/MDL/Customer.py",
line 5, in ?
from Person.Person import Person
ImportError: No module named Person
Sebastien Bigaret
2004-08-18 07:48:21 UTC
Permalink
Okay --I cannot check this right now, I'll get back to you on
it in a couple of days, probably during the next week-end or on
monday.

-- Sébastien.
Post by Lorenzo Gil Sanchez
Post by s***@numericable.fr
I think that the pb. here is that the root package, named
after the model's package nale (here: 'Person' AFAIK) isn't
in the python path.
Could you check whether you get the same error with
/home/lgs/proyectos/pruebas-modeling/
in your PYTHONPATH (before '.', in case you have in the py-path),
and report back? Thanks.
PYTHONPATH=.:/opt/lib/python2.3/site-packages/
File "model_person.py", line 33, in ?
from Person.Customer import Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/Customer.py", line
2, in ?
from MDL.Customer import Customer as MDL_Customer_Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/MDL/Customer.py",
line 5, in ?
from Person.Person import Person
ImportError: No module named Person
Mario Ruggier
2004-08-18 10:56:03 UTC
Permalink
Post by Lorenzo Gil Sanchez
Post by s***@numericable.fr
I think that the pb. here is that the root package, named
after the model's package nale (here: 'Person' AFAIK) isn't
in the python path.
Could you check whether you get the same error with
/home/lgs/proyectos/pruebas-modeling/
in your PYTHONPATH (before '.', in case you have in the py-path),
and report back? Thanks.
PYTHONPATH=.:/opt/lib/python2.3/site-packages/
File "model_person.py", line 33, in ?
from Person.Customer import Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/Customer.py", line
2, in ?
from MDL.Customer import Customer as MDL_Customer_Customer
File "/home/lgs/proyectos/pruebas-modeling/Person/MDL/Customer.py",
line 5, in ?
from Person.Person import Person
ImportError: No module named Person
It may well be a bug... from what I can understand there seems to be a
name-clash problem between the package and the contained modules... if
you modify your package name (that, if not specified, is taken from the
model name) to something else, e.g. PymPerson, and then adjust your
code as follows:

import sys
sys.path.insert(0,'.') # if running in dir containing the generated
PymPerson package...
print sys.path

from Modeling.EditingContext import EditingContext
from PymPerson.Person import Person
from PymPerson.Customer import Customer

ec = EditingContext()
p = Person()
p.setName('Erny')
ec.insert(p)

c = Customer()
c.setName('Lorenzo')
c.setPhone('93923924')
ec.insert(c)

print ec.fetch('Person',isDeep=1)

then, it works as expected... Sebastien, can you can you confirm when
you get around to it whether this is actually a bug?

mario

Loading...