By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

Detecting change in value

Sushi
P: 19
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
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
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
Expert Mod 2.5K+
P: 2,851
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

Post your reply

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