NameError: name 'n' is not defined in python script - python

I'm trying to run a python script that is optimized to work with python 2.7:
#!/usr/bin/env python
import sys,getopt,os
SplitInput_string = """#!/bin/bash
#BSUB -J SplitInput[1-%numSamples%]
#BSUB -o Logs/SplitInput-Out-%I.out
#BSUB -e Logs/SplitInput-Err-%I.err
#BSUB -q week
#BSUB -W 23:58
echo Date: `date`
t1=`date +%s`
sleep ${LSB_JOBINDEX}
python LSFScripts/array_merge.py -r ${LSB_JOBINDEX} -i %input% -o original_reads/
[ $? -eq 0 ] || echo 'JOB FAILURE: $?'
echo Date: `date`
t2=`date +%s`
tdiff=`echo 'scale=3;('$t2'-'$t1')/3600' | bc`
echo 'Total time: '$tdiff' hours'
"""
help_message = "usage example: python setupDirs.py -i /path/to/reads/ -n numberOfSamples"
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], 'hi:n:', ["inputdir="])
except:
print help_message
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print help_message
sys.exit()
elif opt in ('-i', '--inputdir'):
inputdir = arg
if inputdir[-1] != '/':
inputdir += '/'
elif opt in ('-n'):
n = arg
for dir in ['Logs', 'original_reads', 'hashed_reads', 'cluster_vectors', 'read_partitions']:
os.system('mkdir %s' % (dir))
f = open('LSFScripts/SplitInput_ArrayJob.q', 'w')
f.write(SplitInput_string.replace('%numSamples%', n).replace('%input%', inputdir))
f.close()
But I keep getting this error message:
line 42, in <module>
f.write(SplitInput_string.replace('%numSamples%', n).replace('%input%', inputdir))
NameError: name 'n' is not defined
Any advice would be appreciated!

You must assign a value to n before entering the loop.
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:],'hi:n:',["inputdir="])
except:
print help_message
sys.exit(2)
n = ""
for opt, arg in opts:
etc...

n is not defined in all code paths. It is only defined if you follow the path:
elif opt in ('-n'):
Define m at a higher scope (i.e. before the for loop) with a default value if you want to use it afterwards.
n = #default value
for opt, arg in opts:
# ...
If you want to parse arguments, I highly recommend using the argparse package. There is a little bit of a learning curve, but you can very robustly generate the necessary usage.

Related

AttributeError: 'module' object has no attribute 'Parser'

This is my final.py file.
#!/usr/bin/python
import sys
import os
sys.path.insert(0, './src/')
import lexRule
import parser
import ply.lex as lex
import ply.yacc as yacc
import node_file
import genAssembly as ga
import getopt
import pydot
def main(argv):
to_parse = ''
try:
opts, args = getopt.getopt(argv,"o:f:h",["opt=","file=","help"])
except getopt.GetoptError:
print 'Usage : ./bin/final.py [options][-f/-h/-o] [string]'
sys.exit(2)
parse = parser.Parser()
optjump = 0
for opt, arg in opts:
if opt in ("-o", "--opt"):
if arg == "1":
optjump = 1
elif opt in ("-f", "--file"):
tree = parse.parse_file(file(arg))
t = parser.tac.code
q = []
if optjump == 1:
for x in range(0,len(t)-1):
if t[x][0] == "goto" and t[x+1][3] == t[x][3]:
q.append(x)
for qw in reversed(q):
del t[qw]
parser.tac.code = t
old_target = sys.stdout
ga.generate()
sys.stdout = open('output.s', 'w')
ga.generate()
sys.stdout = old_target
print("Run ./a.out for execution")
# os.system("nasm -f elf32 inout.s")
# os.system("nasm -f elf32 fileio.s")
os.system("nasm -f elf32 output.s")
# os.system("nasm -f elf32 val1.s")
# os.system("nasm -f elf32 next1.s")
# os.system("nasm -f elf32 append2.s")
os.system("gcc -m32 output.o ./bin/inout.o ./bin/fileio.o ./bin/val1.o ./bin/next1.o ./bin/append2.o")
elif opt in ("-h", "--help"):
_file = open("./README.txt")
content = _file.read()
print(content)
else:
print 'Usage : ./bin/final.py [options][-f/-h/-o] [string]'
sys.exit(2)
if not opts:
print 'Usage : ./bin/final.py [options][-f/-h/-o] [string]'
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])
when i ran it on cmd this is the error i got.error
I am using python2 and running these commands:
python bin/final.py -f test/ackermann.java
python bin/final.py -o 1 -f test/ackermann.java
the project i am trying to run is :https://github.com/abhigarg-iitk/java_compiler
Since you are using your own parser module (named the same as the standard library) located in another location than the standard library packages you need to import it using the from ... import ... syntax and an __init.py__ file in your parser package directory.
For further reading you can have a look at the python documentation at https://docs.python.org/3/reference/import.html

comparing input with a file and minding the sequence

How can I compare the values of input parmeters with a file in such a way that the sequence of the lines in file are "respected". For example:
file sequence.txt has following enteries
aaa
bbb
ccc
ddd
and the input is coming like this (with comas):
./run.py -c migrate -s ddd,bbb
then output is like this:
bbb
ddd
Here is the script I have worked so far
#!/usr/bin/python
import sys
import getopt
import time
import os
def main(argv):
cmd = ''
schemas = ''
script_dir = os.path.dirname(__file__)
seq_file = "system/sequence.txt"
abs_file_path = os.path.join(script_dir, seq_file)
try:
opts, args = getopt.getopt(argv,"h:c:s",["cmd=","schemas="])
except getopt.GetoptError:
print './run.py -c=<command> -s=<schemas> '
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print './run.py -c=<command> -s=<schemas>'
sys.exit()
elif opt in ("-c", "--cmd"):
cmd = arg
elif opt in ("-s", "--schemas"):
schemas = arg
if cmd == "migrate" :
with open(abs_file_path) as z:
for line in z:
print line.rstrip('\n')
if __name__ == "__main__":
main(sys.argv[1:])
I know that I have to do comparisons at position print line.rstrip('\n') but I can't figure out how to do it. Any suggestions?
Also, how can I make -s switch mandatory if -c has "migrate" value?
Thanks in advance.
You need to check whether the current line of the sequence is specified with the -s flag. So you need to modify the schemas value, so that it is a list that contains all schemas and then you can check if the current line is equal to one of the schemas. As for your second question, I'm not familiar with getopt, but you could simply check whether schemas is not empty when -c is migrate and do the approriate error handling.
[...]
schemas = []
[...]
elif opt in ("-s", "--schemas"):
schemas = arg.split(',')
[...]
if cmd == 'migrate':
if not schemas: # list is empty
# do error handling
for line in z:
if line in schemas:
print line

bash variables in Python

I have a python script conve_cal.py which take 7 arguments as follow:
enter code here
import numpy as np
import sys
def read_file(filename1,filename2):
Input1 = np.loadtxt(filename1)
Input2 = np.loadtxt(filename2)
#print type(Input1)
#print Input1
return Input1,Input2
def NU_calc(T_avg,Trght,Ttop,rmv,argv1,argv2,arg3):
....
if __name__ == "__main__":
T_avg = sys.argv[1]
Trght = sys.argv[2]
Ttop = sys.argv[3]
rmv = sys.argv[4]
arg3 = sys.argv[5]
file1 = sys.argv[6]
file2 = sys.argv[7]
print (sys.argv)
In1,In2 = read_file(file1,file2)
#print type(arg3)
NU_calc(T_avg,Trght,Ttop,rmv,In1,In2,arg3)
If I run the code stand alone in the Terminal as
python2 conve_calc.py 285 282 300 0 noclip Input1.dat Input2.dat
the code give the output as desired. But If i use the same syntax with bash variables in the bash script as follows;
#!/bin/bash
...
...
t_avg=`grep "Average" test.avg |cut -f6 -d" "`
t_right=`grep "Tright" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
t_top=`grep "Ttop" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
echo "$t_right $t_top $Hr $Ht" >> $start_path/post_process_data/'HT_stat.dat'
rm test.avg tmp.dat test_fl.txt
# Call
# Arg-1; Average Temp
## Arg-2; Temp of right wall
## Arg-3; Temp of top wall
## Arg-4; # of faces to remove (0:None ,1...upto max of no. of faces)
## Arg-5; Right GradT file
## Arg-6; Top GradT file
rv0="0" # Change this to Remove the faces
rv1="1"
c1="noclip"
c2="middle"
i1="Input1.dat"
i2="Input2.dat"
python2 $start_path/conve_calc.py $t_avg $t_right $t_top $rv1 $c1 $i1 $i2 >> $start_path/post_process_data/\
'Common_out.dat'
But with this bash script input I am getting Following error I am unable to find why these inputs are getting wrong.
Traceback (most recent call last):
File "/home/meisu/OpenFOAM/meisu-2.4.0/Cyence_data/foam_adagio/conve_calc.py", line 69, in <module>
file2 = sys.argv[7]
IndexError: list index out of range
I have looked into various stack solutions but none of them worked.
Your problem is that one of the variables is empty. The shell expands the variables and then parses the command so it thinks there are only 6 parameters for the command. You could add two types of defensive coding. First, for the derived variables likely to fail, test them before you use them. Second, enclose your parameters in quotes so that empty parameters, or parameters with characters such as spaces that will mess up the command parsing, will be handed down properly.
#!/bin/bash
...
...
t_avg=`grep "Average" test.avg |cut -f6 -d" "`
if [ -z "$t_avg" ]; then
echo t_avg failed >&2
exit 2
fi
t_right=`grep "Tright" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
if [ -z "$t_right" ]; then
echo t_right failed >&2
exit 2
fi
t_top=`grep "Ttop" ./0/Input_conditions|cut -f 3 |cut -f 1 -d';'`
if [ -z "$t_top" ]; then
echo t_top failed >&2
exit 2
fi
echo "$t_right $t_top $Hr $Ht" >> $start_path/post_process_data/'HT_stat.dat'
rm test.avg tmp.dat test_fl.txt
# Call
# Arg-1; Average Temp
## Arg-2; Temp of right wall
## Arg-3; Temp of top wall
## Arg-4; # of faces to remove (0:None ,1...upto max of no. of faces)
## Arg-5; Right GradT file
## Arg-6; Top GradT file
rv0="0" # Change this to Remove the faces
rv1="1"
c1="noclip"
c2="middle"
i1="Input1.dat"
i2="Input2.dat"
python2 $start_path/conve_calc.py "$t_avg" "$t_right" "$t_top" "$rv1" "$c1" "$i1" "$i2" >> "$start_path/post_process_data/Common_out.dat"
You can see exactly what is sent to your python script with a toy script that just prints its arguments and exits
!#/usr/bin/env python
import sys
for i, arg in enumerate(sys.argv):
print i, arg
print '---- end ----'

Running a python program, unsure what the argument is written like?

so i want to run this python program but i am unsure how to run it, when i insert in the arguments it get a token error.
First i "import cw2" which is the file name in the terminal after typing python and then i type in the argument to run a individual task but i get and error. Heres the code, can you tell me how to run the individual parts.
Heres what i typed as the argument cw2 -u user_745409913574d4c6 -d doc_140228202800-6ef39a241f35301a9a42cd0ed21e5fb0 -t task_2, But that doesnt work. Heres the code below showing what the argument is.
def main(argv):
user_uuid = ''
doc_uuid = ''
task_id = 0
try:
opts, args = getopt.getopt(argv, "hu:d:t:", ["user_uuid=", "doc_uuid=", "task_id="])
except getopt.GetoptError:
print 'cw2 -u <user_uuid> -d <doc_uuid> -t <task_id>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'cw2.py -u <user_uuid> -d <doc_uuid> -t <task_id>'
sys.exit()
elif opt in ("-u", "--user_uuid"):
user_uuid = arg
elif opt in ("-d", "--doc_uuid"):
doc_uuid = arg
elif opt in ("-t", "--task_id"):
task_id = arg
if(int(task_id) == 1):
with open("../requirements.txt", 'r') as fin:
print("Requirments.txt file content")
print fin.read()
if(int(task_id) == 2):
if(doc_uuid == ''):
print(" No doc_uuid supplied")
else:
task_2(doc_uuid)
print("Histograms for per country beed saved in : static/results/countries_to_book_UUID.png")
print("Histograms for per continent beed saved in : static/results/continent_to_book_UUI.png")
elif(int(task_id) == 3):
task_3()
print("Histograms of browser usage has been seaved in 'static/results/simple_browser_usage.png' ")
print("Histograms of generalised browser usage has been seaved in 'static/results/general_browser_usage.png")
elif(int(task_id) == 4):
print("Data of 10 most active readers")
task_4(10)
elif(int(task_id) == 5):
if((user_uuid == '') | (doc_uuid == '')):
print("Provide user_uuid or/and doc_uuid")
# 938601f24509a9f1 , 110727005030-000000009cca70787e5fba1fda005c85
else:
task_5(user_uuid, doc_uuid)
if __name__ == "__main__":
main(sys.argv[1:])
Don't run it from the Python shell, this is set up to run as a normal program from your terminal.
cd <wherever this script is>
chmod a+x ./cw.py
./cw.py -u user_745409913574d4c6 -d doc_140228202800-6ef39a241f35301a9a42cd0ed21e5fb0 -t task_2
Also, the indentation is all wrong on the script in your question, but I assume that that's a copy-and-paste error.

Python argument parsing get wrong result

I have a python script to parse input argument from user .my code is like this
def get_arg(argv):
result = {}
input_file=stag_db=main_tb=stag_table=main_table = "";
debug="N"
msg = ''' Syntax: dt transaction date
-i input_file (E.g. input_file.tar.gz)
-ds staging_database
-dm main_database
-ts staging_table
-tm main_table
-db debug (Y/N)'''
try:
opts, args = getopt.getopt(argv,"hd:i:ds:dm:db:ts:tm:",["ifile=","ofile="])
print args
print opts
except getopt.GetoptError:
f_end_process(msg)
for opt, arg in opts:
if opt == '-h':
f_end_process(msg)
elif opt == "-i":
input_file = arg
elif opt == "-ds":
stag_db = arg
elif opt == "-dm":
main_tb = arg
elif opt == "-ts":
stag_table = arg
elif opt == "-tm":
main_table = arg
elif opt == "-db":
debug = arg
result = {'input_file':input_file,'stag_db':stag_db,'main_tb':main_tb,'stag_table':stag_table,'main_table':main_table}
print result
if '' in result.values():
exit_status=-1
f_end_process(msg)
result['debug']= debug
return result
def main():
global input_arg
input_arg = get_arg(sys.argv[1:])
print "process started at " +strftime("%Y-%m-%d %H:%M:%S")
print input_arg
i am running code like this
python main.py -i ok.txt -ds ds_val -dm dm_val -ts tas_val -tm tm_val
i want to parse all input arguments to a list. i imported all required modules to my script
now i am able to parse only -i input.How can i parse -tm,-ts ,-dm,-ds iputs?
In an interactive Python experiment with passing various argv arrays to getopt
>>> getopt.getopt(['-hd','1'],"hd:i:ds:dm:db:ts:tm:")
([('-h', ''), ('-d', '1')], [])
You did not tell it to look for an -hd option, but rather a -h and a -d that takes an argument.
Generally for multiletter options, argument parsers expect you to use --. Your getopt should be
>>> getopt.getopt(['--hd','1'],"i:",["hd=","ds=",...])
([('--hd', '1')], [])
But, do consider argparse.
You can manage using argparse, in just a few lines.
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--input-file', '-i', type=str,
help='input file (eg: input_file.tar.gz)')
parser.add_argument('--staging-db', '-S', type=str,
help='staging database name')
parser.add_argument('--main-db', '-M', type=str,
help='main database name')
parser.add_argument('--staging-table', '-s', type=str,
help='staging table name')
parser.add_argument('--main-table', '-m', type=str,
help='main table name')
parser.add_argument('--debug', '-d', type=bool, default=False,
help="activate debug mode (defaults to False)")
args = parser.parse_args()
The parser help is generated by argparse. You can output it by typing
$ python YOURSCRIPT.py --help

Categories

Resources