[Python-3000] A few small py3k wishes
Ron Adam
rrr at ronadam.com
Mon Apr 3 03:19:29 CEST 2006
Talin wrote:
> -- An easy way to iterate over key, value pairs in a dict in
> sorted order. Currently you have to get the list of keys,
> sort them, and then lookup each value, OR you have to
> get the list of tuples and call sorted() with a key= arg
> containing a lambda function that extracts the first
> tuple element.
What you probably want here instead is an ordered dict, and not a sorted
dict. It's easy enough to sort the keys, but maintaining an other than
sorted order, such as the order the items were added to the dict, or the
order in which you will need the items from a dict isn't as easy and
requires keeping a duplicate list of the keys.
> -- The module class should have a method to iterate
> over child modules. Currently you can iterator through
> all of its attributes, but you have to filter out which ones
> are modules.
Is this what you are looking for?
>>> import inspect
>>> import __main__
>>> inspect.getmembers(__main__, inspect.ismodule)
[('__builtins__', <module '__builtin__' (built-in)>), ('__main__',
<module '__main__' (built-in)>), ('inspect', <module 'inspect' from
'C:\Python24\lib\inspect.pyc'>)]
This is how PyDoc does it. I just happen to be in the middle of
attempting to rewrite it so it's more modular and easier to extend. ;-)
Cheers,
Ron
More information about the Python-3000
mailing list