how to replace some methods with instrumented ones
[python 2.7, linux] I have a python app. I cannot modify the file. But I can import it and mess with it. I need to perform brief tasks before and after some of the member functions. I'd like to do this in as clear and maintainable way as possible (no third party imports). Here's what I have now(which works): from newvbr import * #get all the stuff from the newvbr app. def perf_time(tag): # I do a little more than this, but you get the idea. print sys.stderr, datetime.datetime.now().isoformat() + tag def perf(tag): def decorator(fn): def wrapper(*args): perf_time('start %s' % tag) ret = fn(*args) perf_time('end %s' % tag) return ret return wrapper return decorator I use this on various members of various classes in the newvbr app: old_database_snapshot = VerticaSession.database_snapshot @perf('db_snap') def new_database_snapshot(self, name): old_database_snapshot(self, name) VerticaSession.database_snapshot = new_database_snapshot old_object_snapshot = VerticaSession.object_snapshot @perf('obj_snap') def new_object_snapshot(self, name): old_object_snapshot(self, name) VerticaSession.object_snapshot = new_object_snapshot main() #do the usual stuff of the imported app, but with annotations. This is still pretty laborious. How can I make something roughly like: super_tagger(VerticaSession.database_snapshot) that does all the above? I need to tag a bunch of function members like this. I don't insist on using decorators, it just looked like that kind of problem. -- https://mail.python.org/mailman/listinfo/python-list
case-sensitive configparser without magical interpolation?
[python 2.7] I need to use a configparser that is case-sensitive for option names, but does not do magical interpolation of percent sign. I.e.: [Mapping0] backupHost = eng%26 dbNode = v_br_node0001 should be read (and later written) as is, including capitalization and the percent sign. I find that RawConfigParser keeps the %, but downcases the option name. And SafeConfigParser can be hacked with optionxform to be case-sensitive, but does interpolation. How can I get case-sensitive but no interpolation? -- George -- https://mail.python.org/mailman/listinfo/python-list
Re: case-sensitive configparser without magical interpolation?
On Friday, May 22, 2015 at 1:13:39 PM UTC-4, Ian wrote: On Fri, May 22, 2015 at 10:59 AM, gy wrote: [python 2.7] I need to use a configparser that is case-sensitive for option names, but does not do magical interpolation of percent sign. I.e.: [Mapping0] backupHost = eng%26 dbNode = v_br_node0001 should be read (and later written) as is, including capitalization and the percent sign. I find that RawConfigParser keeps the %, but downcases the option name. And SafeConfigParser can be hacked with optionxform to be case-sensitive, but does interpolation. How can I get case-sensitive but no interpolation? RawConfigParser also has the optionxform method; have you tried overriding that? If that doesn't work, then how strict is the 2.7 requirement? In 3.2 or later, the ConfigParser takes an interpolation keyword argument that can be used to disable interpolation: https://docs.python.org/3.4/library/configparser.html#configparser-objects That worked perfectly, thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: question about loading variables from a file...
On Nov 5, 2:26 pm, avidfan [EMAIL PROTECTED] wrote: Can someone offer some advice as to how the best way to approach this might be? I am trying to write a generic python script to build out some applications, so the python script will be generic enough to work for all of them, but it needs to 'source' a file that contains app and environment specific variables. I have used WLST, which provides the loadProperties() method for reading values for weblogic domains, but I'm trying to find the best way to do the same sort of thing in python... Try configparser: http://docs.python.org/lib/module-ConfigParser.html -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to check if a file exists?
On Oct 31, 4:01 pm, John Salerno [EMAIL PROTECTED] wrote: What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name. You could be more pythonic, and simply try to create the file, catching the exception if if fails. This works on linux: try: newfd = os.open('foobar', os.O_EXCL | os.O_CREAT) new_file = os.fdopen(newdf) except OSError, x: if x[1] == 'File exists': handle_file_exists() [but beware unreliable on an NFS file system, from man open: O_EXCL is broken on NFS file systems, programs which rely on it for performing locking tasks will contain a race condition. The solution for performing atomic file locking using a lockfile is to create a unique file on the same fs (e.g., incorporating hostname and pid), use link(2) to make a link to the lockfile. If link() returns 0, the lock is successful. Otherwise, use stat(2) on the unique file to check if its link count has increased to 2, in which case the lock is also successful.] -- http://mail.python.org/mailman/listinfo/python-list
what is @param in docstrings?
I'm starting to read about twisted and I keep seeing things like: [from twisted/internet/app.py] def __init__(self, name, uid=None, gid=None, authorizer=None, authorizer_=None): Initialize me. If uid and gid arguments are not provided, this application will default to having the uid and gid of the user and group who created it. @param name: a name @param uid: (optional) a POSIX user-id. Only used on POSIX systems. @param gid: (optional) a POSIX group-id. Only used on POSIX systems. _AbstractServiceCollection.__init__(self) self.name = name ... What does the @param mean? It looks like something meant to be machine readable. Alas, googling on @param doesn't work... It looks at first like a decorator, but that doesn't make much sense. -- George Young -- http://mail.python.org/mailman/listinfo/python-list
Re: User Access to the docstring of a property
Colin J. Williams wrote: Is there some way that the user can access the docstring specified for a property? Do keep in mind that the docstring is not guaranteed to be available. If the application is run with optimization turned on, docstrings are usually optimized out. Docstrings are handy for reading code and maybe for debugging, but should not be relied upon for users, as opposed to developers. -- George Young Please see the example below: # propDocTest class A(object): def __init__(self, value): self.value= value def vGet(self): return self.value V= property (fget= vGet, doc=Get Value.) a= A(22) print a.vGet() print a.V print a.V.__doc__ # this gives the docstring for the value returned help(a.V) # this gives the docstring for the class/type of the value returned Colin W. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flexable Collating (feedback please)
On Oct 18, 2:42 am, Ron Adam [EMAIL PROTECTED] wrote: I put together the following module today and would like some feedback on any obvious problems. Or even opinions of weather or not it is a good approach. ,,, def __call__(self, a, b): This allows the Collate class work as a sort key. USE: list.sort(key=Collate(flags)) return cmp(self.transform(a), self.transform(b)) You document _call__ as useful for the key keyword to sort, but you implement it for the cmp keyword. The key allows much better performance, since it's called only once per value. Maybe just : return self.transform(a) -- George -- http://mail.python.org/mailman/listinfo/python-list
why should dict not be callable?
A couple of times recently I've come across this problem: I have a large list to sort and I need to the the key=function argument to sort appropriately. But I actually have the key mapping in a big dictionary. Now I have to make an intermediary function: def key_fn(key): return key_dict[key] If a dict were callable directly, this would be unnecessary. Often the small extra complexity and compute time is not important, but in sorting a large list, it could be a significant difference. Is there some reason a subscriptable thing like a dict should *not* be callable? -- George [nitpicking...] -- http://mail.python.org/mailman/listinfo/python-list
Re: why should dict not be callable?
On Oct 17, 3:37 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: A couple of times recently I've come across this problem: I have a large list to sort and I need to the the key=function argument to sort appropriately. But I actually have the key mapping in a big dictionary.so use a bound method: L.sort(key=key_dict.get) Duh! Why didn't I think of that... Thanks guys, that's perfect. -- George -- http://mail.python.org/mailman/listinfo/python-list
Re: returning None instead of value: how to fix?
sam wrote: i am starting to experiment with recursion, and decided to write a fairly trivial little program which took a float as input, then called a function to halve it recursively until it was less than 1: import recursive_halve_module raw_value = raw_input('please enter the number you would like to halve: ') value = float(raw_value) final_value = recursive_halve_module.recursive_halve(value) print final_value raw_input('hit enter to close:') def recursive_halve(value): if value 1: print value return value else: value = value/2 print value if value 1: return value else: recursive_halve(value) ^^^ This needs to be: return recursive_halve(value) You were executing the function, but not returning the result. -- George it works fine apart from printing 'None' instead of 'final_value' at the end. however, if you enter a value that is already less than 1, it prints 'final_value' (i.e. that very same number) just fine. now, it looks to me like only 'value' can be returned: either right at the beginning (for values less than 1) or when you eventually get down to below 1 after x function calls. but clearly that's not the case. i understand that 'None' is returned by default by functions in Python. my question is: how am i slipping into that default despite seemingly (to me at least) avoiding it through explicitly returning something else? thanks in advance, sam -- http://mail.python.org/mailman/listinfo/python-list
Re: get process id...
SpreadTooThin wrote: How does one get the process id? Is there a method for windows and unix (mac os x etc...) under linux, do: import os os.getpid() -- http://mail.python.org/mailman/listinfo/python-list