How to call clang-format from Python script - python

I have a Python tool that generates C++ files.
In order to test the tool, I have one test that compares the generated file with an expected output file.
diff = difflib.unified_diff(expectedFile.readlines(), file.readlines(), expectedFilename, filename)
The problem is that I'm getting some differences due to the format.
I can run clang-format on the expected output file.
What I'm still trying to do is to run clang-format on the generated files, just before the difflib.unified_diff is called.
Can anyone help me on how I can run clang-format in Python on a file ?
Thank you very much!

You can use the call command that is supplied by Python to call an external command. For example, you can write a script like:
#!/usr/bin/python
import sys
from subprocess import call
lc = ["clang-format","test.c"] # replace 'test.c' with the your filename.
retcode=call(lc)
sys.exit(retcode);

Related

How do I run a .py file quickly if that file pulls functions from other .py files

Background:
I have three .py files that I use on Spyder every morning:
functions.py file contains a functions I use
calculation.py uses the functions to get the data and do some calculations on it
morning.py uses the data to produce a chart
In calculation.py I have the line to import my functions
%run '/Users/mak/Dropbox/Python/fed nlp/functions.py'
Then I run through around 100 lines of code in calculation.py to use these functions to pull data, clean it, and produce a nice table.
Finally, I open morning.py which uses this table to produce a nice chart for me.
Problem:
In order to speed up my daily process, I tried inserting this line into morning.py:
%run '/Users/mak/Dropbox/Python/fed nlp/calculation.py'
However I get the message:
%run '/Users/mak/Dropbox/Python/fed nlp/calculation.py'
File "/Users/mak/Dropbox/Python/fed nlp/calculation.py", line 2
%run '/Users/mak/Dropbox/Python/fed nlp/functions.py'
^
SyntaxError: invalid syntax
I guess you cannot do a 'run' command via another 'run' command...
Any idea for how I can speed up my daily process?
I'm not sure it's going to work but you could try to put these three files in the same directory and after to write in the "functions" file and "calculation" file:
if "_name_" == "_main_":
pass
After that you have to import these two files in the "morning" file using import:
import functions.py
import calculation.py
In this way you can do everything inside the "morning" file.
I apologize if this does not solve your problem

How to fix "'bool' object is not iterable" error when running a UNIX command on a directory of files in python

I am trying to run a list of files in a directory through a UNIX executable using a python. I would the output of the executable for each file written to a different directory but retaining the original filename.
I am using python 2.7 so using the subprocess.call method. I am getting an error that says "'bool' object is not iterable" which I am guessing is due to the part where I am trying to write the output files as when I run the following script through the console I get an expected output specific to the executable within the console window:
import subprocess
import os
for inp in os.listdir('/path/to/input/directory/'):
subprocess.call(['/path/to/UNIX/executable', inp])
My code is currently this:
import subprocess
import os
for inp in os.listdir('/path/to/input/directory/'):
out = ['/path/to/output/directory/%s' % inp]
subprocess.call(['/path/to/UNIX/executable', inp] > out)
However, this second lot of code returns the "'bool' is not iterable" error.
I'm guessing the solution is pretty trivial as it is not a complicated task however, as a beginner, I do not know where to start!
SOLVED: following #barak-itkin's answer, for those who may stumble across this issue in the future, the code ran successfully using the following:
import subprocess
import os
for inp in os.listdir('/path/to/input/directory/'):
with open('/path/to/output/directory/%s' % inp, 'w') as out_file:
subprocess.call(['/path/to/UNIX/executable', inp], stdout=out_file)
To write the output of a subprocess.call to a file, you would need to either use the > path/to/out as part of the command itself, or to do it "properly" by specifying the file to which the output should go:
# Option 1:
# Specify that the command is using a "shell" syntax, meaning that
# things like output redirection (such as with ">") should be handled
# by the shell that will evaluate the command
subprocess.call('my_command arg1 arg2 > /path/to/out', shell=True)
# Option 2:
# Open the file to which you want to write the output, and then specify
# the `stdout` parameter to be that file
with open('/path/to/out', 'w') as out_file:
subprocess.call(['my_command', 'arg1', 'arg2'], stdout=out_file)
Does this work for you?

how to modify txt file properties with python

I am trying to make a python program that creates and writes in a txt file.
the program works, but I want it to cross the "hidden" thing in the txt file's properties, so that the txt can't be seen without using the python program I made. I have no clues how to do that, please understand I am a beginner in python.
I'm not 100% sure but I don't think you can do this in Python. I'd suggest finding a simple Visual Basic script and running it from your Python file.
Assuming you mean the file-properties, where you can set a file as "hidden". Like in Windows as seen in screenshot below:
Use operating-system's command-line from Python
For example in Windows command-line attrib +h Secret_File.txt to hide a file in CMD.
import subprocess
subprocess.run(["attrib", "+h", "Secret_File.txt"])
See also:
How to execute a program or call a system command?
Directly call OS functions (Windows)
import ctypes
path = "my_hidden_file.txt"
ctypes.windll.kernel32.SetFileAttributesW(path, 2)
See also:
Hide Folders/ File with Python
Rename the file (Linux)
import os
filename = "my_hidden_file.txt"
os.rename(filename, '.'+filename) # the prefix dot means hidden in Linux
See also:
How to rename a file using Python

How to start external program and pass in CSV file?

I am trying to start a program and write a saved CSV file to it.
I have the following code to start the program, that works fine:
os.startfile('C:\Program Files\Files\CSV_Reader')
But I'm not sure how to open the CSV File. I tried a few other options like:
os.system('start CSV_Reader.exe "{0}C:\Program Files\Files\card_kingdom.csv"'.format(sys.path[0], ))
But I get the following error message:
C:\Users\JJ\AppData\Local\Programs\Python\Python36\python.exe C:/Users/JJ/PycharmProjects/Buylist/CK_file_test
The system cannot find the file CSV_Reader.exe.
I know that the directory is right, because when I call the os.startfile function it opens the program.
Your help would be much appreciated!
You can use popen like it:
import subprocess
subprocess.Popen(["C:\Program Files\Files\CSV_Reader.exe" , "C:\Program Files\Files\card_kingdom.csv"])

Calling and running other python files from a python file

so i checked several other links with similar titles but, It couldn't solve my specific question. I'm trying to run a python file in notepad++ which is not a problem to me however, this file takes in a few things in order for it to compile. This is how I successfully run it in the command prompt.
python upload.py --file= "video path" --title= "title" --description= "testing"
My question is, how would i set these attributes in a different python file and then just call that file instead?
here is my code that i have in my new file
Thanks
enter image description here
enter image description here
You can use the subprocess module to do this. Following the example from the docs and the code you've listed:
import subprocess
result = subprocess.check_output('python upload.py --file="video path" --title="title" --description="testing"')
result will store any output from your command.
Note: if you're running in a windows environent, not linux, change the /usr/bin/python to python.
Maybe you can use subprocess to call your specific command.
In a separate file in the same folder, you can put a file like this:
import subprocess
subprocess.call("python upload.py --file= \"video path\" --title= \"title\" --description= \"testing\"")
And then you just call that file, and that's it...

Categories