Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

What is now proved was once only imagin'd. -- William Blake


devel / comp.lang.python / Re: Cyclic imports

SubjectAuthor
o Re: Cyclic importsChris Angelico

1
Re: Cyclic imports

<mailman.421.1629224405.4164.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=14650&group=comp.lang.python#14650

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Cyclic imports
Date: Wed, 18 Aug 2021 04:19:51 +1000
Lines: 47
Message-ID: <mailman.421.1629224405.4164.python-list@python.org>
References: <CAGGBd_qevXB-gk2AfhjJKt-hABaQSs+x3Bo2okNqN2+Go=hrfQ@mail.gmail.com>
<11811693.O9o76ZdvQC@varric.chelsea.private>
<CAPTjJmpy7-KFyq-upY2J=scJBq32MeYMPQM0tDkZEJBgnrmKaQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de aIXTdbvXfddYqCPWN52gyw2rgUVLL1CfPSJb8F3kmX/g==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=lCwTSw3j;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.015
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'def': 0.04; 'fairly':
0.05; 'importerror:': 0.07; '18,': 0.09; 'ast': 0.09; 'aug': 0.09;
'barry': 0.09; 'example.': 0.09; 'module:': 0.09; 'coding': 0.10;
'import': 0.14; '"import': 0.16; '(note': 0.16; 'chrisa': 0.16;
'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16;
'script,': 0.16; 'wrote:': 0.16; 'that.': 0.16; 'python': 0.16;
'maybe': 0.20; "i'd": 0.23; 'to:addr:python-list': 0.23; 'code':
0.24; 'anything': 0.24; '>>>': 0.26; 'depends': 0.26; 'module':
0.28; "isn't": 0.29; 'text': 0.29; 'but': 0.31; 'message-
id:@mail.gmail.com': 0.33; 'received:209.85.166': 0.33; 'header
:In-Reply-To:1': 0.33; 'received:google.com': 0.34; 'words': 0.35;
'from:addr:gmail.com': 0.35; 'trying': 0.36; 'inside': 0.37;
'recommend': 0.37; 'two': 0.37; 'handle': 0.37; 'received:209.85':
0.38; "it's": 0.38; 'something': 0.38; 'received:209': 0.38;
'main': 0.40; 'simple': 0.40; 'pretty': 0.40; 'skip:i 30': 0.60;
'feel': 0.61; 'lot': 0.62; 'miss': 0.63; 'everything': 0.64;
'ignore': 0.66; 'spam': 0.66; 'skip:b 40': 0.69; 'longer': 0.72;
'easy': 0.75; 'little': 0.75; "you'll": 0.75; '....': 0.81;
'2021': 0.84; 'bar,': 0.84; 'easier,': 0.84; 'scott': 0.84;
'skip:" 40': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=pesRrVMXs70RxHXnkuNXc5To6PTO+NrrvzIPZGjIyBk=;
b=lCwTSw3jxgfMELUOA7Gfs1VKEedC9yVgKd7zT838urywyLaEY6B6yVbVaQBNmA4FjV
89PBlA4KPYywSgBbfgnI0mIWZeyAeC+wX1Jmwii0bb441Y0QdgPOu6R7C1f4BT9hirf6
jMmP2EIXivn3W958awOAh4o/mOSFA9ML90xT5PrDWxZGwwFREQyDOCrx7+QdBOmuJ2k7
rQGaQyHLsAAhzABeitHlGqBFLRCg/lP7Xtyvl14nyJdmPiRwFglbZLKXWZbgtGgQGX4j
I5HgSvpnbJvibrN6zA5LDBAHRExpaHSwEwMRL2Nf7ZpvxOrPv+igxmsscpSlRmefX0kb
okow==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=pesRrVMXs70RxHXnkuNXc5To6PTO+NrrvzIPZGjIyBk=;
b=fD4anAFZy0Re4V8X9vxqfIeFlVKzpWj3KldFGeu2lB23sroTjWzh/5l3fdHX4kK1/z
PVO2ED9jwmo9KP7OxPyo9TQMeRxa8GhD9BAdwQ3nm0TYmJhBxdLVSRLRTM1oTz8aIP4r
U8Vxv+u7MAFt9ZJdV+guKXzyLgNvxjWtGMhw+i14ic0wTrlZGaGC2xNt4cuSSBkpeMUu
PZhcn0O0fiOKmqrZodki0JpV9buZ7mwfhVpdFqk9Jf1LOizmmgxOoFQuvvBR8lM8mNu8
4QHeYcCiKAQdzeVL/piMnGCzdofpsYATe6QkwepZil4lctSMx9e9HCN0+m8WHL9Xl3ii
C0wg==
X-Gm-Message-State: AOAM532KommGEHqE7qnkF50btZB1qPeBkEHBjm2cTzuUGJgxQOKjSIN/
n0Fup3cHSkUHBe13EywQOVdyciN1q2mLqIihWHCq7tOeVs8=
X-Google-Smtp-Source: ABdhPJyCCjPX5OwdNpyaSa+pFdwhvFkHi161NVnQc9xhB1ZOvSG4Wb6xnSQmsrbWI1rXUFgFw9G/4UvU17GlKMwWifM=
X-Received: by 2002:a92:da87:: with SMTP id u7mr3355825iln.297.1629224402244;
Tue, 17 Aug 2021 11:20:02 -0700 (PDT)
In-Reply-To: <11811693.O9o76ZdvQC@varric.chelsea.private>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
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: <https://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>
X-Mailman-Original-Message-ID: <CAPTjJmpy7-KFyq-upY2J=scJBq32MeYMPQM0tDkZEJBgnrmKaQ@mail.gmail.com>
X-Mailman-Original-References: <CAGGBd_qevXB-gk2AfhjJKt-hABaQSs+x3Bo2okNqN2+Go=hrfQ@mail.gmail.com>
<11811693.O9o76ZdvQC@varric.chelsea.private>
 by: Chris Angelico - Tue, 17 Aug 2021 18:19 UTC

