0

I'd like to save an object's pointer to the database so that it can be run again later.

The way I see it, I need to know how I can import it before I can run it. Then, I can do some dynamic importing with __import__().

However I'm not sure how, given an arbitrary instantiated object, how do I resolve its import hierarchy.

For example:

I have a Base class imported this way:

from action.base_model import Base

Then I want to save this to a database:

save_task(Base, *args)

But I need to resolve "action.base_model.Base" from the object passed into save_task. How can I do this?

Thanks!

nubela
  • 1
  • 24
  • 75
  • 123
  • This is possible, but I have to ask *why*, because there is very little reason to do this unless you are making a Python-specific object database. – Lennart Regebro Oct 29 '13 at 06:17
  • I am indeed making a python specific object database. I'm doing cron jobs, but I can't trust the server (EC2), so rather than have a long running process, I want to back up the queue tasks onto the database. – nubela Oct 29 '13 at 06:18
  • There is one already: ZODB. https://pypi.python.org/pypi/ZODB, http://www.zodb.org/en/latest/ There you can store and load python objects. If you think it's overkill you can also just pickle them to a file. – Lennart Regebro Oct 29 '13 at 06:23
  • I had a feeling I had seen an answer to this. It's called a"fully qualified name". [Get fully qualified class name of an object in python](http://stackoverflow.com/questions/2020014/get-fully-qualified-class-name-of-an-object-in-python) – Lennart Regebro Oct 29 '13 at 06:24
  • i'd avoid using ZODB because it adds an additional layer of dependency, when i could do the very same simply by knowing the string for importing. or would you disagree? – nubela Oct 29 '13 at 06:26
  • AHH. YEAP. That's the right answer. how about you answer this question and i'd mark it as correct? – nubela Oct 29 '13 at 06:26
  • I disagree. Making an object database is not a trivial task. If that's really what you want, then use the ZODB. If you don't really want an object database, you just want to save objects, then pickling would be enough. Both solutions solve this particular problem for you. – Lennart Regebro Oct 29 '13 at 06:34
  • 1
    I think you may be reinventing the `pickle` module. Why don't you just save and restore your objects using it and save yourself a lot of trouble? – martineau Oct 29 '13 at 08:27

0 Answers0