[FAQTS] Python Knowledge Base Update -- October 9th, 2000
Hi All,
It's been a while. Sorry, I've taken a full time job which doesn't
give me much time to play with the knowledge base ..
This post is simply what's happened since my last post.
regards,
Fiona Czuczman
Unanswered Questions :
- How do I change the name of a process (as viewed by 'ps') from Python?
- How can I get my _full_ program name (eg. "mylongpythonscript.py")
under eg. NT? sys.argv[0] gives me the truncated MS-DOS
style, "MYLONG~1.PY" etc.
- What are the criteria I should consider when deciding between Numeric
Python, Matlab or Octave?
- getting full program name via sys.argv[0] on (eg.) NT?
- is there code that sends the contents of a directory as a multi-part
mime message to e-mail recipients?
Answered Questions :
- Alternative to os.path.walk
- How do I find reference count errors?
- Python's big, is there a crisp overview? a quick reference card? a
bare bones documentation?
- Of what use is 'lambda'?
- How do I handle command line args with gnome?
## Unanswered Questions ########################################
-------------------------------------------------------------
How do I change the name of a process (as viewed by 'ps') from Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/6149
-------------------------------------------------------------
Adam Feuer
-------------------------------------------------------------
How can I get my _full_ program name (eg. "mylongpythonscript.py") under eg. NT? sys.argv[0] gives me the truncated MS-DOS style, "MYLONG~1.PY" etc.
http://www.faqts.com/knowledge-base/view.phtml/aid/6159
-------------------------------------------------------------
Jon Nicoll
-------------------------------------------------------------
What are the criteria I should consider when deciding between Numeric Python, Matlab or Octave?
http://www.faqts.com/knowledge-base/view.phtml/aid/5990
-------------------------------------------------------------
Louis Luang
-------------------------------------------------------------
getting full program name via sys.argv[0] on (eg.) NT?
http://www.faqts.com/knowledge-base/view.phtml/aid/6158
-------------------------------------------------------------
Jon Nicoll
-------------------------------------------------------------
is there code that sends the contents of a directory as a multi-part mime message to e-mail recipients?
http://www.faqts.com/knowledge-base/view.phtml/aid/5977
-------------------------------------------------------------
Wolfgang Lipp
## New Entries #################################################
-------------------------------------------------------------
Alternative to os.path.walk
http://www.faqts.com/knowledge-base/view.phtml/aid/6000
-------------------------------------------------------------
Daniel Dittmar
If you have trouble specifying the correct callback to os.path.walk and
would prefer to use an iterator as in
for fname in RecursiveFileIterator ('dir1', 'dir2'):
process (fname)
class RecursiveFileIterator:
def __init__ (self, *rootDirs):
self.dirQueue = list (rootDirs)
self.includeDirs = None
self.fileQueue = []
def __getitem__ (self, index):
while len (self.fileQueue) == 0:
self.nextDir ()
result = self.fileQueue [0]
del self.fileQueue [0]
return result
def nextDir (self):
dir = self.dirQueue [0] # fails with IndexError, which is fine
# for iterator interface
del self.dirQueue [0]
list = os.listdir (dir)
join = os.path.join
isdir = os.path.isdir
for basename in list:
fullPath = join (dir, basename)
if isdir (fullPath):
self.dirQueue.append (fullPath)
if self.includeDirs:
self.fileQueue.append (fullPath)
else:
self.fileQueue.append (fullPath)
-------------------------------------------------------------
How do I find reference count errors?
http://www.faqts.com/knowledge-base/view.phtml/aid/6006
-------------------------------------------------------------
Joseph VanAndel
Will Ware
x = make_stack() x('push', 3) x('tos') 3 x('push', 4) x('tos') 4 x('pop') x('tos') 3 x('pop') x('tos') Traceback (innermost last): File "<stdin>", line 1, in ? File "<stdin>", line 2, in <lambda> File "<stdin>", line 5, in stack IndexError: list index out of range x('unknown') def hello(): ... print "hello, world" ... x = make_stack(lambda op, arg: hello()) x('push', 37) x('tos') 37 x('unknown arg') hello, world
Just as you can create a lambda that acts like an object, you can create an object that emulates a lambda, simply by creating an object with only one method. Both are kind of awkward. (It gets a lot worse when you start trying to do multiple inheritance with lambdas, closures with objects, etc.) (Also, even without closures, function pointers allow you to simulate objects in languages that really have only structs: C, JavaScript, and Lua come to mind, and of these, JavaScript and Lua have syntactic sugar to make it easier. In C, you have to say things like obj->method(obj, arg).) So both approaches are equally powerful; but both of them are more convenient for expressing certain kinds of algorithms. Lambdas seem to be especially convenient for expressing non-mutative stateless algorithms: map, filter, zip. Objects seem to be especially convenient for writing things with lots of state that can be peacefully extended later on. Lambdas make it possible to have really private attributes, even in libertine[0] languages like Perl, JavaScript, and Python. Well, maybe not in Python --- I don't know it well enough yet, but I wouldn't be surprised if there were a way to get at default argument values ;) In Perl (and, I think, JavaScript), you can use closures to have really private methods, too. This has the unfortunate disadvantage that if the methods are potentially recursive, the closures will have circular references and screw up the reference-counting garbage collection, so you have to deallocate things by hand or leak memory. Sometimes it's clearer to write a function in-line in a lambda, even when you're not binding any data into a closure. "sort" is the usual example; you could define an interface (or protocol) "comparator" consisting of a method "compare" which compared two objects in some user-defined way, and then specify your sort routine to require an object implementing "comparator" which compares objects. This has the disadvantage that the order you want the data in is specified in a different class from the one that wants it sorted, which often means that it's actually in a different file --- by convention or by language requirement. Defining a comparator function is often much easier --- you can usually put it in the same file. But, in many languages, you still have to put it outside of the routine that wants the sorting done --- which means that it's ten, twenty, forty lines away from the sort call. Being able to write the function in-line in a lambda keeps related things together and unrelated things apart, which makes your code easier to read, easier to write, easier to find bugs in, and easier to improve. ------------------------------------------------------------- How do I handle command line args with gnome? http://www.faqts.com/knowledge-base/view.phtml/aid/6117 ------------------------------------------------------------- Joseph VanAndel frederic.gedin@free.fr You need to remove from sys.argv the options you processed before invoking the command line option parser from gnome. E.g: import sys,getopt usageStr =\ """-i input_dir [-o output_dir] -r [-- gnome_args] -r specifies real_time mode (default is batch processing) """ # we have to parse arguments before importing gnome.ui, which also wants # to parse arguments. if __name__ == "__main__": global optlist try: optlist, args = getopt.getopt(sys.argv[1:], 'i:o:r') except: detail = sys.exc_info() print detail[0], detail[1] print __doc__ print 'usage : %s %s' % (sys.argv[0], usageStr) sys.exit(-1) # pass remaining arguments to gnome.ui sys.argv = list((sys.argv[0],)) for a in args: sys.argv.append(a) from gnome.ui import * from gtk import * from gnome.config import * # Applications arguments can now be processed from 'optlist'.
participants (1)
-
Fiona Czuczman