I am in Python3. I have a bunch of Python2 .py files. For each of these files, I need to check whether the contents are valid Python2 code and return it as a boolean. The files are small scripts, sometimes they may import things or have a function or class, but mostly they are pretty simple.
How can I do this?
Do you do it at build time? If so, you can try run 2to3 and parse its output to determine if the file is valid Python 2 code.
Related
enter image description here
I am reading an e-book on machine-learning by Dr. Joseph Brownlee.
This is the code from the book, and this is what I am getting. ( I do have the CSV. )
It may be my computer, because I haven't been able to load a CSV from any version of Python 3.
Why?
Josh, your python-interpreter is not using the same installation as Jason uses.
First: check, where is the Jason's CSV-file actually located ( python did not find it in his location )
Next: get that file moved/copied into your python-interpreter's directory.
In case: you may load the "classical" ML-dataset from here, just put it into the same directory, as your python is located and rename the file to match your .csv CSV-example
If all fails: ask Jason, where is the missing file, if it was some modified version of the "classic" ML-dataset.
Python 2.7 will make it. Do not panic. There is no need to worry about any Py3 remarks. Except for some printing issues to fix, the course will work for you in Py27 either.
You need to start the Python interpreter in the same folder as your csv file. Best way to do that would be write a Python script in the same folder, then just execute it
or give the full path to the file. open(r"C:\Users\username\files\blah.csv")
Also, you're using Python27, not Python3, and both will work, so it's recommended nowadays to use Python3
I'm using Pelican for a static blog, and attempting to install the figure-ref extension. Since I'm using Markdown, the plugin relies on the figureAltCaption third-party Markdown extension. However I have no idea how to install it.
Pelican has an MD_EXTENSIONS configuration option, but I've tried a few obvious options with no luck. It seems like this is a dead-simple gimme but it's not clear how to proceed. Would love some suggestions.
Unfortunately, the author of figureAltCaption appears to have not provided an install script. My suggestion would be to create one and contribute it as a pull request. This tutorial about creating Extensions for Python-Markdown covers creating an install script as well.
However, as a shortcut, you should be able to just copy the figureAltCaption.py file to the appropriate directory. Usually you want the site-packages directory. As this answer shows, just do the following from Python:
>>> import site; site.getsitepackages()
Then copy the figureAltCaption.py file to the first directory returned.
Now that the extension is on your PYTHONPATH, it should be importable. From the Python prompt, try:
import figureAltCaption
If you get no errors, then it worked and you just need to tell Pelican about it.
MD_EXTENSIONS = ['figureAltCaption']
I was using ast module of python3.4 to get the imports and function calls within a file.
It works correctly if I run the code on a file which has python3.4 syntax but throws an exception if I try to parse a file of older python2.7 version (for print statements, except statements which have a "," etc).
Is there a way to force ast to use python2.7 compiler while dealing with old files and use python3.4 compiler when dealing with python3.4 file?
Is there any other way to resolve this issue??
Turns out it is not possible to use different versions of AST parsers in python to the best of my knowledge. (It is still possible to parse them separately by carrying out multiple iterations each time using a different version AST)
I've already looked through the forums, but I haven't found an answer that is mavlink specific.
I am currently writing script in python, and I want to use a python module of mavlink.
The documentation for mavlink tells me to run mavgenerate.py from my mavlink folder. When I run this script, a gui appears asking for my xml files, a specified output directory, and what language I want my headers in. mavgenerate.py works when I choose to make headers for C, but it gives me the error: "attempted relative import in non-package" when I try to choose python.
my xml files is located at:
C:\Python27\mavlink\message_definitions\v1.0
and I have my python module output directory as:
C:\Python27\mavlink\pymavlink\include
below is a screenshot of my error.
can anyone tell me what I'm doing wrong?
This is quite an old question but i have just encountered with this problem. Solution that i have applied is: in mavgenerate.py replace following lines
sys.path.append(os.path.join('pymavlink','generator'))
from mavgen import *
with
from pymavlink.generator.mavgen import *
I dont know the exact reason of the problem but from
this thread i assume problem lies with python's interpretation of directories as packages.
I have used python 2.7. Tried though to do with python 3.4 but didn't had chance there.
I have a C shell script that calls two
C programs - one after the another
with some file handling before,
in-between and afterwards.
Now, as such I have three different files - one C shell script and 2 .c files.
I need to give this script to other users. The problem is that I have to distribute three files - which the users must keep in the same folder and then execute the script.
Is there some better way to do this?
[I know I can make one C code file out of those two... but I will still be left with a shell script and a C code. Actually, the two C codes do entirely different things... so I want them to be separate]
Sounds like you're worried that your users aren't savy enough to figure out how to resolve issues like command not found errors and the like. If absolutely MUST hide "complexity" of a collection of files you could have your script create the other files. In most other circumstances I would suggest that this approach is only going to increase your support workload since semi-experienced users are less likely to know how to troubleshoot the process.
If you choose to rely on the presence of a compiler on the system that you are running on you can store the C code as a collection of cat $STRING >> file.c commands to to create your two C files, which you then compile and use.
If you would want to use pre-compiled programsn instead then the same basic process can be used except instead use xxd to both generate the strings in your script and reverse the conversion process to give you working binaries. Note: Remember to chmod the binary so that it is executable.
use shar command to create self-extracting archive.
or better yet use unzipsfx with AUTORUN option.
This provides users with ONE file, and only ONE command to execute (as opposed to one for untarring and one for execution).
NOTE: The unzip command to run should use "-n" option, that way only the first run would extract the files and the subsequent would skip the extraction.
Use a zip or tar file? And you do realize that .c files aren't executable, you need to compile & link them first?
You can include the c code inside the shell script as a here document:
#!/bin/bash
cat > code.c << EOF
line #1
line #2
...
EOF
# compile
# execute
If you want to get fancy, you can test for the existence of the executable and skip compiling them if they exists.
If you are doing much shell programming, the rest of the Advanced Bash-Scripting Guide is worth looking at as well.