简体   繁体   English

如何在 iPython 笔记本中调用用 argparse 编写的模块

[英]How to call module written with argparse in iPython notebook

I am trying to pass BioPython sequences to Ilya Stepanov's implementation of Ukkonen's suffix tree algorithm in iPython's notebook environment.我正在尝试将 BioPython 序列传递给Ilya Stepanov 在 iPython 的笔记本环境中实现 Ukkonen 的后缀树算法 I am stumbling on the argparse component.我偶然发现了 argparse 组件。

I have never had to deal directly with argparse before.我以前从来没有直接处理过 argparse。 How can I use this without rewriting main()?如何在不重写 main() 的情况下使用它?

By the by, this writeup of Ukkonen's algorithm is fantastic . 顺便说一句,Ukkonen 算法的这篇文章很棒

An alternative to use argparse in Ipython notebooks is passing a string to:在 Ipython 笔记本中使用 argparse 的另一种方法是将字符串传递给:

args = parser.parse_args() (line 303 from the git repo you referenced.) args = parser.parse_args() (您引用的 git repo 中的第 303 行。)

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())

Edit: It works编辑:它有效

Using args = parser.parse_args(args=[]) would solve execution problem.使用args = parser.parse_args(args=[])将解决执行问题。

or you can declare it as class format.或者您可以将其声明为类格式。

class Args:
  data = './data/penn'
  model = 'LSTM'
  emsize = 200
  nhid = 200

args=Args()

I've had a similar problem before, but using optparse instead of argparse .我以前遇到过类似的问题,但使用optparse而不是argparse

You don't need to change anything in the original script, just assign a new list to sys.argv like so:您无需更改原始脚本中的任何内容,只需为sys.argv分配一个新列表,如下所示:

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()

If all arguments have a default value, then adding this to the top of the notebook should be enough:如果所有参数都有默认值,那么将其添加到笔记本顶部就足够了:

import sys
sys.argv = ['']

(otherwise, just add necessary arguments instead of the empty string) (否则,只需添加必要的参数而不是空字符串)

I ended up using BioPython to extract the sequences and then editing Ilya Steanov's implementation to remove the argparse methods.我最终使用 BioPython 来提取序列,然后编辑 Ilya Steanov 的实现以删除 argparse 方法。

import imp
seqs = []
lcsm = imp.load_source('lcsm', '/path/to/ukkonen.py')
for record in SeqIO.parse('/path/to/sequences.txt', 'fasta'):
    seqs.append(record)
lcsm.main(seqs)

For the algorithm, I had main() take one argument, his strings variable, but this sends the algorithm a list of special BioPython Sequence objects , which the re module doesn't like.对于算法,我让main()接受一个参数,即他的strings变量,但这会向算法发送一个特殊的BioPython 序列对象列表,这是 re 模块不喜欢的。 So I had to extract the sequence string所以我不得不提取序列字符串

suffix_tree.append_string(s)

to

suffix_tree.append_string(str(s.seq))

which seems kind of brittle, but that's all I've got for now.这似乎有点脆弱,但这就是我现在所拥有的。

I face a similar problem in invoking argsparse, the string '-f' was causing this problem.我在调用 argsparse 时遇到了类似的问题,字符串 '-f' 导致了这个问题。 Just removing that from sys.srgv does the trick.只需从 sys.srgv 中删除它就可以了。

import sys
if __name__ == '__main__':
    if '-f' in sys.argv:
        sys.argv.remove('-f')
    main()

Here is my code which works well and I won't worry about the environment changed.这是我的代码,它运行良好,我不会担心环境改变。

import sys
temp_argv = sys.argv

try:
    sys.argv = ['']
    print(sys.argv)
    args = argparse.parser_args()
finally:
    sys.argv = temp_argv
    print(sys.argv)

Suppose you have this small code in python:假设你在 python 中有这个小代码:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", help="increase output verbosity",
                    action="store_true")
parser.add_argument("-v_1", "--verbose_1", help="increase output verbosity",
                    action="store_true")
args = parser.parse_args()

To write this code in Jupyter notebook write this:要在 Jupyter 笔记本中编写此代码,请编写以下代码:

import argparse

args = argparse.Namespace(verbose=False, verbose_1=False)

Note: In python, you can pass arguments on runtime but in the Jupyter notebook that will not be the case so be careful with the data types of your arguments.注意:在 python 中,您可以在运行时传递参数,但在 Jupyter notebook 中则不然,因此请注意参数的数据类型。

If arguments passed by the iPython environment can be ignored (do not conflict with the specified arguments), then the following works like a charm:如果 iPython 环境传递的参数可以被忽略(不与指定的参数冲突),那么下面的工作就像一个魅力:

# REPLACE   args = parser.parse_args()   with:
args, unknown = parser.parse_known_args()

From: https://stackoverflow.com/a/12818237/11750716来自: https ://stackoverflow.com/a/12818237/11750716

If you don't want to change any of the arguments and working mechanisms from the original argparse function you have written or copied.如果您不想更改您编写或复制的原始 argparse 函数的任何参数和工作机制。

To let the program work then there is a simple solution that works most of the time.为了让程序正常工作,有一个在大多数情况下都可以工作的简单解决方案。

You could just install jupyter-argparser using the below command:您可以使用以下命令安装jupyter-argparser

pip install jupyter_argparser

The codes work without any changes thanks to the maintainer of the package.由于包的维护者,代码无需任何更改即可工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM