471,310 Members | 1,229 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Properties for modules?

I have a simple module that reads in values from disk when imported,
and stores them in attributes, allowing for code like:
>>import moduleFoo
print moduleFoo.someSetting
'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:
>>import moduleFoo
print moduleFoo.someSetting
<property object at 0x78a990>

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

-- Ed Leafe
-- http://leafe.com
-- http://dabodev.com
Jun 11 '07 #1
4 1817
Ed Leafe wrote:
I have a simple module that reads in values from disk when imported,
and stores them in attributes, allowing for code like:
>>import moduleFoo
>>print moduleFoo.someSetting
'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:
>>import moduleFoo
>>print moduleFoo.someSetting
<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
Jun 11 '07 #2
Ed Leafe wrote:
I have a simple module that reads in values from disk when
imported, and stores them in attributes, allowing for code like:
>>import moduleFoo
>>print moduleFoo.someSetting
'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:
>>import moduleFoo
>>print moduleFoo.someSetting
<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
Jun 11 '07 #3
James Stroud wrote:
# 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
Jun 11 '07 #4
On Jun 11, 2007, at 5:55 PM, Steven Bethard wrote:
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
Jun 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Gerry Abbott | last post: by
26 posts views Thread by julien | last post: by
17 posts views Thread by Lee Harr | last post: by
6 posts views Thread by =?iso-8859-1?B?QW5kcuk=?= | last post: by
1 post views Thread by George V. Neville-Neil | last post: by

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.