473,473 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

adding properties dynamically (how to?)

I didn't want to hijack the original thread but I have basically the
same request...

On Aug 17, 7:09 am, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
akonsu a écrit :hello,
[SNIP]
>
Wrong solution to your problem, I'd say. Let's start again:

"""
i need to add properties to instances dynamically during run time.
this is because their names are determined by the database contents.
"""

Care to elaborate ? I may be wrong, but I suspect you're trying to roll
your own python/database mapper. If so, there are quite a couple Python
ORMs around. Else, please tell us more.
I'm not the original poster, but I'd like to do the same thing (for a
different reason).

I have a program (crunchy) that is extensible via plugins. New
options available via plugins can be turned on or off (or selected
among a list of options). I have a module for user preferences (let's
call it prefs.py) that allows the setting of these options (and do
error checking, automatic saving of the options selected for future
sessions, etc.). These options are implemented as properties.

Currently I have it simplified so that only two lines need to be added
to prefs.py to add new options; something like
options = { ...
'new_option': [value1, value2, ..., valueN],
...}

and
class Preferences(object):
...

new_option = make_property('new_option', 'some nicely worded help
string')

===
make_property is a custom define function that return fgets, fsets,
fdel and doc.

Ideally, I'd like to be able to define new would-be properties from
the plugin and add them to the class prior to creating instances. In
other words, have something like

===
for option in options_defined_in_plugins:
add_option_as_property_to_Preferences(Preferences, option, ...)

user_preferences = Preferences()

Performance in this case would not be an issue.

Cheers,

André
Aug 17 '08 #1
3 1491
On Aug 17, 7:51 pm, André <andre.robe...@gmail.comwrote:
I didn't want to hijack the original thread but I have basically the
same request...

On Aug 17, 7:09 am, Bruno Desthuilliers<bdesth.quelquech...@free.quelquepart .frwrote:
akonsu a écrit :hello,

[SNIP]
Wrong solution to your problem, I'd say. Let's start again:
"""
i need to add properties to instances dynamically during run time.
this is because their names are determined by the database contents.
"""
Care to elaborate ? I may be wrong, but I suspect you're trying to roll
your own python/database mapper. If so, there are quite a couple Python
ORMs around. Else, please tell us more.

I'm not the original poster, but I'd like to do the same thing (for a
different reason).

I have a program (crunchy) that is extensible via plugins. New
options available via plugins can be turned on or off (or selected
among a list of options). I have a module for user preferences (let's
call it prefs.py) that allows the setting of these options (and do
error checking, automatic saving of the options selected for future
sessions, etc.). These options are implemented as properties.

Currently I have it simplified so that only two lines need to be added
to prefs.py to add new options; something like
options = { ...
'new_option': [value1, value2, ..., valueN],
...}

and
class Preferences(object):
...

new_option = make_property('new_option', 'some nicely worded help
string')

===
make_property is a custom define function that return fgets, fsets,
fdel and doc.

Ideally, I'd like to be able to define new would-be properties from
the plugin and add them to the class prior to creating instances. In
other words, have something like

===
for option in options_defined_in_plugins:
add_option_as_property_to_Preferences(Preferences, option, ...)

user_preferences = Preferences()

Performance in this case would not be an issue.

Cheers,

André
You can dynamicly add properties to a class just before returning the
instance using __new__():

class AClass(object):
def __new__(cls):

setattr(cls,"active", property(fget = ...,
fset = ...,
fdel = ...,
doc = ...))

obj = super(BaseGroup, cls).__new__(cls)
return obj
You can put this in a for loop and add a property per option, etc.

- Rafe
Aug 19 '08 #2
On Aug 17, 7:51 pm, André <andre.robe...@gmail.comwrote:
I didn't want to hijack the original thread but I have basically the
same request...

On Aug 17, 7:09 am, Bruno Desthuilliers<bdesth.quelquech...@free.quelquepart .frwrote:
akonsu a écrit :hello,

[SNIP]
Wrong solution to your problem, I'd say. Let's start again:
"""
i need to add properties to instances dynamically during run time.
this is because their names are determined by the database contents.
"""
Care to elaborate ? I may be wrong, but I suspect you're trying to roll
your own python/database mapper. If so, there are quite a couple Python
ORMs around. Else, please tell us more.

I'm not the original poster, but I'd like to do the same thing (for a
different reason).

I have a program (crunchy) that is extensible via plugins. New
options available via plugins can be turned on or off (or selected
among a list of options). I have a module for user preferences (let's
call it prefs.py) that allows the setting of these options (and do
error checking, automatic saving of the options selected for future
sessions, etc.). These options are implemented as properties.

Currently I have it simplified so that only two lines need to be added
to prefs.py to add new options; something like
options = { ...
'new_option': [value1, value2, ..., valueN],
...}

and
class Preferences(object):
...

new_option = make_property('new_option', 'some nicely worded help
string')

===
make_property is a custom define function that return fgets, fsets,
fdel and doc.

Ideally, I'd like to be able to define new would-be properties from
the plugin and add them to the class prior to creating instances. In
other words, have something like

===
for option in options_defined_in_plugins:
add_option_as_property_to_Preferences(Preferences, option, ...)

user_preferences = Preferences()

Performance in this case would not be an issue.

Cheers,

André

Hi,

You can dynamically add properties to a class just before returning
the
instance using __new__():

class AClass(object):
def __new__(cls):

setattr(cls,"propName", property(fget = ...,
fset = ...,
fdel = ...,
doc = ...) )

obj = super(AClass, cls).__new__(cls)
return obj

You can put this in a for loop and add a property per option, etc. You
can also do this with your own descriptor if you make a custom one.

- Rafe
Aug 19 '08 #3
Rafe a écrit :
(snip)
>
You can dynamically add properties to a class
The OP was asking for *per instance* properties...
just before returning
the
instance using __new__():

class AClass(object):
def __new__(cls):

setattr(cls,"propName", property(fget = ...,
fset = ...,
fdel = ...,
doc = ...) )

obj = super(AClass, cls).__new__(cls)
return obj

Very bad idea IMHO. __new__ is called on each instanciation, which means
the above code will uselessly modify the class each time you create an
instance of it, overwriting the same properties again and again and
again. If what you want is to automagically add properties (or whatever
type of attributes) to a class and/or it's subclass, you'd be better
using a custom metaclass.

Aug 20 '08 #4

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

Similar topics

10
by: David | last post by:
Can anyone give me a quick code snippet (that is standards-based) for adding OPTION tags to a SELECT dynamically. I have no problem doing it in IE but I am kind of new to the whole standards world...
20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
9
by: James Geurts | last post by:
Hey all... I posted this in the vs.net ide group too, but people are not answering, so I figured that it might be more appropriate here. I'm not sure if I'm adding a designer to my code properly. ...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
4
by: Joe | last post by:
I have an xml file which looks similar to this: <data> Please enter the value: <dynamicControl id="myDC", type="Textbox", MaxLength="100"/> </data> I am trying to devise a way to have a web...
7
by: Jason | last post by:
I am trying to dynamically add a web user control - ctrlBlogEntry.ascx - to a page - default.aspx - (via an ASP:PlaceHolder). This web user control has two ASP:Label controls and I'm accessing...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
by: dixonjm | last post by:
Hi There, I am looking for a little help with direction on how to do the above. Basically I have a sql database which contains a table of properties - this table contains a description col, a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.