¡@

Home 

python Programming Glossary: ast

Restricting Python's syntax to execute user code safely. Is this a safe approach?

http://stackoverflow.com/questions/10661079/restricting-pythons-syntax-to-execute-user-code-safely-is-this-a-safe-approach

functions are allowed and only unused names. import ast allowed_functions set #math library 'acos' 'acosh' 'asin' 'asinh'.. safe_names set 'True' 'False' 'None' class SyntaxChecker ast.NodeVisitor def check self syntax tree ast.parse syntax self.visit.. SyntaxChecker ast.NodeVisitor def check self syntax tree ast.parse syntax self.visit tree def visit_Call self node if node.func.id..

Use of eval in Python?

http://stackoverflow.com/questions/1087255/use-of-eval-in-python

to deal with such dynamic Python sources I'd reach for the ast module ast.literal_eval is MUCH safer than eval you can call.. such dynamic Python sources I'd reach for the ast module ast.literal_eval is MUCH safer than eval you can call it directly.. a one off and relies on simple constants only or do node ast.parse source first then keep the node around perhaps munge it..

Parsing SQL with Python

http://stackoverflow.com/questions/1394998/parsing-sql-with-python

examples in antlr need a lot of work to convert to a nice ast in python. The sql standard grammers are here but it would be..

Simple example of how to use ast.NodeVisitor?

http://stackoverflow.com/questions/1515357/simple-example-of-how-to-use-ast-nodevisitor

example of how to use ast.NodeVisitor Does anyone have a simple example using ast.NodeVisitor.. ast.NodeVisitor Does anyone have a simple example using ast.NodeVisitor to walk the abstract syntax tree in Python 2.6 The.. 2.6 abstract syntax tree share improve this question ast.visit unless you override it in a subclass of course when called..

Using python's eval() vs. ast.literal_eval()?

http://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval

python's eval vs. ast.literal_eval I have a situation with some code where eval came.. its entered or after the datamap variable is called Is the ast module's .literal_eval the only safe option python eval abstract.. as the function is called. See also the dangers of eval . ast.literal_eval raises an exception if the input isn't a valid..

convert string representation of list to list in python

http://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python

of python string share improve this question import ast x u' A B C D ' x ast.literal_eval x x 'A' 'B' 'C' ' D' x n.strip.. share improve this question import ast x u' A B C D ' x ast.literal_eval x x 'A' 'B' 'C' ' D' x n.strip for n in x x 'A'.. x 'A' 'B' 'C' ' D' x n.strip for n in x x 'A' 'B' 'C' 'D' ast.literal_eval Safely evaluate an expression node or a string..

How can I sandbox Python in pure Python?

http://stackoverflow.com/questions/3068139/how-can-i-sandbox-python-in-pure-python

The other way is to parse the code and then use the ast module to kick out constructs you don't want e.g. import statements..

How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function

http://stackoverflow.com/questions/5073767/how-can-i-efficiently-process-a-numpy-array-in-blocks-similar-to-matlabs-blkpro

as np from numpy.lib.stride_tricks import as_strided as ast A np.arange 36 .reshape 6 6 print A # 0 1 2 3 4 5 # 6 7 8 9.. 6 7 8 9 10 11 # ... # 30 31 32 33 34 35 # 2x2 block view B ast A shape 3 3 2 2 strides 48 8 24 4 print B 1 1 # 14 15 # 20 21.. we go from numpy.lib.stride_tricks import as_strided as ast def block_view A block 3 3 Provide a 2D block view to 2D array...

Converting a string that represents a list, into an actual list object

http://stackoverflow.com/questions/5214344/converting-a-string-that-represents-a-list-into-an-actual-list-object

list type conversion share improve this question Use ast.literal_eval . import ast i ast.literal_eval ' 22 33 36 41 46.. improve this question Use ast.literal_eval . import ast i ast.literal_eval ' 22 33 36 41 46 49 56 ' i 3 41 share improve.. this question Use ast.literal_eval . import ast i ast.literal_eval ' 22 33 36 41 46 49 56 ' i 3 41 share improve..

Load module from string in python

http://stackoverflow.com/questions/5362771/load-module-from-string-in-python

