Re: [Modeling-users] Request for testers: dynamic creation of classes from a model
Status: Abandoned
Brought to you by:
sbigaret
From: Sebastien B. <sbi...@us...> - 2003-09-28 17:46:23
|
Hi all, I wrote: > As I wrote earlier, I'm working on dynamic creation of modules/classes > from a (py)model, either classic- or new-style (metaclass). That would > be an alternative to code generation that already exists. >=20 > Would anyone here have some time experimenting with an alpha release > of these features? If you think it would be of some interest in your First at all, thanks to all who replied they could possibly have some time to test it. I understand that your time is precious, and your help is really appreciated. I've just submitted patch #814055 at: https://sf.net/tracker/index.php?func=3Ddetail&aid=3D814055&group_id=3D5893= 5&atid=3D489337 You'll download from there the file Modeling-0.9pre15-dynamic.tar.gz. It consists of python files that should be dropped in the Modeling/ and Modeling/tests directory (see INSTALL in the tarball). Important note: you'll need the freshly released NotificationFramework v0.6 for this to work properly. For your information, I've included at the end of the message the complete README file included in the tarball, so that you can have an overview of the new features this introduces before choosing to test them. I've been asked how one can help; you may want to test that the three testsuites test_EC_Global, test_EC_Global_Inheritance and test_EC_ParentChild works with all three options '-c', 'm' and '-M' (described below) in your environment. Then of course, if you find some time to experiment it on your own models, I'll be happy to hear from that and the issues you might encounter, along with solving them, if needed. Thanks again for your time, -- S=E9bastien. ------------------------------------------------------------------------ --------------------------- Dynamic creation of classes --------------------------- :Authors: S=E9bastien Bigaret <sbi...@us...> Principles ---------- The module Modeling.dynamic has been added. It can be used in three different ways: * method build(model): dynamically build the package derived from the model. The generated package and its modules have the very same functionality as the ones generated by the script mdl_generate_python_code.py (in "base" mode, option ``-B``). * metaclass ``CustomObjectMeta``: automatically adds all the necessary methods to a class for integration with the modeling framework. It looks for the following attributes in the class: - ``entityName``: mandatory, this is the name of the class' entity. The corresponding model should have been loaded prior to the class declaration, or you'll get a dynamic.EntityNotFound exception. - ``verbose_metaclass``: if set, the metaclass prints on sys.stderr some info while building the class. - ``mdl_define_properties``: if set, the metaclass will also add properties for each attribute on relationship in the entity, so that you do not need anymore to use e.g. ``book.getTitle()`` or ``book.setTitle``, but simply ``print book.title`` or ``book.title=3D"my title"`` Note: this name is not definitive and will probably be changed before this is integrated into the core (same for metaclass, methods' names, etc.). Suggestions are welcome! * method ``build_with_metaclass(model, define_properties)``: builds the package and its module from a model. The generated modules and classes uses the ``CustomObjectMeta`` metaclass. Parameter ``define_properties`` triggers the creation of properties by the metaclass. This method raises ``NotImplementedError`` under python2.1, as expected. Note: The model will be loaded, as usually, from XML or PyModels, for example with Model.searchModel(). See e.g. tests/test_EditingContext_Global.py for an example. Example of use -------------- You find there three different examples on how these new features can be used:: ### Common to exs 1, 2 & 3: load the model from Modeling import ModelSet, Model, dynamic model=3DModel.searchModel('AuthorBooks', '.', verbose=3D1) ModelSet.defaultModelSet().addModel(model) =20=20 ### 1. example with dynamic.build() dynamic.build(model) from Modeling.EditingContext import EditingContext from AuthorBooks.Book import Book # fetch ec=3DEditingContext() print [b.getTitle() for b in ec.fetch('Book')] # create a new book b=3DBook(title=3D'mon titre') ec.insert(b) ec.saveChanges() =20=20 ### 2. example with dynamic.build_metaclass() dynamic.build_with_metaclass(model, define_properties=3D1) from Modeling.EditingContext import EditingContext ec=3DEditingContext() print [b.title for b in ec.fetch('Book')] from AuthorBooks.Book import Book b=3DBook(title=3D'mon titre7.2') ec.insert(b) ec.saveChanges() b.price=3D7.21 =20=20 ### 3. example with the metaclass class Book: __metaclass__=3Ddynamic.CustomObjectMeta entityName=3D'Book' mdl_define_properties=3D1 =20=20 class Writer: __metaclass__=3Ddynamic.CustomObjectMeta entityName=3D'Writer' mdl_define_properties=3D1 =20=20 # fetch and insert etc., as usual: from Modeling.EditingContext import EditingContext ec=3DEditingContext() b=3DBook(title=3D'book_w1') ; w1=3DWriter(lastName=3D'w1') ec.insert(b) ; ec.insert(w1) b.author=3Dw1 w1.addToBooks(b) ec.saveChanges() Tests ----- All three tests test_EC_Global.py, test_EC_Global_Inheritance.py and test_EC_ParentChild.py now accepts new options: * ``-c`` uses ``dynamic.build()`` * ``-m`` uses ``dynamic.build_with_metaclass(model, define_properties=3D0= )`` * ``-M`` uses ``dynamic.build_with_metaclass(model, define_properties=3D1= )`` * without any of these options, they use the test packages in tests/testPackages as in the standard distribution. Note: they all require that you copy the pymodels pymodel_AuthorBooks.py (in testPackages/AuthorBooks/) and pymodel_StoreEmployees.py (in (testPackages/StoreEmployees/) in the tests/ directory. Changes ------- Some classes have been changed to derive from ``object``, when available (python v2.2+), just because I've noted that this results in slightly better performance; however, this has nothing to do with the dynamic creation of classes. Among those classes that are now new-style classes: CustomObject. When it is integrated into the framework, this will probably be left as a choice for the user: since this transforms any user-class inheriting from CustomObject into new-style classes, this can have unexpected side-effects in other portions of the user's code (for example, ``type(object)=3D=3DInstanceType`` for classic-style classes, while it equals to ``obj.__class__`` for new-style classes). ------------------------------------------------------------------------ |