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?
Related
I just figured out that PyCharm supports a docstring with code prefixed by >>> like this:
def foo(x):
"""
Use it like:
>>> foo(42)
"""
print(x)
PyCharm then even applies the usual code highlighting on this.
I tried to find any documentation on this syntax. It doesn't seem to be part of reStructuredText. Also Sphinx does not mention this. Was this introduced in some PEP? Or is this PyCharm specific? But I did not find any PyCharm documentation on this.
(It's hard to use Google search for this. I don't know how to call this. "3-times greater sign"?)
The comments have answered the question. I'm just putting it here for completeness:
It's three fleshes (>>>).
It's used for doc test.
In PyCharm, when you rightclick it, it allows you to Run Doctest.
The syntax probably comes from the Python interactive shell.
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.
I've just started looking at the Vim jedi plugin, and it seems pretty impressive. One feature of some of the Java IDEs I've used is the ability to automatically add required imports. Can Jedi do that? For example, if I enter a line such as
arg1 = sys.argv[1]
and then invoke some Jedi command, is it possible for the plugin to automatically insert an import sys line at the top of the source file (if sys is not already being imported)?
I've looked through the Jedi help, and can't see anything like this - but it's possible I missed something. Alternatively, is there another Vim plugin that would do this? (It needs a certain level of understanding of Python syntax to get it right, which is why I looked to Jedi to be able to do it).
Currently Jedi doesn't do refactoringing. This includes import additions. There's an issue for the whole subject: https://github.com/davidhalter/jedi/issues/667.
It's not that easy to implement this command with good performance. However any help is appreciated. :)
FIY, I've defined a generic import feature that can be used on demand in lh-dev. I use it from my C&C++ suite, and from my snippet engine (mu-template).
So far I don't parse anything to add the missing import/include statements. This part would be complex as Dave said. Instead my snippets know which files need to be imported/included and import/include them if not already imported/included.
It's far from being perfect, but it's a start. mu-template provides a hook to do stuff at the start of the file after the snippet has been expanded, this is where I call lh-dev function. If other snippet engines provide similar hooks, you should be able to call lh#dev#import#add() from your snippets.
Here a proof of concept snippet for Python (I seldom program in Python, and don't have many snippets for it): https://github.com/LucHermitte/mu-template/blob/master/after/template/python/path-exists.template
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?
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.