[Python-ideas] Add __main__ for uuid, random and urandom

Random832 random832 at fastmail.com
Sat Apr 2 04:22:15 EDT 2016


On Sat, Apr 2, 2016, at 02:37, Koos Zevenhoven wrote:
>  python -e "random.randint(0,10)"

#!/usr/bin/env python3
import sys
class magicdict(dict):
    def __getitem__(self, x):
        try:
            return super().__getitem__(x)
        except KeyError:
            try:
                mod = __import__(x)
                self[x] = mod
                return mod
            except ImportError:
                raise KeyError

g = magicdict()
for arg in sys.argv[1:]:
    try:
        p, obj = True, eval(arg, g)
    except SyntaxError:
        p = False
        exec(arg, g)
    if p:
        sys.displayhook(obj)


Handling modules inside packages is left as an exercise for the reader. 


More information about the Python-ideas mailing list