0

See code below:

#!/opt/rh/python33/root/usr/bin/python
import sys
print( sys.argv[1:-1] )
if ( len( sys.argv ) < 5 or len( sys.argv ) % 3 != 2 ) :
    print( '''Oy vey! You need to input: <1lib name> <1schematic name > <nview name> .. <nlib name> <nschematic name > <nview name>  <output fname>''' )
    exit ( 1 )
print( len( sys.argv[1:-1] ) )
for lib_name,cell_name,view_name in sys.argv[1:-1] :
    print( "lib is:" + lib_name )
    print( "cell is:" + cell_name )
    print( "view is:" + view_name )

This code should be very simple. I am running the code, and I get an error:

~]./run_automated_block.py ko lo pm do
['ko', 'lo', 'pm']
3
Traceback (most recent call last):
   File "~/run_automated_block.py", line 8, in <module>
     for lib_name,cell_name,view_name in sys.argv[1:-1] :
ValueError: need more than 2 values to unpack
[~]$

I don't get it. The length of the slice is 3. There are 3 variables in the for loop.

1
  • That's an awful way to handle cli args. Why don't you use argparse ? Commented Apr 3, 2018 at 10:27

1 Answer 1

1

When you're iterating over the sys.argv[1:-1] list you're iterating one element at the time, not three as you might think, so it naturally complains that it cannot unpack ko (lo, pm...) into three variables - it would if you had the arguments themselves to be of length 3 (e.g. ko1 lo1 po1) but then it would unpack individual characters.

If you insist doing it this way, you can zip the values together into a tuple of three:

for lib_name, cell_name, view_name in zip(*[iter(sys.argv[1:-1])]*3):
    print("lib is:" + lib_name)
    print("cell is:" + cell_name)
    print("view is:" + view_name)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.