On Wed, Aug 18, 2021 at 4:10 AM Barry Scott <barry@barrys-emacs.org> wrote:
>
> def allImports( self, module_name ):
> for line in f:
> words = line.strip().split()
> if words[0:1] == ['import']:
> all_imports.append( words[1] )
>

This will work for a lot of programs, but it depends too much on
coding style. If you feel like trying something a little more
adventurous, I'd recommend looking into the ast module:

>>> import ast
>>> ast.parse("""
.... import foo
.... import bar, baz
.... from quux import spam
.... try: import hello
.... except ImportError: import goodbye
.... """)
<ast.Module object at 0x7f83b65398d0>
>>> m = _
>>> ast.dump(m)
"Module(body=[Import(names=[alias(name='foo')]),
Import(names=[alias(name='bar'), alias(name='baz')]),
ImportFrom(module='quux', names=[alias(name='spam')], level=0),
Try(body=[Import(names=[alias(name='hello')])],
handlers=[ExceptHandler(type=Name(id='ImportError', ctx=Load()),
body=[Import(names=[alias(name='goodbye')])])], orelse=[],
finalbody=[])], type_ignores=[])"

If you ast.parse() the text of a Python script, you'll get a Module
that has all the top-level code in a list. It's then up to you how
much you dig into that. For instance, a simple try/except like this is
fairly common, but if something's inside a FunctionDef, you might want
to ignore it. Or maybe just ignore everything that isn't an Import or
FromImport, which would be a lot easier, but would miss the try/except
example.

The main advantage of ast.parse() is that it no longer cares about
code layout, and it won't be fooled by an import statement inside a
docstring, or anything like that. It's also pretty easy to handle
multiple variants (note how "import bar, baz" just has two things in
the names list).

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor