[FAQTS] Python Knowledge Base Update -- August 31st, 2000
Fiona Czuczman
fiona at sitegnome.com
Thu Aug 31 01:49:25 EDT 2000
More information about the Python-list mailing list
Thu Aug 31 01:49:25 EDT 2000
- Previous message (by thread): [FAQTS] Python Knowledge Base Update -- August 31st, 2000
- Next message (by thread): Simple Interpolation in Numpy?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hello All, Here are the latest entries into http://python.faqts.com Now that the knowledge base has grown to a substantial number, 773 entries currently, my contract with FAQTS has ended. I will still hang around, I can't be got rid of that easily :-), and add content and maintain the resource (perhaps a little less frequently, sorry :(, if others want to help me out in my quest that would be great. best regards, Fiona Czuczman ## Unanswered Questions ######################################## ------------------------------------------------------------- How to "expire" a web page with Python ? http://www.faqts.com/knowledge-base/view.phtml/aid/5664 ------------------------------------------------------------- Quynh Nguyen Anh ## New Entries ################################################# ------------------------------------------------------------- Is there any online reference document which summarizes/discusses all Tkinter events? http://www.faqts.com/knowledge-base/view.phtml/aid/5646 ------------------------------------------------------------- Fiona Czuczman Fredrik Lundh http://www.pythonware.com/library/tkinter/introduction/events-and-bindin gs.htm briefly describes all important events. There are more events, but they're either rather obscure, or they don't work the same way on all platforms. ------------------------------------------------------------- How can I convert a gif image to a bitmap representation of it? http://www.faqts.com/knowledge-base/view.phtml/aid/5665 ------------------------------------------------------------- Fiona Czuczman Greg Landrum I'm not completely sure what you mean by "bitmap representation", but PIL probably does what you want. :-) To convert to a BMP (windows bitmap), you would do: >>> from Pil import Image >>> img = Image.open('foo.gif') >>> img.save('foo.bmp') If you would rather get a Numeric array with the values from the image: >>> from Pil import Image >>> from Numeric import * >>> img = Image.open('foo.gif') >>> arr = resize(fromstring(img.tostring(),Int8),img.size) I hope this helps. You can find PIL at URL: http://www.pythonware.com/products/pil/index.htm ------------------------------------------------------------- Is it possible to get the declaration of the argument-list of any function in the python engine? just like it is possible to get the func_name or doc-string. http://www.faqts.com/knowledge-base/view.phtml/aid/5666 ------------------------------------------------------------- Fiona Czuczman Alex Martelli Yes; a very good packaging of this you can find in Lemburg's module 'hack', see http://starship.python.net/crew/lemburg/hack.py. Don't get put off by the module's name -- it's just a collection of "tools that I find useful to examine code from inside an interactive interpreter session", as the author says! The functionality you want is packaged as part of function func_sig of this module. It is, of course, instructive to examine exactly what this function does. First of all: def func_sig(func): if hasattr(func,'im_func'): func = func.im_func This takes care of the case where 'func' is not really a function, but rether a method; in this case, the real function is its attribute im_func, so this part "normalizes" this case back to the general case. Then: code = func.func_code fname = code.co_name callargs = code.co_argcount args = code.co_varnames[:callargs] Now we have the function name (fname), and the arguments' names (args). With just this part, we can already, in a sense, satisfy your request: def func_args(func): if hasattr(func,'im_func'): func = func.im_func code = func.func_code fname = code.co_name callargs = code.co_argcount args = code.co_varnames[:callargs] return "%s(%s)" % (fname, string.join(args,',')) and now for example: >>> fa.func_args(string.join) 'join(words,sep)' >>> What we're still missing are the default values of arguments that do have them -- here, for example, the fact that the 'sep' argument can be omitted, because it defaults to space. And also, the ability of some functions to accept unlimited lists of arguments (the *args form, and/or **kw for keyword-arguments). Lemburg goes into slightly deeper waters to add this information, too...: args = list(code.co_varnames[:callargs]) ...list, not tuple, because we may want to change some elements... if func.func_defaults: i = len(args) - len(func.func_defaults) for default in func.func_defaults: try: r = repr(default) except: r = '<repr-error>' if len(r) > 100: r = r[:100] + '...' ...repr is used to get the string with which to represent the default value, with due care for possible errors, and/or too-long representations, which get truncated to 100 chars (with an ellipsis ... to remind the reader they're truncated) arg = args[i] if arg[0] == '.': # anonymous arguments arg = '(...)' args[i] = '%s=%s' % (arg,r) i = i + 1 I don't know what "anonymous arguments" are -- I'm learning of their very existence by studying this code (talk about instructive!-) if code.co_flags & 0x0004: # CO_VARARGS args.append('*'+code.co_varnames[callargs]) callargs = callargs + 1 if code.co_flags & 0x0008: # CO_VARKEYWORDS args.append('**'+code.co_varnames[callargs]) callargs = callargs + 1 As Lemburg notes in his comments, these constants are taken right from the Python compiler's include file. return '%s(%s)' % (fname,string.join(args,', ')) At last -- the nicely formatted result! >>> func_sig(string.join) "join(words, sep=' ')" >>> Just one caveat: this will not work for functions written in C! The "signature" is not available in this case by "introspection" in this way; to see the difference: >>> import string >>> import strop >>> dir(string.join) ['__doc__', '__name__', 'func_code', 'func_defaults', 'func_doc', 'func_globals', 'func_name'] >>> dir(strop.join) ['__doc__', '__name__', '__self__'] >>> i.e., the Python-written 'string.join' wrapper offers the various func_code, etc, members upon which this introspection can be based; the bare-C 'strop.join' underlying level doesn't, so, no real introspection -- you have to rely on the __doc__: >>> print strop.join.__doc__ join(list [,sep]) -> string joinfields(list [,sep]) -> string Return a string composed of the words in list, with intervening occurences of sep. Sep defaults to a single space. (join and joinfields are synonymous) >>> Not too bad, after all:-). ## Edited Entries ############################################## ------------------------------------------------------------- Is there a Python MySQL module for use under Windows? http://www.faqts.com/knowledge-base/view.phtml/aid/4188 ------------------------------------------------------------- Fiona Czuczman, Gerhard Haering Stefan Franke,Steve Cook You will find a version of MySQLdb (version 0.2.1) compiled for Win32 at http://home.t-online.de/home/err666/MySQL/MySQLdb-0.2.1-windows.zip It works fine for me connecting to MySQL 2.23/Win2k and MySQL 2.22/Linux.
- Previous message (by thread): [FAQTS] Python Knowledge Base Update -- August 31st, 2000
- Next message (by thread): Simple Interpolation in Numpy?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list