Beyond Inspect

Development, Python April 12th, 2006

Suppose you have the following scenario:

You have a dammmmmmmn long argument list for the function foo:

def foo( A=‘a’, B=‘b’, C=‘c’, … , Z=‘z’ ) :
  url = ‘A=%s&’ % (A) +  ‘B=%s&’ % (B) + … + ‘Z=%s&’ % (Z)

How to make it more user/developer friendly?

In the user side, it would be a nightmare for fill all real arguments. If almost all parameters have default values, is it possible to specify only as few parameters as possilbe and leave others untouched? Positive. Python supports a “dictionary-style” function call interface like this:

foo( F=‘ft’, B=‘bs’)

For the foo function developer, it is still tedious to repeat the boring expressions. Remember, python is an elegant language. Let’s abstract this problem: Is there anyway to access the (name, value) pair of function arguments directly? As you know, python is a pure object-oriented programming language, everything is object, so it is with function. There might be some interface exposed for this purpose.

Not really, the function object does expose several attributes, like func_defaults, which stores the (name, value) pair for default values, and func_name, that records the name of the function itself, but there is no “argument dictionary” at all.

Google, and here is a post discussing inspect module, which is mainly used in debugging. Here is the example to implement the same functionality with more pythonic style:

import inspect
def foo( A=‘a’, B=‘b’, C=‘c’, … , Z=‘z’ ) :
  argv = inspect.getargvalues( inspect.currentframe() )[-1].items();
  url = ‘%’.join( [ ‘%s=%s’ % (k,v) for k,v in argv.items() ] )

NOTE: Ln4 should be called before any local variables declared, otherwise, argv would include the declared local variables before Ln4.

This techanique is used in user interface ( e.g pyaws.ecs.ItemLookup )

UPDATE: A better approach is to use the built-in function vars.

PyAWS on Sf.net

Development, Python April 11th, 2006

Since the gelman project is developed by python, I need a python wrapper for AWS:

PyAmazon is a XML-Object mapping for Amazon Web Service, however, the interface is obsolete and the current maintainer, Michael Josephson does not show any interest to upgrade it.

pyamazon4 seems to address this problem, but so far it is mere a place holder.

OK, I would wrap my sleeves and start this dirty work.

UPDATE: Accepted by Sf.net, Here is the new project PyAWS.

Gelman on Sf.net — Rejected

Desktop, Development, Python April 11th, 2006

Eventually, I could not tolerate myself to postpone the development of gelman, a eBook management system any longer, so I registered a new project in Sf.net: gelman

Gelman is named after the Gelman library in GWU. Both of them are lightweight.

  • All immutable meta data is stored online, aka Amazon Web Service (AWS)
  • User preference is stored in sqlite in-process database
  • Implemented by PyKDE
  • Tag support
  • Script and plug-in support

Here is the proposal to sf.net:

Yet another personal media management system. What makes the gelman distinguishing?

1. plug-in. The user may override the default behavior
2. lightweight.
3. Tag support Organize the media in del.icio.us way
4. scriptable

UPDATE: This project is rejected by sf.net due to the vague description. Since I do not have an developement environment for PyKDE right now, I would revise the proposal and re-submit it later.