473,499 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Detecting change in value

Sushi
19 New Member
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
May 28 '07 #1
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:

Expand|Select|Wrap|Line Numbers
  1. class C(object):
  2.     def __init__(self): self.__x = None
  3.     def getx(self): return self.__x
  4.     def setx(self, value): self.__x = value
  5.     def delx(self): del self.__x
  6.     x = property(getx, setx, delx, "I'm the 'x' property.")
  7.  
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.
May 28 '07 #2
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:
Expand|Select|Wrap|Line Numbers
  1. import time, sys
  2.  
  3. f = sys.stdout
  4.  
  5. fn = 'xdata.txt'
  6. data = False
  7. while True:
  8.     s = open(fn).read().strip()
  9.     if data:
  10.         if s != data:
  11.             print 'The data was %s and now is %s. Exiting loop...' % (data, s)
  12.             break
  13.     else:
  14.         data = s
  15.     f.write(data+'\n')
  16.     f.flush()
  17.     time.sleep(1)
  18.  
  19. '''
  20. >>> 147566349
  21. 147566349
  22. 147566349
  23. 147566349
  24. 147566349
  25. The data was 147566349 and now is 0123456789. Exiting loop...
  26. '''
May 28 '07 #3
bartonc
6,596 Recognized Expert Expert
While this is not 100% useful, it works:
Expand|Select|Wrap|Line Numbers
  1. # class to trace writes of its value member
  2.  
  3. class TracedValue:
  4.     def __init__(self, value):
  5.         self.value = value
  6.  
  7.     def get(self):
  8.         return self.value
  9.  
  10.     def set(self, value):
  11.         print "%s changed value from %s to %s" %(repr(self), str(self.value), str(value))
  12.         self.value = value
  13.  
  14.  
  15. a = TracedValue(10)
  16. a.set(11)
  17. print a.get()
  18.  
<__main__.TracedValue instance at 0x009D5C10> changed value from 10 to 11
11
May 28 '07 #4
bartonc
6,596 Recognized Expert Expert
While this is not 100% useful, it works:
Expand|Select|Wrap|Line Numbers
  1. # class to trace writes of its value member
  2.  
  3. class TracedValue:
  4.     def __init__(self, value):
  5.         self.value = value
  6.  
  7.     def get(self):
  8.         return self.value
  9.  
  10.     def set(self, value):
  11.         print "%s changed value from %s to %s" %(repr(self), str(self.value), str(value))
  12.         self.value = value
  13.  
  14.  
  15. a = TracedValue(10)
  16. a.set(11)
  17. print a.get()
  18.  
<__main__.TracedValue instance at 0x009D5C10> changed value from 10 to 11
11
Or you could add a name member:
Expand|Select|Wrap|Line Numbers
  1. class TracedValue:
  2.     def __init__(self, name, value):
  3.         self.name = name
  4.         self.value = value
  5.  
  6.     def get(self):
  7.         return self.value
  8.  
  9.     def set(self, value):
  10.         print "%s changed value from %s to %s" %(repr(self.name), str(self.value), str(value))
  11.         self.value = value
  12.  
  13.  
  14. a = TracedValue('a', 10)
  15. a.set(11)
  16. print a.get()
'a' changed value from 10 to 11
11
May 28 '07 #5
bartonc
6,596 Recognized Expert Expert
Or you could add a name member:
Expand|Select|Wrap|Line Numbers
  1. class TracedValue:
  2.     def __init__(self, name, value):
  3.         self.name = name
  4.         self.value = value
  5.  
  6.     def get(self):
  7.         return self.value
  8.  
  9.     def set(self, value):
  10.         print "%s changed value from %s to %s" %(repr(self.name), str(self.value), str(value))
  11.         self.value = value
  12.  
  13.  
  14. a = TracedValue('a', 10)
  15. a.set(11)
  16. print a.get()
'a' changed value from 10 to 11
11
Or how 'bout this one:
Expand|Select|Wrap|Line Numbers
  1. class TracedHeap(object):
  2.     def __setattr__(self, name, value):
  3.         try:
  4.             traceStr = "%s had a previous value of %s" %(repr(name), str(getattr(self, name)))
  5.             oldMember = True
  6.         except AttributeError:
  7.             traceStr = "Added %s to the heap. Its value is %s" %(repr(name), str(value))
  8.             oldMember = False
  9.         self.__dict__[name] = value
  10.         print traceStr
  11.         if oldMember:
  12.             print "The new value is %s" %str(value)
  13.  
  14. th = TracedHeap()
  15. th.a = 10
  16. th.a = 11
  17.  
Added 'a' to the heap. Its value is 10
'a' had a previous value of 10
The new value is 11
May 28 '07 #6
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.
Jul 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2716
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...
2
2222
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....
2
1228
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...
6
4193
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"...
2
2240
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...
9
2001
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...
79
3703
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...
2
2919
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...
1
24009
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...
15
4188
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...
0
7128
marktang
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,...
0
7006
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
7169
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
7215
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
5467
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,...
1
4917
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...
0
3096
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...
0
3088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
661
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.