On Wed, Nov 04, 2015 at 05:01:47PM +0000, Florian Wilmshoever wrote: > > Hi Dave, Hi list, > > first of all thanks for working in the adjustments for the attribute > references we talked about a while ago. > > generateDS has been working very well for me since then. >
Florian, Good to hear from you. Glad to hear that generateDS.py is *mostly* working well for you. > Unfortunately like a couple of days ago I tried to integrate a > second, generated subclass into my program and came across some > unexpected behaviour. > > The setup is as follows. > -generated parent class 'parent' > -two generated subclasses 'subA' and 'subB' How did you generate the subclass modules? Did you use the "-s" command line option? > > In my own module I'm parsing a xml file according to the base xsd of parent. > > My module imports both subclass-modules to either create an instance > of 'subA' or 'subB' by using > > modulesubA.parse(filename) or modulesubB.parse(filename). > > The unexpected behaviour while testing this, no matter which parse > function I call, both will return an object of the last imported > subclass module. > > So if my imports look like this: > > 'import modulesubA' > 'import modulesubB' > > Both function calls: > 'modulesubA.parse(filename)' > 'modulesubB.parse(filename)' > will return an object of type 'subB'. A couple of things to think about (which you are likely already aware of): - When you import a module, it is only imported once, even if you try to import it twice. - Therefore, there will only be one instance of the superclass module. - And, therefore, that class variable 'subclass' will have a single value. > > If you change the order of the imports, it will be the other way round. > > My initial observation is, that this is due to the assignment of the > classvariable 'subclass' in 'parent'. I believe you are right. > > It seems to get assigned from each modulesubA and modulesubB > initially while importing them and after that the factory function > of the main class will only return objects of the subclass which was > assigned there last. One thing to try is to make two copies of the superclass module, that is, copy the file itself. Or, if you are on Linux, make a symbolic link; I don't know how to do that on MS Windows. Then change the import statements in the subclass modules so they import two different modules. Or, here is an alternative that you can try -- Try to force the import to really happen a second time by doing something like the following: import sys sys.modules.pop('modulesuper') import modulesubA # or modulesubB Take a look at the documentation on 'sys.modules'. (https://docs.python.org/2/library/sys.html#sys.modules) It says: "This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. Note that removing a module from this dictionary is not the same as calling reload() on the corresponding module object." So, possibly, if you do the sys.modules.pop(mod_name) before each import of a sub-module, that will change the behavior. Actually, copying the superclass module (or making symbolic links) seems to me to be a better and less devious and less tricky approach. But, of course, I don't know your specific needs. Let me know if any of the above solves your problem. And, tell me if there is other help I can give. Dave > > I may have an idea on how this could be solved, but I would like to > have some opinion or discussion from you first. > > I can also provide an minimal example of this but that will take me > some more time, so I hope it gets understood like I described. > > Let me know if you can reproduce this behavior or need me to provide > some more information. > > Thanks & Best Regards > Florian Wilmshöver > > -- Dave Kuhlman http://www.davekuhlman.org ------------------------------------------------------------------------------ _______________________________________________ generateds-users mailing list generateds-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/generateds-users