StringIO 'print hello world ' Traceback most recent call last File stdin line 1 in module TypeError load_source argument 3.. hello world ' '' '' '' 0 Traceback most recent call last File stdin line 1 in module ValueError load_module arg#2 should.. load is only partially trusted. I've gone through it with ast and determined that it doesn't import anything or do anything..

Equation parsing in Python

http://stackoverflow.com/questions/594266/equation-parsing-in-python

slightly you'll be happier. import compiler eq sin x x 2 ast compiler.parse eq You get an abstract syntax tree that you can..

Python - Parse a .py file, read the AST, modify it, then write back the modified source code

http://stackoverflow.com/questions/768634/python-parse-a-py-file-read-the-ast-modify-it-then-write-back-the-modified

python source code using standard python modules such as ast or compiler . However I don't think any of them support ways.. seeing what breaks. python compiler source code source ast share improve this question Pythoscope does this to the.. if you want to do more refactoring like transforms. The ast module is your other option and there's an older example of..

Converting a String to Dictionary?

http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary

question Starting in Python 2.6 you can use the built in ast.literal_eval import ast ast.literal_eval 'muffin' 'lolz' 'foo'.. 2.6 you can use the built in ast.literal_eval import ast ast.literal_eval 'muffin' 'lolz' 'foo' 'kitty' 'muffin' 'lolz'.. 2.6 you can use the built in ast.literal_eval import ast ast.literal_eval 'muffin' 'lolz' 'foo' 'kitty' 'muffin' 'lolz' 'foo'..

Safety of Python 'eval' For List Deserialization

http://stackoverflow.com/questions/1112665/safety-of-python-eval-for-list-deserialization

of variables and the like before you eval the resulting AST when it's down to literals . The possible exploit of eval starts..

Parsing SQL with Python

http://stackoverflow.com/questions/1394998/parsing-sql-with-python

manner. I am looking into using ANTLR to produce an AST that represents the SQL as a relational algebra expression...

How to write the Visitor Pattern for Abstract Syntax Tree in Python?

http://stackoverflow.com/questions/2525677/how-to-write-the-visitor-pattern-for-abstract-syntax-tree-in-python

suggested me to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it As far.. I start writing it As far as I understand each Node in AST would have visit method that would somehow get called from where.. a mechanism for double dispatch . Each node in your AST would need to implement an accept method NOT a visit method..

Return a list of imported Python modules used in a script?

http://stackoverflow.com/questions/2572582/return-a-list-of-imported-python-modules-used-in-a-script

to get not only directly imported modules but it uses the AST to parse the code for runtime dependencies that a more static.. all imports you can also do this in code Please NOTE The AST chunks of this routine were lifted from the snakefood source.. import Discard Const from compiler.visitor import ASTVisitor def pyfiles startPath r d os.path.abspath startPath if..

What kinds of patterns could I enforce on the code to make it easier to translate to another programming language?

http://stackoverflow.com/questions/3455456/what-kinds-of-patterns-could-i-enforce-on-the-code-to-make-it-easier-to-translat

which is a start. From then on I can build the AST symbol tables and control flow. Then I believe I can start outputting.. team of computer scientists. DMS provides generic parsing AST building symbol tables control and data flow analysis application.. they are familiar that does a part of the job. Python ASTs are great example . The good news is that part of the job is..

Are there any static analysis tools for Python?

http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python

one that could be avoided with an in depth analysis of the AST. Obviously testing is important and I don't imply that tests..

Accessing the name that an object being created is assigned to

http://stackoverflow.com/questions/3744792/accessing-the-name-that-an-object-being-created-is-assigned-to

python internals share improve this question The AST can't give you that answer. Try using frame.f_lasti and then.. peeking into the bytecode. If the next line isn't a STORE_FAST you've got interior calls or something else going on other than..

Process escape sequences in a string in Python

http://stackoverflow.com/questions/4020539/process-escape-sequences-in-a-string-in-python

# python2 print decoded_string spam eggs Don't use the AST or eval. Using the string codecs is much safer. share improve..

Python - Parse a .py file, read the AST, modify it, then write back the modified source code

http://stackoverflow.com/questions/768634/python-parse-a-py-file-read-the-ast-modify-it-then-write-back-the-modified

Parse a .py file read the AST modify it then write back the modified source code I want to.. code. Basically I want to read a .py file generate the AST and then write back the modified python source code i.e. another.. comments in source when it's round tripped from source AST source. The rope project may meet your needs if you want to..