Question

I need to figure out how to create a class that has optional ORM functionality, ie sometimes I will need to save it to a database, and other times I will not have to (I will have no connection).

SQLObject, for example, doesn't work, because it barfs when I don't supply a connection, so I can't use it without a connection.

One option is to define separate classes, one would be the base class, and the other would be the ORM class that inherits from the base class and updates the ORM fields in the setters for all of the original fields. THIS WOULD BE A MAJOR PAIN.

Anybody know of libraries that let me define an ORM class, but don't require me to save or fetch from a DB?

EDIT: Ok, here's an example. Suppose I have a script that crawls the web and compiles a list of people and their favorite color socks. I have two classes, a Person class, and a Sock class. The script provides the user the option of either saving the processed data (the Person and Sock classes) back out to a file in csv, json, or some other non-db format, or to save the information to a database (sqlite3, mysql, etc).

Was it helpful?

Solution

I've used sqlalchemy for what you're suggesting. It also allows you to write separate mappers, which might be more aligned with your use case. See both links below for the two different interfaces.

Declarative interface: http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/declarative.html

Mappers: http://docs.sqlalchemy.org/en/rel_0_8/orm/mapper_config.html#classical-mapping

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top