Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80447

__missing__ for the top-level Python script

Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'represents': 0.05; 'subject:Python': 0.06; 'absent': 0.09; 'assuming': 0.09; 'happen.': 0.09; 'imported': 0.09; 'subject:script': 0.09; 'suppress': 0.09; 'try:': 0.09; 'python': 0.11; 'be:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'importerror': 0.16; 'module?': 0.16; 'nameerror:': 0.16; 'raised,': 0.16; 'semantics': 0.16; 'subject:top': 0.16; 'sys.exit(1)': 0.16; '(not': 0.18; 'successful,': 0.19; 'work,': 0.20; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'idea': 0.28; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; 'implicit': 0.31; 'intellectual': 0.31; 'raised': 0.31; 'open': 0.33; 'subject:the': 0.34; 'except': 0.35; 'something': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done': 0.36; 'possible': 0.36; 'should': 0.36; 'implement': 0.38; 'to:addr :python-list': 0.38; 'bad': 0.39; 'extremely': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'exercise,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=6NDaiZLzrJ0dzvZUIGxEMLhdWfUn6BgnLl4zvyLFkKY=; b=lPk/ho1rShyilSOx16R1Sl3eBBrfyjhWBOMN7tLU9+oOkUyn2BnV3L5Dr35M76eGby MpByAzQTi3OsdqcZ68hbYsGcTjUlZZ6F/+fKgPxHTqTfurSBSSIeprOHPhGHEJr4/Tba o922oBEljT86AlFYvIOL4PlZAIjct/AGnVj1rOQi0NHacXk/h3WmspwVOlvulKf6L4mE hGXyiu7HIRoOK2QNVQEjzaOilqd9s24Bz9zPTfu42ckOaB9P+y6twYgyjK0jUyfy3Bz5 pQd3txKKkFf/3LYE1eiNNSDgVE8TLV6F1gP+GAOzx5pk+PWweFNRpf1kkNrJFPAZSrGy yMEw==
MIME-Version 1.0
X-Received by 10.107.128.106 with SMTP id b103mr14023408iod.42.1415575881498; Sun, 09 Nov 2014 15:31:21 -0800 (PST)
Date Mon, 10 Nov 2014 10:31:21 +1100
Subject __missing__ for the top-level Python script
From Chris Angelico <rosuav@gmail.com>
To "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15613.1415575883.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415575883 news.xs4all.nl 2930 [2001:888:2000:d::a6]:34275
X-Complaints-To abuse@xs4all.nl
Path csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Xref csiph.com comp.lang.python:80447

Show key headers only | View raw


Let's have some fun nutting out possible implementations for a bad idea :)

If you want a dictionary that prepopulates itself on demand, you
implement __missing__. Is there a way to implement the same thing for
the __main__ module? Since it isn't imported (as such), I don't think
"switch out what's in sys.modules" will work, though I'm open to
correction on that.

Desired result: Implicit imports.

## test.py
print("Path separator is",os.sep)
try:
    print("Version", psycopg2.__version__, "of psycopg2 is installed.")
except NameError:
    print("You don't have psycopg2 installed.")
sys.exit(1)

So the semantics should be: If NameError would be raised (not
including UnboundLocalError, which still represents an error), attempt
to import the absent name. If successful, continue as if it had
already been done. If ImportError is raised, suppress it and let the
original NameError happen.

Yes, this is extremely unPythonic. But just as an intellectual
exercise, can this be done?

I'm assuming Python 3 here, so if there's something neat that can be
done with import hooks, go for it.

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

__missing__ for the top-level Python script Chris Angelico <rosuav@gmail.com> - 2014-11-10 10:31 +1100

csiph-web