Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #16385 > unrolled thread
| Started by | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| First post | 2011-11-29 09:51 +0000 |
| Last post | 2011-12-02 17:01 +0000 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: python 2.5 and ast Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-11-29 09:51 +0000
Re: python 2.5 and ast Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-29 11:32 +0000
Re: python 2.5 and ast Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-11-29 13:51 +0000
Re: python 2.5 and ast Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-11-30 18:03 +0000
Re: python 2.5 and ast DevPlayer <devplayer@gmail.com> - 2011-12-02 07:18 -0800
Re: python 2.5 and ast Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-02 16:54 +0000
Re: python 2.5 and ast Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-12-02 17:01 +0000
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2011-11-29 09:51 +0000 |
| Subject | Re: python 2.5 and ast |
| Message-ID | <mailman.3121.1322560291.27778.python-list@python.org> |
On 11/29/2011 03:55 AM, Dave Angel wrote:
>
> But don't forget to tag it as version specific, so it gets removed
> when the later version of the library is available. There are various
> ways of doing that, but the easiest is probably to put a test in the
> acceptance suite that fails if this code is used in 2.6 or later.
>
Ok thanks,
something like this is ok?
(or maybe I can use a try/catch and export my own if is not found in the
standard library?)
from sys import version_info
if version_info[1] == 5:
from psi.devsonly.ast import parse, NodeVisitor
else:
from ast import parse, NodeVisitor
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-11-29 11:32 +0000 |
| Message-ID | <4ed4c2ce$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #16385 |
On Tue, 29 Nov 2011 09:51:24 +0000, Andrea Crotti wrote:
> On 11/29/2011 03:55 AM, Dave Angel wrote:
>>
>> But don't forget to tag it as version specific, so it gets removed when
>> the later version of the library is available. There are various ways
>> of doing that, but the easiest is probably to put a test in the
>> acceptance suite that fails if this code is used in 2.6 or later.
>>
>>
> Ok thanks,
> something like this is ok?
> (or maybe I can use a try/catch and export my own if is not found in the
> standard library?)
>
> from sys import version_info
>
> if version_info[1] == 5:
> from psi.devsonly.ast import parse, NodeVisitor
> else:
> from ast import parse, NodeVisitor
I prefer to check against sys.version.
import sys
if sys.version <= '2.5':
from psi.devsonly.ast import parse, NodeVisitor
else:
from ast import parse, NodeVisitor
Or even:
try:
from ast import parse, NodeVisitor
except ImportError:
from ast import parse, NodeVisitor
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2011-11-29 13:51 +0000 |
| Message-ID | <mailman.3124.1322574694.27778.python-list@python.org> |
| In reply to | #16387 |
On 11/29/2011 11:32 AM, Steven D'Aprano wrote: > > I prefer to check against sys.version. > > import sys > if sys.version<= '2.5': > from psi.devsonly.ast import parse, NodeVisitor > else: > from ast import parse, NodeVisitor > > > > Or even: > > > try: > from ast import parse, NodeVisitor > except ImportError: > from ast import parse, NodeVisitor > > > The try/except is probably the nicest... I've seen in other places using sys.version <=, but is it a good idea to rely on the fact that the <= on strings has the right semantic? After all that's just a string, version_info looks more correct to me..
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2011-11-30 18:03 +0000 |
| Message-ID | <mailman.3166.1322676221.27778.python-list@python.org> |
| In reply to | #16387 |
Another thing about the AST, I am having fun trying to for example list out all the unused imports. I have already a visitor which works quite nicely I think, but now I would like to get a way to find all the unused imports, so I need more visitors that find out all the used names. I didn't find too much documentation about these kind of things, so any suggestions on how to approach? Pylint and snakefood have similar code, but they use the old "compiler" module, so it's not really helpful.
[toc] | [prev] | [next] | [standalone]
| From | DevPlayer <devplayer@gmail.com> |
|---|---|
| Date | 2011-12-02 07:18 -0800 |
| Message-ID | <4bbaa89c-55d2-40d1-91fb-cb00c3f2239a@s4g2000yqk.googlegroups.com> |
| In reply to | #16443 |
On Nov 30, 1:03 pm, Andrea Crotti <andrea.crott...@gmail.com> wrote: > Another thing about the AST, I am having fun trying to for example list > out all > the unused imports. > > I have already a visitor which works quite nicely I think, but now I > would like > to get a way to find all the unused imports, so I need more visitors that > find out all the used names. > > I didn't find too much documentation about these kind of things, so any > suggestions on how to approach? > > Pylint and snakefood have similar code, but they use the old "compiler" > module, > so it's not really helpful. There was another topic in these forums recently about "un-importing" modules (and how you can not do that reliably without restarting python). There was various ways mentioned of keeping track of what was imported. And there was mentioned reasonable ways of finding all possible imports on a computer. By "unused imports" I assume you mean "all unused imports in the application's source code" as opposed to meaning "all unused modules/ packages/libraries on that computer." Personally I'd love to see more tutorials on the AST module; An AST for Dummies. Pretty much the tutorials talk about parsing an expression like "1+2=3". But I'd like to see how blocks are compiled/ managed by the indent/dedent tokens and how the complete algorithm for finding qouted strings is implimented (using really simple explanations). Some google buzzwords to help with your search for your question: sys import cache, import hook, pydoc walkpackages(). And I just found this little tool; never knew about it: C:\PythonXX \Tools\Scripts\pydocui.pyw
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2011-12-02 16:54 +0000 |
| Message-ID | <mailman.3230.1322844878.27778.python-list@python.org> |
| In reply to | #16545 |
On 12/02/2011 03:18 PM, DevPlayer wrote:
> There was another topic in these forums recently about "un-importing"
> modules (and how you can not do that reliably without restarting
> python). There was various ways mentioned of keeping track of what was
> imported. And there was mentioned reasonable ways of finding all
> possible imports on a computer.
>
> By "unused imports" I assume you mean "all unused imports in the
> application's source code" as opposed to meaning "all unused modules/
> packages/libraries on that computer."
>
> Personally I'd love to see more tutorials on the AST module; An AST
> for Dummies. Pretty much the tutorials talk about parsing an
> expression like "1+2=3". But I'd like to see how blocks are compiled/
> managed by the indent/dedent tokens and how the complete algorithm for
> finding qouted strings is implimented (using really simple
> explanations).
>
> Some google buzzwords to help with your search for your question: sys
> import cache, import hook, pydoc walkpackages().
>
> And I just found this little tool; never knew about it: C:\PythonXX
> \Tools\Scripts\pydocui.pyw
>
Yes I meant unused imports in each of the modules actually...
The problem for me is to actually understand what are all the possible
AST node that can involve the use of an actual import, and I didn't
find anything helpful aboupt that.
The ASDL definition says everything in theory but without some examples
is a bit hard.
I got something nice however:
http://stackoverflow.com/questions/8340567/python-ast-to-dot-graph/8342383#8342383
But on windows and Python 2.5 nothing is working :/, even if I blandly
copied the
ast.py from the python 2.7 code file..
File "h:\long\path\devsonly\ast.py", line 166, in iter_fields
for field in node._fields:
TypeError: 'NoneType' object is not iterable
[toc] | [prev] | [next] | [standalone]
| From | Andrea Crotti <andrea.crotti.0@gmail.com> |
|---|---|
| Date | 2011-12-02 17:01 +0000 |
| Message-ID | <mailman.3231.1322845312.27778.python-list@python.org> |
| In reply to | #16545 |
And on a related topic, how can I actually detect other types of
imports, for example
__import__
Doing a dump I get this:
In [113]: ast.dump(ast.parse('__import__("module")'))
Out[113]: "Module(body=[Expr(value=Call(func=Name(id='__import__',
ctx=Load()), args=[Str(s='module')], keywords=[], starargs=None,
kwargs=None))])"
So the visitor should be quite smart, and the string passed can't be
always be done.
I think the easiest way is just to use regexp and look for them warning
the user that there might be other
stupid imports, the harder way is to try to detect it and if the string
passed is actually known at compile-time
use it, otherwise warn the user.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web