Re: dynamic class instantiation



Ognen Duzlevski wrote:
> Larry Bates <larry.bates@xxxxxxxxxxx> wrote:
>>> Now I want to use something like xml.dom.minidom to "parse" the
>>> .xml file into a set of classes defined according to the "language
>>> definition" file. The parse() method from the xml.dom.minidom
>>> package will return a document instance and I can get the node
>>> name from it. Say I got "page" as a string. How do I go about
>>> instantiating a class from this piece of information? To make it
>>> more obvious how do I create the page() class based on the "page"
>>> string I have? I want to make this generic so for me the language
>>> definition file will contain pages, functions, datasets etc. but
>>> for someone else mileage will vary.
>>>
>>> Thanks,
>>> Ognen
>
>> I think you should rethink this approach. Why have 3 different
>> files instead of just defining everything as Python classes?
>> Get good baseclass definitions and use OOP inheritance to
>> create your specific class instances. I think mixing XML,
>> language definition file (LDF), LDF to python parser is a LOT
>> harder (and slower) than just writing the classes outright.
>
> Hi Larry,
>
> here is the whole story. I have a database (postgres) xml "transformer"
> and substitution engine. It is written entirely in plpgsql and the whole
> environment works something like this. Someone writes an xml file that
> defines a web page. This xml file is then fed into the database at the
> time of development. Now at runtime a mod_python apache webapp makes a
> request for a certain page. Its xml form is retrieved from the database
> and a lot of data substitution goes on. Also parts of the page are "masked"
> out by the database engine based on the "level" of the person requesting
> the page. The "transformed and substituted" xml is then spit back to the
> webapp which now applies an xml->html transformation and presents the
> final result to the user. So, I want people to be able to "define" their
> own "language" when it comes to page definitions. And instead of rewriting
> the xml file parser I want to be able to generate the parser.
>
> Thanks,
> Ognen

Well you certainly can do that, but why invent a new language to define
the pages instead of just using Python directly? I did an application
a few years ago that sounds a little like what you describe and now
wish I had not spent so much time doing Text File->Python transforma-
tions. The logic got pretty ugly and extending things was rather
involved. It wasn't until I started using ReportLab that I began to
"see the light" on just keeping everything in Python and inheriting
from base classes and extending.

If you want to define a Python class, the best way to do that is to
use Python from the outset. I probably don't completely understand
what you want to do, but thought I'd at least put in my 2-cents worth.

Regards,
Larry
.



Relevant Pages

  • Re: XML parser and writer
    ... them on a calendar. ... Therefore I will need to both easily parse and write new XML files. ... why not some database technology? ... an advanced user can edit the XML file directly at ...
    (comp.lang.java.programmer)
  • Re: Special Characters not resolving
    ... starting data at the origin in an ORacle database is 2000 characters. ... When the XML isdelivered to me on disk and I load an ... Obviously I need to find either a way to have the XML file provider strip ...
    (microsoft.public.dotnet.xml)
  • Re: XML and the Datagrid
    ... I find it, when it is in memory, exactly as good as the database, because it ... I wished that the complexity from an XML file was in a real database system. ... It is in my opinion the best thing to describe and hold data until now and I ...
    (microsoft.public.dotnet.languages.vb)
  • Performance of cPickle module
    ... I want to write my own little blog using Python (as a fairly small but doable ... I don't want so far to use a database as a backend, I'd prefer use XML which is ... I don't want to parse the xml file each time, instead I want to save the Users ...
    (comp.lang.python)
  • Re: Pythonic gui format?
    ... But isn't python code more flexible than XML? ... to edit py file than xml file. ... GUI design; it's better in such situations to make use of those tools ...
    (comp.lang.python)