472,958 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1882
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Antoon Pardon | last post by:
Can anyone explain why descriptors only work when they are an attribute to an object or class. I think a lot of interesting things one can do with descriptors would be just as interesting if the...
3
by: Gerry Abbott | last post by:
Access allows all objects, tables, forms, queries, macros, modules, to be documented with common object properties, including Description, Type, modified and created dates, owner, and some...
24
by: downwitch | last post by:
Hi, I know this has been covered here and in the .public groups, but it seems like it's been a while, especially around here, so I just thought I'd ask again to see if anyone has figured out a...
26
by: julien | last post by:
Hello, I don't know when to use fields and when to used properties. It looks to me that using properties is always better. But I guess fields must be better in some cases, otherwise they wouldn't...
1
by: [Yosi] | last post by:
I have the following structure : public struct CA_structure { public int ModuleId; //reference to TreeNode in tree view public TreeNode ModuleTreeNode; } ArrayList listOf Modules...
2
by: mgoold2002 | last post by:
Hello. I've just begun programming in VB .NET, and I'm trying to turn all my modules into classes. In order to retrieve/exchange values from one class to another, I initiated New instances of the...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
6
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I've encountered a problem using gettext with properties while using a Python interpreter. Here's a simple program that illustrate the problem. ============== # i18n_test.py: test of gettext &...
1
by: George V. Neville-Neil | last post by:
I have been reading the mailing list and I am unfortunately going to open up discussion I've seen twice in the last two years but which still bugs me, so please accept my apologies in advance. I...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.