473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Dynamically creating attributes and methods

Hi,

I'm writing a DBI for my company's database.
I will have one class for each of the tables
in the database.

I would then like each class, once instantiated,
to be able to read the field list from it's table,
and create properties based on each field, with
get/set methods created for accessing.

The get/set methods could be the same for every
field, provided that I can determine the name
of the field(property) being accessed.

Given a field name such as 'phone', how can I
give the class a home property, and then attach
that property to the get/set methods on the fly?

Thanks,

Toby

Jul 18 '05 #1
3 3155

<us**@domain.invalid> wrote in message
news:a7******************************@news.teranew s.com...
Hi,

I'm writing a DBI for my company's database.
I will have one class for each of the tables
in the database.

I would then like each class, once instantiated,
to be able to read the field list from it's table,
and create properties based on each field, with
get/set methods created for accessing.

The get/set methods could be the same for every
field, provided that I can determine the name
of the field(property) being accessed.

Given a field name such as 'phone', how can I
give the class a home property, and then attach
that property to the get/set methods on the fly?

Thanks,

Toby
The easiest method to understand is to build
the desired method in a string and use exec() to
create a function object. Then use setattr() to
insert the function object into the class object.
Then you can create the property object and
insert it into the class the same way.

John Roth

Jul 18 '05 #2
us**@domain.invalid wrote:
Hi,

I'm writing a DBI for my company's database.
I will have one class for each of the tables
in the database.

I would then like each class, once instantiated,
to be able to read the field list from it's table,
and create properties based on each field, with
get/set methods created for accessing.
OK -- I think you can find already-made Python
modules that do such kinds of jobs, but still,
it's probably OK to do the job yourself, too.

It is not clear to me if you want to regenerate
all the classes and their attributes/properties
dinamycally every time your program starts, and
why -- generating Python source code once, then
importing it on each run (regenerating the source,
perhaps automatically, if and when you determine
the database's schema has changed.

I assume the source-generation approach is clear
to you and will sketch how attack the "rebuild
dynamically each time the program starts" one.

The get/set methods could be the same for every
field, provided that I can determine the name
of the field(property) being accessed.
Sure, and that's the key idea -- use a closure.
Given a field name such as 'phone', how can I
give the class a home property, and then attach
that property to the get/set methods on the fly?


I'm not sure what "a home property" is. In
context it would seem to make more sense to ask
for "a property named 'phone'".

E.g., a trivial example...:

def add_property(cls, name):
def get(self):
return getattr(self, '_'+name)
def set(self, value):
setattr(self, '_'+name, value)
setattr(cls, name, property(get, set))

this shows how to add a property that's a pair
of trivial accessors which just map get and set
to an underlying data attribute which has the
same name plus a leading underscore; of course,
in your case the get and set will be way more
elaborate, but I hope this might have all the
elements you might have been missing -- setattr,
getattr, closures, the property descriptor type
and how it's used...
Alex
Jul 18 '05 #3
<us**@domain.invalid> wrote in message
news:a7******************************@news.teranew s.com...
Hi,

I'm writing a DBI for my company's database.
I will have one class for each of the tables
in the database.

I would then like each class, once instantiated,
to be able to read the field list from it's table,
and create properties based on each field, with
get/set methods created for accessing.

The get/set methods could be the same for every
field, provided that I can determine the name
of the field(property) being accessed.

Given a field name such as 'phone', how can I
give the class a home property, and then attach
that property to the get/set methods on the fly?

Thanks,

Toby


Hi.
You may find this relevant:
http://aspn.activestate.com/ASPN/Coo.../Recipe/157768

HTH,
Sean

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
9
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
1
by: engwar | last post by:
Not really sure how to ask this. I'm new to CSS/DHTML I want to create something similar to how yahoo and gmail handle the "to" field when you type an email. If you start typing "d" you will...
5
by: Angel | last post by:
Is there a way to create an IFRAME dynamically via VB.NET. In other words creating the HTML element in the server side code? thanks in advance....
5
by: Nathan Harmston | last post by:
Hi, Sorry if the subject line of post is wrong, but I think that is what this is called. I want to create objects with class Coconuts(object): def __init__(self, a, b, *args, **kwargs):...
3
by: pbd22 | last post by:
Hi. How do I add the runat=server attribute on a buttonfield link dynamically? thanks!
4
by: =?Utf-8?B?QWJoaQ==?= | last post by:
I am using Reflection to invoke methods dynamically. I have got a special requirement where I need to pass a value to method by setting the custom method attribute. As I cannot change the...
5
by: akonsu | last post by:
hello, i need to add properties to instances dynamically during run time. this is because their names are determined by the database contents. so far i found a way to add methods on demand: ...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.