473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 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 1896
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
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,...
0
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...

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.