[Python-3000] A few small py3k wishes

Tim Hochberg tim.hochberg at ieee.org
Mon Apr 3 00:11:09 CEST 2006


Talin wrote:
> Just a few things that have wanted while doing python
> programming. Maybe some of these are already doable
> or are already on someone's wish list...

Some of these are already pretty easy to do. Perhaps you should
explain what's inadequate about the current solutions. For
example:


> -- The __main__ module should contain a file path like imported
> modules. in other words, every module should know where it
> came from. In fact, I see no reason why the __main__ module
> should have any different attributes than an imported module
> other than the name.
> 
> -- An easy way to make a case-insensitive, case-preserving
> dict that works with regular string keys.
> 
> -- 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.

for key in sorted(adict.keys()):
    value = adict[key]
    # ...

It would also be trivial to define your own generator function to do 
this if you are doing it a lot:

def keysorted_items(obj):
     return ((x, obj[x]) for x in sorted(obj.keys()))

for (key, value) in keysorted_items(adict):
     # ...



> 
> (Another one of my wild ideas was an "order by"
> clause for list comprehensions, but let's not go there.)
> 
> -- A simple way to import all modules in a directory
> (this would be used for plugins)
> 
> -- A mechanism whereby imported modules can import
> symbols from the module that imported them. (Specifically,
> I want to take a bunch of variables in my __main__ module
> and make them accessible to the imported module.)

For this specific use, can't you just use "import __main__"  and look at 
the symbols that way?

> 
> -- 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.

Why? In other words, what wrong with:

for key, value in module.__dict__.items():
    if is_module(value):
       # do something?

Or again, define your own generator if you do this a lot:

def submodules(obj):
    for key, value in obj.__dict__.items():
        if is_module(value):
           yield key, value

for name, module in submodules(mymodule):
     # do something.


> 
> -- A path-globbing function that supports regex-style
> captures.
> 
> -- A path-globbing function that supports the
> perforce-style syntax "..." (three dots) to mean "all descendants".
> So for example, "foo/.../*.cpp" matches any cpp files in
> foo or any of its subdirectories.
> 
> That should be enough for the moment.


Regards,

-tim



More information about the Python-3000 mailing list