Heyo all again!
I was wondering how to get python to detect a change in value? Basically I have a loop where its looking at data from somewhere and assigning it to a. I want python to be able to see if a changes and tell me (e.g.print 'ooo it changed'). Ive been looking around, and searched these forums, but couldn't find anything. Its probably very simple but my head's not working today.
Thanks!
Mel
6 20697 bartonc 6,596
Recognized Expert Expert
Heyo all again!
I was wondering how to get python to detect a change in value? Basically I have a loop where its looking at data from somewhere and assigning it to a. I want python to be able to see if a changes and tell me (e.g.print 'ooo it changed'). Ive been looking around, and searched these forums, but couldn't find anything. Its probably very simple but my head's not working today.
Thanks!
Mel
Good morning, Mel. Nice to hear from you again.
I've recently seen the built-in function, property(), used to do as you describe:
property( [fget[, fset[, fdel[, doc]]]])
Return a property attribute for new-style classes (classes that derive from object).
fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del'ing, an attribute. Typical use is to define a managed attribute x: -
class C(object):
-
def __init__(self): self.__x = None
-
def getx(self): return self.__x
-
def setx(self, value): self.__x = value
-
def delx(self): del self.__x
-
x = property(getx, setx, delx, "I'm the 'x' property.")
-
I haven't used in myself, though.
In an event driven environment (Tk, for example), I'd create a (say) IntVar and assign a trace() to it. Events would then be generated when that instance was accessed via its get() and or set() methods.
bvdet 2,851
Recognized Expert Moderator Specialist
Heyo all again!
I was wondering how to get python to detect a change in value? Basically I have a loop where its looking at data from somewhere and assigning it to a. I want python to be able to see if a changes and tell me (e.g.print 'ooo it changed'). Ive been looking around, and searched these forums, but couldn't find anything. Its probably very simple but my head's not working today.
Thanks!
Mel
Maybe this will give you some ideas: - import time, sys
-
-
f = sys.stdout
-
-
fn = 'xdata.txt'
-
data = False
-
while True:
-
s = open(fn).read().strip()
-
if data:
-
if s != data:
-
print 'The data was %s and now is %s. Exiting loop...' % (data, s)
-
break
-
else:
-
data = s
-
f.write(data+'\n')
-
f.flush()
-
time.sleep(1)
-
-
'''
-
>>> 147566349
-
147566349
-
147566349
-
147566349
-
147566349
-
The data was 147566349 and now is 0123456789. Exiting loop...
-
'''
bartonc 6,596
Recognized Expert Expert
While this is not 100% useful, it works: - # class to trace writes of its value member
-
-
class TracedValue:
-
def __init__(self, value):
-
self.value = value
-
-
def get(self):
-
return self.value
-
-
def set(self, value):
-
print "%s changed value from %s to %s" %(repr(self), str(self.value), str(value))
-
self.value = value
-
-
-
a = TracedValue(10)
-
a.set(11)
-
print a.get()
-
<__main__.TracedValue instance at 0x009D5C10> changed value from 10 to 11
11
bartonc 6,596
Recognized Expert Expert
While this is not 100% useful, it works: - # class to trace writes of its value member
-
-
class TracedValue:
-
def __init__(self, value):
-
self.value = value
-
-
def get(self):
-
return self.value
-
-
def set(self, value):
-
print "%s changed value from %s to %s" %(repr(self), str(self.value), str(value))
-
self.value = value
-
-
-
a = TracedValue(10)
-
a.set(11)
-
print a.get()
-
<__main__.TracedValue instance at 0x009D5C10> changed value from 10 to 11
11
Or you could add a name member: -
class TracedValue:
-
def __init__(self, name, value):
-
self.name = name
-
self.value = value
-
-
def get(self):
-
return self.value
-
-
def set(self, value):
-
print "%s changed value from %s to %s" %(repr(self.name), str(self.value), str(value))
-
self.value = value
-
-
-
a = TracedValue('a', 10)
-
a.set(11)
-
print a.get()
'a' changed value from 10 to 11
11
bartonc 6,596
Recognized Expert Expert
Or you could add a name member: -
class TracedValue:
-
def __init__(self, name, value):
-
self.name = name
-
self.value = value
-
-
def get(self):
-
return self.value
-
-
def set(self, value):
-
print "%s changed value from %s to %s" %(repr(self.name), str(self.value), str(value))
-
self.value = value
-
-
-
a = TracedValue('a', 10)
-
a.set(11)
-
print a.get()
'a' changed value from 10 to 11
11
Or how 'bout this one: -
class TracedHeap(object):
-
def __setattr__(self, name, value):
-
try:
-
traceStr = "%s had a previous value of %s" %(repr(name), str(getattr(self, name)))
-
oldMember = True
-
except AttributeError:
-
traceStr = "Added %s to the heap. Its value is %s" %(repr(name), str(value))
-
oldMember = False
-
self.__dict__[name] = value
-
print traceStr
-
if oldMember:
-
print "The new value is %s" %str(value)
-
-
th = TracedHeap()
-
th.a = 10
-
th.a = 11
-
Added 'a' to the heap. Its value is 10
'a' had a previous value of 10
The new value is 11
bartonc 6,596
Recognized Expert Expert
Heyo all again!
I was wondering how to get python to detect a change in value? Basically I have a loop where its looking at data from somewhere and assigning it to a. I want python to be able to see if a changes and tell me (e.g.print 'ooo it changed'). Ive been looking around, and searched these forums, but couldn't find anything. Its probably very simple but my head's not working today.
Thanks!
Mel
Hi, Mel. I just found py-notify in the cheese shop.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Erik Bethke |
last post by:
Hello All,
I am trying to clean up some polish bugs with the Shanghai game I am
working on and I am currently stuck on trying to get the right event
for detecting when the user has changed the...
|
by: Frank |
last post by:
Hello,
I'm a total newbie on Javascript, and with a lot of help and some copy
and paste managed to get things running.
On my site: http://home.wanadoo.nl/homepage I use a form from
Bravenet....
|
by: epigram |
last post by:
I have a form on a web page that contains several textbox controls (i.e.
input type="text") and a checkbox. As soon as the text changes, in only a
subset of these textbox controls, I want to...
|
by: Thomas Mlynarczyk |
last post by:
Hi,
Say I have an array containing a reference to itself:
$myArray = array();
$myArray =& $myArray;
How can I, looping iteratively through the array, detect the fact that
$myArray will "take"...
|
by: Deano |
last post by:
In my app I have lots of forms with data presented in various ways
using all kinds of controls.
Is there a simple way of detecting if the user has changed any data? I
would like to do this so I...
| |
by: D. Shane Fowlkes |
last post by:
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field...
|
by: VK |
last post by:
I wandering about the common proctice of some UA's producers to spoof
the UA string to pretend to be another browser (most often IE).
Shouldn't it be considered as a trademark violation of the...
|
by: =?Utf-8?B?TWlrZQ==?= |
last post by:
Greetings,
I am trying to find out how to do something that on the surface seems like
it should be very simple to do.
I have a datagrid with a datatable bound to it using the SetDataBinding...
|
by: wwwords |
last post by:
Is there a general method for detecting that a user has changed the
record currently visible on a form, whether this is by hitting PgUp or
PgDn or clicking on a navigation button, even if no change...
|
by: RobG |
last post by:
When using createEvent, an eventType parameter must be provided as an
argument. This can be one of those specified in DOM 2 or 3 Events, or
it might be a proprietary eventType.
My problem is...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
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...
|
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,...
|
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...
|
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,...
|
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: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |