Using vimrc function to pass python arguments - python

I am attempting to create a vimrc function that will be used in an autocmd. The function must simply call a python script and pass the file name as an argument.
.vimrc
fu! Test(filename)
let filename = expand("%:t")
"echom filename
!test.py filename
example.py
#!usr/bin/python
import sys
print sys.argv[1]
If I uncomment the echo line, example.py is echo'd correctly. If I try to execute as it is displayed above, however, the string filename is passed literally.
Is there any way around this?

Sure, you can use the execute command to execute a string, which is built from the command you want and the variable concatenated together:
fu! Test()
let filename = expand("%:t")
execute "!test.py " . l:filename
endfunction
I've omitted the filename argument in your Test function because it doesn't seem to be used

You have two options either you pass the filename directly as an argument or pass it as a local variable:
fu! Test(filename)
"echom a:filename
execute "!test.py ".a:filename
or
fu! Test()
let l:filename = expand("%:t")
"echom filename
execute "!test.py ". l:filename

Related

Open a Python file with command line arguments

NOTE : I don't know if this has been asked before, but I couldn't find what wording would be used for asking this question
I am creating a python file, and inside it needs to have a name of a file.
What I want is when you put python Compiler.py <Filename> in the command line, it takes the filename and uses it in the python file.
How would I go about doing this?
someVariable = sys.argv[1]
This will take the first element after the file name and store it in the variable.
someOtherVariable = sys.argv[2]
will take the second element, and so forth.
python Compiler.py firstArg secondArg NthArg
Note: You will need to import sys to use the sys module.
Use sys.argv to get the args that are passed to the script.
import sys
open(sys.argv[1], 'w').write('hello world')

How to invoke a Perl script that need args from Python

I'm writing my script in Python, and I want to invoke a Perl script from it.
This is the line I want to call:
perl conv_intel_hex_2_verilog.pl arg1 arg2
while arg1 is the input from the Python script and arg2 is a new file that I'm creating.
So far I have this:
def main(argv):
file = open("test.hex", "w")
input = argv[0];
subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input]);
file.close();
This runs and does nothing.
And when I'm changing the
subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input]);
to
subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input ,file]);
it doesn't compile...
As you've described it, the program you're running expects its second argument to be the name of a file, so that's what you need to give it. In your current code, you're giving it a Python file object, which is not the same as the string name of a file.
subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input, "test.hex"]);
There is no need to create the file object prior to running that code, either. The other program will surely create it for you, and might even balk if it finds that the file already exists.
You cannot just give it a filehandle (or whatever that is called in Python). You are constructing a call to another program, and that program expects a filename, so pass the filename as a string.
subprocess.call(["perl", "conv_intel_hex_2_verilog.pl", input ,"test.hex"]);
You also don't need to open the file first.

Command Line Arguments in Python with sys.argv

I want to use sys.argv to access the arguments passed to the script. Here is my code :
if __name__ == '__main__':
data = {}
if len(sys.argv) >= 2 :read_inputs(data, sys.argv[1])
else : print "ERROR : the config file is required in the command line"
if len(sys.argv) >= 3 :data['Parameters']['Mode'] = sys.argv[2]
print_data(data)
I understand that sys.argv[1] and sys.argv[2] refer to the arguments.
My arguments are contained in a text file.
What I cannot understand is how can I tell the code that it needs to read the arguments in that exact text file.
I used python Interface.py config.txt but it didn't work.
Any ideas ?
If I understand you correctly you want what would normally be on the command line to be in that file, right?
You can do that using command substitution python Interface.py $(< config.txt), as seen here
Although not a direct answer to your question, I would highly recommend using the Python argparse module to parse command line argument. I your case I would add a "-c, --config" option that specifies the location of the config file that you want to use. See the documentation for examples: https://docs.python.org/3/library/argparse.html

"sys.argv[x]" is out of range

I must run the exact command:
python3 main.py flip pattern.ppm > flippedpattern.ppm
on the following code:
def main(args):
if sys.argv[1] == "flip":
fileName = sys.argv[2]
method = read_ppm(fileName)
print(method.flip())
return
if __name__ == '__main__':
sys.exit(main(sys.argv))
I have a ppm file and I am trying to manipulating it using another module, but it keeps telling me sys.argv[4] is out of range when assigning it to the variable 'outputFile'
The code executed should be essentially this for the flip command
method = "flip"
method = read_ppm("pattern.ppm")
f.write(method.flip())
Doing exactly that using the repl in visual studio leads to success but when I try to run it from the command line through the main module it doesn't work. Why is this? Does the '>' character somehow interfere with sys.argv or am I making a clear counting mistake that I can't see?
The else statement would account for the input not catching and simply printing, using the input
python3 main.py flip pattern.ppm
without any file redirect.
The argument after > is not part of the command line passed to python. It is the file that the shell writes standard output to. So, for example, print "Hello" will write Hello to flippedpattern.ppm.
If you want flippedpattern.ppm to be the fourth command line argument, just leave out the > in the call. (Then, standard output will be written to the screen.)
Edit: Given your modified description, you can write sys.stdout.write(method.flip()). The only difference between this and print(method.flip()) is that print adds a newline character at the end of the string, while .write doesn't.
I'm not sure how Visual Studio works, but if it works with argv[4] it's probably interpreting every word after the script name like another argument (as it's not bash to have a special interpretation for ">").
So for it to work from bash, you should probably change argv[4] in the code to argv[3], and pass the output file as another argument instead of redirecting:
python main.py flip pattern.ppm flippedpattern.ppm
, or leave the code as it is and add a "dummy" third argument so the output file will be the 4th, for example force it to treat ">" as a regular argument as well:
python3 main.py flip pattern.ppm ">" flippedpattern.ppm

How to write a Python interpreter for script that can use a shebang to load the interpreter

I have simple script interpreter written in Python that processes a script written in a text file. I can refer to the interpreter using a shebang at the top of the script so that I can then execute the script directly.
The interpreter has some logic to detect when it is invoked via the shebang so that it can
adjust the argument list to compensate. This is necessary because, when called directly, each argument is a separate item in argv but, when called via the shebang, all arguments on the shebang line are contained in the first argument string and the name of the script is in the second argument string with any arguments given directly to the script following thereafter.
The way I check for the shebang is as follows:
def main(name, argv):
...
if len(argv) >= 2 and name[0] == '/' and os.path.isfile(argv[1]) and os.access(argv[1], os.X_OK):
input = open(argv[1])
arglist = argv[0].split() + argv[2:]
else:
arglist = argv
input = sys.stdin
...
sys.exit(main(sys.argv[0], sys.argv[1:]))
What this does is assume execution was via shebang if there is at least 2 argv values, the command name begins with a "/" (the shebang executable path is absolute) and the script name in argv[1] is an executable file. If execution is via a shebang then the argument list is argv[0] split out plus argv[2] onwards appended to that list.
I would like to know whether this is correct or if there is another way to more definately determine this. What I have done works fine for all the scenarios that I need to use but I would be interested to learn of a better way if there is one.

Categories

Resources