I am doing code transforms on old python code using the ast module. But when I write the newly converted code it does not include comments. I know that the ast just ignores comments because they are not important in regards to the code. But I would like to keep the comments in the code after the transform. How would I do this?
Related
I know that's possible from a snippet of code in python to get its AST or its code object via compile().
I was wondering if it is possible to do the opposite: I have the code object(extracted by a .pyc file) and I was looking for the its AST.
No, unfortunately it is not possible to do by conventional methods unless the file that the code object created is still available in the path. There are tools like uncompyle6, which might help though.
Following this question other question I got to the next question.
I have 40 matlab files and I'm implementing a general solution for extracting that info, as each of them has a different internal organization. As soon as I have it I will post here.
But my question is: the solutions given by gg349 and mergen gets me some objects like this:
<scipy.io.matlab.mio5_params.mat_struct object at 0x7f368cff3f90>,
And I don't know how to deal with those, could anyone help?
It is the equivalent of Matlab struct in python. You can access the elements by dot, like object.value. An example here. Complex Matlab struct mat file read by python
A library I'd like to use seems to get confused by triple quote comments when introspecting python code. It appears that Python's inspect doesn't give access to de-commented code though?
This code in Hyperas breaks and looks un-pythonic but I can't figure out how it could be done better. Is AST the way forward?
I'm new to Python and I'm needing to convert some Matlab code I have to Python. Here's the code I have:
save(myFile, 'list','config');
else
load(myfile, 'list');
end
I've been looking within Python and I can't seem to find an equivalent method for this. Any help would be greatly appreciated.
Thanks!
Checkout the numpy IO routines here, they can handle various formats.
I am working on a opensource project where i implementing searching of "print" statements
and such other statements that are unnecessary in a live production enviroment and can
create a error.
But i dont want to trouble the user if there is a print statement commmented out or "print"
word in a docstring or a comment. So i am trying to find out those portion of the python
script that have a chance to get executed. How can i do that ?
Use ast module and NodeVisitor to analyze statically program's code. This way you will have no problems with docstrings or comments.
For Python 2.6 or later you could probably use the ast module. Read in the code (as a string, use ast.parse() to create an abstract syntax tree of that code, and then walk over the code looking for the ast.Print objects and then translate those back into filename, line number tuples.
A similar thread is here:
Check Python code for certain statements
You can get some more ideas.