Eve from Adams' Ribs

G

Godwin

Dear Pythoneer,
I'm thinking of making a class out of an rdbms table name(let's
say oracle).
I can dynamically query the column names and their datatypes from
oracle. So at runtime can i create a class object that creates a class
for example employee with methods that can manipulate that table. For
example :

class Employee(object):

def insert(self,**kwds):
"should insert values into the table based on column and value pairs
supplied"

def update(self,**kwds):
"should update the table"

def delete(self,where):
"should delete a row"

def select(self,*cols,where)
"should retrieve results based on columns and where clause"

But the funny fact is that i want this class to be dynamically
generated at run
time simply from a table name string. "Yes i want to create Eve out of
Adams' rib". I think its possible with python as it is called a dynamic
language. Would u let me on this python secret?
 
G

Gregory Bond

Godwin said:
But the funny fact is that i want this class to be dynamically
generated at run
time simply from a table name string.

The thing you are looking for is called an "object-relational mapper".

try SQLObject http://sqlobject.org/

This is mainly intended to work the other way (i.e. describe the
structure in Python and create tables from that), but it does support a
"get the structure from a legacy table" mode that may be good enough
(you may be in strife if your existing tables don't meet certain
requirements, like having a unique integer key, or have odd
column-naming schema etc). I don't think it supports Oracle (yet...)
 
M

Magnus Lycka

Godwin said:
I think its possible with python as it is called a dynamic
language.

Certainly, but what's really the point with these classes?
If they are completely generated from tables, they won't
have different behaviour anyway, just a different chunk
of attributes to fetch, save and update.

It's probably as easy to just have one class, and to have
a dynamic list of attributes and values that you define by
passing in the table name in __init__.
 
G

Godwin

www.devx.com/dbzone/Article/22093

U've made a point and i have seen that approach in the above article.
But i was curious about creating python classes and code on fly. The
only way i could do it was by using exec function. Is there a standard
way of doing it. What are the plus points of such kind of programming?
Is it only done by hackers?

Sorry for asking the wrong question for getting the right answer!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top