Properties for modules?

E

Ed Leafe

I have a simple module that reads in values from disk when imported,
and stores them in attributes, allowing for code like:
'the value'

What I'd like to do is have a more property-like behavior, so that
if they try to set the value of moduleFoo.someSetting, it also
persists it to disk. But properties are really only useful in
instances of classes; if I define 'someSetting' as a property at the
module level, I get:
<property object at 0x78a990>

Does anyone know any good tricks for getting property-like behavior
here?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
 
S

Steven Bethard

Ed said:
I have a simple module that reads in values from disk when imported,
and stores them in attributes, allowing for code like:

'the value'

What I'd like to do is have a more property-like behavior, so that
if they try to set the value of moduleFoo.someSetting, it also persists
it to disk. But properties are really only useful in instances of
classes; if I define 'someSetting' as a property at the module level, I
get:

<property object at 0x78a990>

Does anyone know any good tricks for getting property-like behavior
here?

I typically define a module wrapping class like::

class GiveThisModuleProperties(object):
def __init__(self, module_name):
self._module = sys.modules[module_name]
sys.modules[module_name] = self
# now define whatever behavior you need
def __getattr__(...):
...
def __setattr__(...):
...

Then, in the module you want wrapped, you write::

GiveThisModuleProperties(__name__)

The trick here is basically that we replace the module object in
sys.modules with a class instance that wraps the module with whatever
extra behavior is necessary.

It's not beautiful, but it does seem to work. ;-)

STeVe
 
J

James Stroud

Ed said:
I have a simple module that reads in values from disk when
imported, and stores them in attributes, allowing for code like:

'the value'

What I'd like to do is have a more property-like behavior, so that
if they try to set the value of moduleFoo.someSetting, it also persists
it to disk. But properties are really only useful in instances of
classes; if I define 'someSetting' as a property at the module level, I
get:

<property object at 0x78a990>

Does anyone know any good tricks for getting property-like behavior
here?

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com


Most pythonic and recommended would be to create a class inside
moduleFoo that has the functionality you describe, instantiate an
instance, and then import reference to the instance into the local
namespace. This will be essentially equivalent to "module level
properties" as you describe them.


# moduleFoo.py

def get_setting(self, name):
return do_whatever(name)

def set_setting(self, name, arg):
return do_whatever_else(name, arg)

class Foo(object):
someSetting = property(set_setting, get_setting)

foo = Foo()


# program.py

from moduleFoo import foo

foo.someSetting = some_value

# etc.


Of course, its probably better to move the getters and setters into Foo
if they will only be used in foo context.

James
 
J

James Stroud

James said:
# moduleFoo.py

def get_setting(self, name):
return do_whatever(name)

def set_setting(self, name, arg):
return do_whatever_else(name, arg)

class Foo(object):
someSetting = property(set_setting, get_setting)

foo = Foo()


someSetting = property(set_setting, get_setting)

should be

someSetting = property(get_setting, set_setting)

James
 
E

Ed Leafe

I typically define a module wrapping class like::

class GiveThisModuleProperties(object):
def __init__(self, module_name):
self._module = sys.modules[module_name]
sys.modules[module_name] = self
# now define whatever behavior you need
def __getattr__(...):
...
def __setattr__(...):
...

Then, in the module you want wrapped, you write::

GiveThisModuleProperties(__name__)

The trick here is basically that we replace the module object in
sys.modules with a class instance that wraps the module with whatever
extra behavior is necessary.

OK, I see the trick involved. Yes, that does work for what I need.
Thanks!

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top