Merge lp:~andrew-monkeysailor/python-snippets/ast-snippets into lp:~jonobacon/python-snippets/trunk

Proposed by Monkeysailor
Status: Needs review
Proposed branch: lp:~andrew-monkeysailor/python-snippets/ast-snippets
Merge into: lp:~jonobacon/python-snippets/trunk
Diff against target: 51 lines (+35/-0)
2 files modified
AST/ASTcount.py (+34/-0)
CATEGORIES (+1/-0)
To merge this branch: bzr merge lp:~andrew-monkeysailor/python-snippets/ast-snippets
Reviewer Review Type Date Requested Status
Jono Bacon Needs Fixing
Review via email: mp+23144@code.launchpad.net

Description of the change

Adds AST folder for AST module examples, includes an example based on finding import statements in a python file.

To post a comment you must log in.
Revision history for this message
Jono Bacon (jonobacon) wrote :

Thanks for the snippet, pal!

I am getting:

Traceback (most recent call last):
  File "/usr/share/python-snippets/AST/ASTcount.py", line 28, in <module>
    f = open("parser.py","r")
IOError: [Errno 2] No such file or directory: 'parser.py'

could you take a look?

review: Needs Fixing
83. By user <user@user-PC>

corrected rudimentary execution error.

Revision history for this message
Monkeysailor (andrew-monkeysailor) wrote :

should be fixed now.

Revision history for this message
Jono Bacon (jonobacon) wrote :

Thanks for the re-submission. Now I am getting:

Traceback (most recent call last):
  File "/usr/share/python-snippets/AST/ASTcount.py", line 33, in <module>
    counter = ASTcount(x)
  File "/usr/share/python-snippets/AST/ASTcount.py", line 13, in __init__
    self.source = ast.parse(txt)
  File "/usr/lib/python2.6/ast.py", line 37, in parse
    return compile(expr, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 7

    ^
SyntaxError: invalid syntax

Revision history for this message
Jono Bacon (jonobacon) :
review: Needs Fixing
Revision history for this message
Monkeysailor (andrew-monkeysailor) wrote :

On 24 April 2010 19:28, Jono Bacon <email address hidden> wrote:
> Review: Needs Fixing
>
> --
> https://code.edge.launchpad.net/~andrew-monkeysailor/python-snippets/ast-snippets/+merge/23144
> You are the owner of lp:~andrew-monkeysailor/python-snippets/ast-snippets.
>

Weird... it's working for me.

Unmerged revisions

83. By user <user@user-PC>

corrected rudimentary execution error.

82. By user <user@user-PC>

AST snippet

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'AST'
2=== added file 'AST/ASTcount.py'
3--- AST/ASTcount.py 1970-01-01 00:00:00 +0000
4+++ AST/ASTcount.py 2010-04-10 20:43:22 +0000
5@@ -0,0 +1,34 @@
6+# [SNIPPET_NAME: Reading AST]
7+# [SNIPPET_CATEGORIES: AST]
8+# [SNIPPET_DESCRIPTION: Scans the AST to find import statements]
9+# [SNIPPET_AUTHOR: Andrew Lewis <andrew@monkeysailor.co.uk>]
10+# [SNIPPET_DOCS:]
11+# [SNIPPET_LICENSE: GPL]
12+
13+import ast, _ast
14+
15+class ASTcount():
16+ """ Spit out a list of imports from a python file"""
17+ def __init__(self, txt):
18+ self.source = ast.parse(txt)
19+
20+ def imports(self):
21+ # Get the names of import statements
22+ # and return as a list
23+ output = []
24+ for each in self.source.body:
25+ if type(each)== _ast.Import:
26+ for modules in each.names:
27+ output.append(modules.name)
28+ return list(set(output))
29+
30+if __name__ == "__main__":
31+
32+ # read in a python file
33+ f = open("ASTcount.py","r")
34+ x = "".join(f.readlines())
35+ f.close()
36+
37+ # create instance and print imports and variable names
38+ counter = ASTcount(x)
39+ print counter.imports()
40
41=== modified file 'CATEGORIES'
42--- CATEGORIES 2010-04-10 20:36:15 +0000
43+++ CATEGORIES 2010-04-10 20:43:22 +0000
44@@ -13,6 +13,7 @@
45 When adding a snippet please try to use one of the following categories if suitable:
46
47 Application Indicator Application indicator examples.
48+ AST Abstract Syntax Tree examples
49 bzrlib Bazaar source control system Python module.
50 Cairo Cairo drawing examples
51 Clutter Clutter toolkit examples.

Subscribers

People subscribed via source and target branches