473,320 Members | 2,162 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,320 software developers and data experts.

property puzzle

[python 2.3.3, x86 linux, part of a substantial gtk/postgres app]
I want a Run instance to have a Status instance which inherits
from Observable. I want to be able to say:

thisrun.status = 'planned'

and have thisrun's _set_status do side-effects and then the status
object's setter incorporate the new value as it's new state.
But the setter functions never seem to get called.
Am I using "property" wrong?

class Observable: #("observer" software pattern)
def __init__(self):
self.observers = []
def register(self, func, *args, **kw):
self.observers.append((func, args, kw))
def notify(self):
for func, args, kw in self.observers: func(*args, **kw)

class RunStatus(Observable):
def __init__(self, val):
Observable.__init__(self)
self._value = val
def set(self, newval):
print 'RunStatus.set'
self._value = newval
self.notify()
def get(self):
print 'RunStatus.get'
return self._value

class Run:
def __init__(self):
self._status = RunStatus('planned')
def _get_status(self):
return self._status
def _set_status(self, val):
# do various things with val...
self._status.set(val)
status = property(_get_status, _set_status)

if __name__ == '__main__':
def stat_display():
print 'display to screen: r.status=', r.status
r = Run()
r.status.register(stat_display) #many registers in real app.
r.status='hold'
print 'status=', r.status
print r._status.observers

Status needs to be a real and persistant object so it's
observers persist, but needs it's value changed simply, like
r.status = 'something'

[flames about using string values for what is semantically a
six valued enum *are* welcome in a separate thread...]
Jul 18 '05 #1
3 1420
"george young" <gr*@ll.mit.edu> wrote in message
news:78**************************@posting.google.c om...
[python 2.3.3, x86 linux, part of a substantial gtk/postgres app]
I want a Run instance to have a Status instance which inherits
from Observable. I want to be able to say:

thisrun.status = 'planned'

and have thisrun's _set_status do side-effects and then the status
object's setter incorporate the new value as it's new state.
But the setter functions never seem to get called.
Am I using "property" wrong?

class Observable: #("observer" software pattern)


'property' only works with new-style classes. Try changing to

class Observable(object):

and also make the same change in class Run.
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #2

Also, property needs bound methods to get, set, and delete, like

status = property(self._get_status, self._set_status)

/Jean Brouwers
In article <78**************************@posting.google.com >, george
young <gr*@ll.mit.edu> wrote:
[python 2.3.3, x86 linux, part of a substantial gtk/postgres app]
I want a Run instance to have a Status instance which inherits
from Observable. I want to be able to say:

thisrun.status = 'planned'

and have thisrun's _set_status do side-effects and then the status
object's setter incorporate the new value as it's new state.
But the setter functions never seem to get called.
Am I using "property" wrong?

class Observable: #("observer" software pattern)
def __init__(self):
self.observers = []
def register(self, func, *args, **kw):
self.observers.append((func, args, kw))
def notify(self):
for func, args, kw in self.observers: func(*args, **kw)

class RunStatus(Observable):
def __init__(self, val):
Observable.__init__(self)
self._value = val
def set(self, newval):
print 'RunStatus.set'
self._value = newval
self.notify()
def get(self):
print 'RunStatus.get'
return self._value

class Run:
def __init__(self):
self._status = RunStatus('planned')
def _get_status(self):
return self._status
def _set_status(self, val):
# do various things with val...
self._status.set(val)
status = property(_get_status, _set_status)

if __name__ == '__main__':
def stat_display():
print 'display to screen: r.status=', r.status
r = Run()
r.status.register(stat_display) #many registers in real app.
r.status='hold'
print 'status=', r.status
print r._status.observers

Status needs to be a real and persistant object so it's
observers persist, but needs it's value changed simply, like
r.status = 'something'

[flames about using string values for what is semantically a
six valued enum *are* welcome in a separate thread...]

Jul 18 '05 #3
Jean Brouwers wrote:
Also, property needs bound methods to get, set, and delete, like

status = property(self._get_status, self._set_status)

/Jean Brouwers


No it doesn't. The following is a working piece of code:

class C(object):
def getx(self):
return 1

x = property(getx)

print C().x

Prints 1 as expected.

--
Shalabh

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.