close

How to call module written with argparse in iPython notebook

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to call module written with argparse in iPython notebook in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to call module written with argparse in iPython notebook?

  1. How to call module written with argparse in iPython notebook?

    An alternative to use argparse in Ipython notebooks is passing a string to:
    args = parser.parse_args() (line 303 from the git repo you referenced.)

  2. call module written with argparse in iPython notebook

    An alternative to use argparse in Ipython notebooks is passing a string to:
    args = parser.parse_args() (line 303 from the git repo you referenced.)

Method 1

An alternative to use argparse in Ipython notebooks is passing a string to:

args = parser.parse_args() (line 303 from the git repo you referenced.)

Would be something like:

parser = argparse.ArgumentParser(
        description='Searching longest common substring. '
                    'Uses Ukkonen\'s suffix tree algorithm and generalized suffix tree. '
                    'Written by Ilya Stepanov (c) 2013')

parser.add_argument(
        'strings',
        metavar='STRING',
        nargs='*',
        help='String for searching',
    )

parser.add_argument(
        '-f',
        '--file',
        help='Path for input file. First line should contain number of lines to search in'
    )

and

args = parser.parse_args("AAA --file /path/to/sequences.txt".split())

Method 2

I’ve had a similar problem before, but using optparse instead of argparse.

You don’t need to change anything in the original script, just assign a new list to sys.argv like so:

if __name__ == "__main__":
    from Bio import SeqIO
    path = '/path/to/sequences.txt'
    sequences = [str(record.seq) for record in  SeqIO.parse(path, 'fasta')]
    sys.argv = ['-f'] + sequences
    main()

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read