Re: creating objects by name

Sebastien Bigaret <[email protected]> 08 Jul 2004 08:26:35 +0200
Newsgroups gmane.comp.python.modeling
Message-ID <[email protected]>
I don't remember that this was asked before, anyhow here is the magic :)

(provided that the model has already been loaded into the
 defaultModelSet)

>>> import AuthorBooks
>>> from Modeling import ClassDescription
>>> cd=ClassDescription.classDescriptionForName('Book')
>>> # get the class
... cd.classForInstances() 
<class 'Book.Book'>
>>> from Modeling.EditingContext import EditingContext
>>> ec=EditingContext()
>>> # create an instance
... book=cd.createInstanceWithEditingContext(ec)
<Book.Book object at 0x4103158c>
>>> # WARNING! it is NOT registered inside the EC
... print book.editingContext()
None

2 remarks:

- createInstanceWithEditingContext() completely ignores the EC, the
  object still need to be inserted into the EC afterwards. I need to
  double-check this carefully (esp. wrt DBChannel.fetchObject() if
  you're curious). Plus the documentation is incorrect, part of it
  applies to classForInstances() --esp. for the delegate part)

- the real stuff (importing the correct module and class) is done by
  EntityClassDescription.classForEntity()


-- Sébastien.



John Lenton <[email protected]> wrote:
> I'm certain this has been asked and answered before, but I can't find
> it anywhere: how can I create an object by name? By this I mean,
> instead of doing
> 
>    from MyModel.Thing import Thing
>    a_thing = Thing()
>    ec.insert(a_thing)
> 
> what magic do I use to do
> 
>    do_magic()
>    a_thing = more_magic('Thing')
>    ec.insert(a_thing)
> 
> ?
> 
> 
> -- 
> John Lenton ([email protected]) -- Random fortune:
> bash: fortune: command not found



-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
digital self defense, top technical experts, no vendor pitches, 
unmatched networking opportunities. Visit www.blackhat.com