Hi all, I have an interesting problem that I'm hoping can be solved with metaprogramming, but I don't know how far Python supports code generation (and I don't know if I'm taking the correct approach either... hence why I'm asking on this group):
I'd like to write a program that writes/manipulates a statemachine. My idea was that I would define states in a .py file along with their transitions and other metadata. I could then write a suite of programs that would manipulate these states in interesting ways, such as generate diagrams and documentation, test suites, do verifications and also edit them. These programs would import the .py file and use introspection upon the module's classes (states would be modelled as classes). For example, a documentation tool could use graphviz to draw the statediagram with the transition table and docstring... My problem is that I don't know if it's possible to edit these states and then write them back to .py. Firstly, if my editing tool was to create a new state, I would want to create the class (using type) and attach it to the imported state module somehow. I can't find information on whether this is possible. Secondly, I'd like to manipulate the class and then use the inspect module to get the source and write it back to file. Now I'm pretty sure the inspect module will only write the source code as found in the original .py file. Is there a way to generate the code for a class or other python objects in a generic way? Or will I have to bite the bullet and write to custom file and generate code from that (like glade with XML)? Or is there another solution with templates that I'm over looking? Having the classes in memory, editing their attributes, adding methods and then serialising them to code has an elegance to it in my mind and would hopefully simplify things. The result would be directly executable by the interpreter itself and editable by python developers. But I fear that I may need an intermediary notation/format for storage. Any advice/hints would be very useful. Thanks, Paul -- http://mail.python.org/mailman/listinfo/python-list