Dowemo
0 0 0 0


Question:

I am trying to pass BioPython sequences to Ilya Stepanov's implementation of Ukkonen's suffix tree algorithm in iPython's notebook environment. I am stumbling on the argparse component.

I have never had to deal directly with argparse before. How can I use this without rewriting main()?

By the by, this writeup of Ukkonen's algorithm is fantastic.


Best Answer:


If you use ipython for testing then transforming argparse into class format can be a quick dummy solution.

#!/usr/bin/env python
from __future__ import print_function
import sys
def transform():
    if len(sys.argv) <2:
        print('Usage : python arg2cls.py [target.py] [target2.py(optional)] ...')
        sys.exit(0)
    sys.argv.pop(0)
    for fname in sys.argv:
        if(__name__!='__main__'):
            print(fname)
        try :
            with open(fname, 'r') as f:
                txt = f.read()
                t = txt.split('argparse.ArgumentParser')
                if len(t) <2:
                    print('Nothing to make from file.')
                    sys.exit(0)
                t = t[1].split('.parse_args')[0].split('add_argument')
                t.pop(0)
                name=[]
                val=[]
                for i, x in enumerate(t):
                    t =x.split('--')[1]
                    name.append(t.split(''')[0].replace('-','_'))
                    dtype = ''
                    if('type' in t):
                        dtype = t.split('type=')[1].split(',')[0]
                    dfult = t.split('default=')
                    if len(dfult) <2:
                        val.append('###manual_setting_required###')
                    elif (dtype in ['int','float','long']):
                        val.append(dfult[1].split(',')[0].replace(''',''))
                    else:
                        val.append(dfult[1].split(',')[0])
                print('')
                print('class args:')
                for i in zip(name,val):
                    print('    ',end='')
                    print(i[0],'=',i[1].replace('"',''))
                print('')
        except IOError:
            print('IOError : Maybe no such file.', fname)
if(__name__ == "__main__"):
    transform()

this script makes argparse module into class format, so you don't need to edit other internal codes. Execution example :

python this_script.py target.py

then you copy & paste class format to replace your argparse. https://github.com/sngjuk/Argparse-to-classThis repo also offers Online quick transformation. Hope that it would be helpful for your testing.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs