Re: Parsing a commandline from within Python
>> Is there any buildin function which mimics the behavior of the >> standard commandline parser (generating a list of strings >> "foo bar" and "some text" from the commandline >> <"foo bar" "some text">)? > > Try the shlex module:: > > >>> import shlex > >>> shlex.split('"foo bar" "some text"') > ['foo bar', 'some text'] > Thanks, that is exactly what i need. Andreas Huesgen -- http://mail.python.org/mailman/listinfo/python-list
Parsing a commandline from within Python
Hello, I am writing a commandline tool in Python which is often feed with lots of commandline arguments. In practice, the commandline already reached a critical length which is to long for Windows (some versions of Windows only support commandlines of up to 2047 characters). To work around this problem, we thought up the idea of bypassing the commandline into a file and then letting Python parse the content of the file the same way, the arguments in sys.argv are parsed. Is there any buildin function which mimics the behavior of the standard commandline parser (generating a list of strings "foo bar" and "some text" from the commandline <"foo bar" "some text">)? If not, do you have any other ideas how to handle this problem (increasing commandline length, xml files might be a way)? Regards, Andreas Huesgen Viel oder wenig? Schnell oder langsam? Unbegrenzt surfen + telefonieren ohne Zeit- und Volumenbegrenzung? DAS TOP ANGEBOT FÜR ALLE NEUEINSTEIGER Jetzt bei Arcor: günstig und schnell mit DSL - das All-Inclusive-Paket für clevere Doppel-Sparer, nur 34,95 inkl. DSL- und ISDN-Grundgebühr! http://www.arcor.de/rd/emf-dsl-2 -- http://mail.python.org/mailman/listinfo/python-list
Find out the name of a variable passed as an argument
Hello everybody, is there a way to receive the name of an object passed to a function from within the function. something like def foo(param): print theNameOfTheVariablePassedToParam var1 = "hello" var2 = "world" >>> foo(var1) var1 >>> foo(var2) var2 thanks in advance, greets Andreas Huesgen -- http://mail.python.org/mailman/listinfo/python-list
Re: python reference counting and exceptions
Delaney, Timothy (Tim) wrote: > RIIA - Resource Initialisation is Acquisition > > Python is adding an RIIA mechanism in 2.5 - look at the "with" > statement. > Ah, thanks, thats exactly what I was looking for. Gabriel Genellina wrote: > Yes: the try/finally construct, which is *not* the same as a try/except. The > finally clause is always executed, whether or not an exception is raised. Yes, of course it must be the try finally construct and not try/except. Shame on me ;) Andreas Huesgen -- http://mail.python.org/mailman/listinfo/python-list
python reference counting and exceptions
Hello, I have a question refering python's reference counting/garbage collection in combination with thrown exceptions. I'm very new to this mailinglist, so I hope, that this question has not been asked and answered before. In c++, it is possible to write a locking system similar to the one below: void myFunction() { # create a resource lock. Locks some resource ResourceLock myLock; # the following line may throw an exception doStuff(); } myLock is a stack variable which is destroyed as soon as the function is left (either by returning or because of an uncatched exception). As myLock is destroyed, the destructor will automatically unlock the resource. The following python code has the same effect (at least on my system and my python implementation). class MyLock(object): def __init__(self): print "MyLock.__init__" def __del__(self): print "MyLock.__del__" def myFunction(): print "prelock" lock = MyLock() print "postlock" print "prefun" try: myFunction() except Exception, e: print str(e) print "postfun" output is: prefun prelock MyLock.__init__ postlock MyLock.__del__ postfun MyLock.__init__ MyLock.__del__ However, when myFunction raises an exception, lock is not destroyed as soon as the function is exited but only at the end of the script My question is: is there some reliable way to mimic the c++ code snipped above in python without adding a try-except-unlock-rethrow block around every peace of code that locks some resources. Greets, Andreas Huesgen -- http://mail.python.org/mailman/listinfo/python-list