adding properties dynamically (how to?)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?ISO-8859-1?Q?Andr=E9?=

    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.quelque ch...@free.quel quepart.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(obj ect):
    ...

    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_p roperty_to_Pref erences(Prefere nces, option, ...)

    user_preference s = Preferences()

    Performance in this case would not be an issue.

    Cheers,

    André
  • Rafe

    #2
    Re: adding properties dynamically (how to?)

    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<b desth.quelquech ...@free.quelqu epart.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(obj ect):
    ...
    >
    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_p roperty_to_Pref erences(Prefere nces, option, ...)
    >
    user_preference s = 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,"ac tive", property(fget = ...,
    fset = ...,
    fdel = ...,
    doc = ...))

    obj = super(BaseGroup , cls).__new__(cl s)
    return obj


    You can put this in a for loop and add a property per option, etc.

    - Rafe

    Comment

    • Rafe

      #3
      Re: adding properties dynamically (how to?)

      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<b desth.quelquech ...@free.quelqu epart.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(obj ect):
      ...
      >
      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_p roperty_to_Pref erences(Prefere nces, option, ...)
      >
      user_preference s = 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,"pr opName", property(fget = ...,
      fset = ...,
      fdel = ...,
      doc = ...) )

      obj = super(AClass, cls).__new__(cl s)
      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

      Comment

      • Bruno Desthuilliers

        #4
        Re: adding properties dynamically (how to?)

        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,"pr opName", property(fget = ...,
        fset = ...,
        fdel = ...,
        doc = ...) )
        >
        obj = super(AClass, cls).__new__(cl s)
        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.



        Comment

        Working...