Why is it faster the second time ?
Hello the group, I am wondering why doctests run slower the first time. In the transcript below "try" is a script which finds and runs doctests in the current directory. It also shows how long it takes to run these tests. I added a new test which searches recursively for files given a path, and it took (approx) 3 seconds to run. But all subsequent runs take less than half a second. Am I right in thinking that the extra time on the first run is the time it takes to compile .py -> .pyc ? Transcript follows $ try paths.test try ./paths.test; 10 tests passed in 3 seconds 10 tests passed, 0 failed, in 3 seconds $ try paths.test try ./paths.test; 10 tests passed very quickly 10 tests passed, 0 failed, in 0 seconds $ try paths.test try ./paths.test; 10 tests passed very quickly 10 tests passed, 0 failed, in 0 seconds -- Alan -- http://mail.python.org/mailman/listinfo/python-list
Easy caching
Evening all, And thank you for your valuable reading skills. The following pattern turned up in coding tonight. It works, but I'm suspicious, it just seems too easy. So any comments or constructive criticisms welcome ? *** Start doctest format *** >>> class Cacher: ... def __init__(self): ... self.value_in_database = 0 ... ... def very_slow_database_access(self): ... return self.value_in_database ... ... @property ... def show(self): ... result = self.very_slow_database_access() ... self.show = result ... return result ... >>> Create an instance >>> cacher = Cacher() Someone writes to the database >>> cacher.value_in_database = 9 Call show as a method, get the value >>> print cacher.show 9 show is now a cached string >>> print cacher.show 9 show is not a method so if someone else writes to the database we will not see the change >>> cacher.value_in_database = 7 >>> print cacher.show 9 To force another read from the database delete the string attribute >>> del cacher.show Next time we try to use the attribute Python will find the method again >>> print cacher.show 7 *** -- Alan -- http://mail.python.org/mailman/listinfo/python-list
Can I get a value's name
Context for this question arises from some recent code. In particular the "replace_line" method, which takes in a regexp to look for, and a replacement for when it matches. It is supposed to work for single lines only (we add ^ and $ to the regexp), so arguments which have '\n' in them are not accepted. So at start of the method we search for such bad chars, and the code ends up something like this: def replace_line(pattern,replacement): errors = '\n' in pattern and [ 'pattern' ] or [] errors += '\n' in replacement and [ 'replacement' ] or [] values = [ locals()[e] for e in errors ] # etc, etc, and eventually: print 'Argument %s is bad : "%s"' % (errors[0],values[0]) And the question arises from that locals() line: Given a variable name I can use locals() to get the value Is there a way to do it the other way round Given the value, can I get the variable name ? For example, suppose I had started like this (using the variables, not strings with their names) def replace_line(pattern,replacement): values = '\n' in pattern and [ pattern ] or [] values += '\n' in replacement and [ replacement ] or [] Can I later get the name "pattern" via values[0]? If this was an array in another language: Of course not, values[0] is a copy of the value so the connection to the variable is lost But, AFAIK, in Python values[0] is "just a rename" of pattern so there might be a way to get "through to" the original variable Thank you for reading this far. If you were now to start writing, I'd be very grateful indeed. -- Alan P.S. This is just curiosity - replace_lines() works great as is. -- http://mail.python.org/mailman/listinfo/python-list