Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use namespace returned by parse_known_args?

I am currently writing a Python script and trying to dynamically generate some arguments. However, an error is being thrown for the following script, stating 'Namespace' object is not iterable. Any ideas on how to fix?

import argparse
from os import path
import re

replacements = {}
pattern = '<<([^>]*)>>'

def user_replace(match):
   ## Pull from replacements dict or prompt
    placeholder = match.group(1)
    return (replacements[placeholder][0] 
         if placeholder in replacements else 
          raw_input('%s? ' % placeholder))

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('infile', type=argparse.FileType('r'))
    parser.add_argument('outfile', type=argparse.FileType('w'))

    required, extra = parser.parse_known_args()
    infile, outfile = required.infile, required.outfile
    args = re.findall(pattern, infile.read())
    args = list(set(args))
    infile.seek(0)

    parser = argparse.ArgumentParser()
    for arg in args:
        parser.add_argument('--' + arg.lower())

    replacements = vars(parser.parse_args(extra))

    matcher = re.compile(pattern)

    for line in args.infile:
        new_line = matcher.sub(user_replace, line)
        args.outfile.write(new_line)

    args.infile.close()
    args.outfile.close()

if __name__ == '__main__':
    main()

The error is with the returned value of parser.parse_known_args(). Any ideas on how I could bypass this though? Someone suggested creating an object and using the dict interface, but I don't know what this entails exactly. I'm really new to Python, so I don't understand why (infile, outfile), extra = parser.parse_known_args() wouldn't work.

Edit: Updated with two fixes. First fixed the error above by using the accepted answer below. Second, also fixed an error where I was getting flagged for trying to add the same argument twice. Fixed by making args a set then back to a list. Now my script runs, but the optional arguments have no effect. Any ideas?

like image 586
Shayon Saleh Avatar asked Jul 26 '11 19:07

Shayon Saleh


1 Answers

ArgumentParser.parse_known_args returns a namespace and a list of the remaining arguments. Namespaces aren't iterable, so when you try to assign one to the tuple (infile, outfile) you get the "not iterable" error.

Instead, you should write something like

namespace, extra = parser.parse_known_args()

and then access the parsed arguments as namespace.infile and namespace.outfile.

like image 63
Gareth Rees Avatar answered Nov 01 '22 08:11

Gareth Rees