导入自定义python模块..为什么只有一些元素可以继承? [英] importing custom python modules.. why do only some elements carry over?

查看:176
本文介绍了导入自定义python模块..为什么只有一些元素可以继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个许多脚本最终需要导入的python模块:

I need to create a python module that many scripts need to ultimately import from:


  • 自定义arg解析以设置格式和常量args (即我的详细和帮助args,以及在脚本中视觉上令人愉悦/一致的一切)

我的自定义模块目前独立工作,我'我很满意现在继续前进,但我似乎无法将其正确导入另一个脚本。该模块导入另一个模块(ver.py),定义一个简单的变量常量,没有任何问题(我希望只在一个位置存在一个版本代码)。但是当我尝试将该文件导入另一个文件(例如myexecutable.py)并添加用于解析其他参数的代码时,它会失败。此外,我在目录中有_ _init__.py,其中包含我正在使用的所有文件。

My custom module is currently working independently and I'm satisfied enough to move forward for now, but I can't seem to import it into another script properly. THAT module imports another module (ver.py) defining a simple variable constant with no issues (a version code I want to exist in only one location). But when I try to import that file into another (e.g. myexecutable.py) and add code for parsing additional arguments, it fails. Also, I have _ _init__.py in the dir which contains all of the files I'm working with right now.

此代码已经发生了很大变化,我可以'让它恢复到部分工作状态,但我可以让它没有错误。这里不是错误:

This code has changed so much and I can't get it back to a "partially working" state, but I can get it to have no errors. Here's what's not throwing errors:

#!/usr/bin/env python
import argparse
import par as pp  ##par.py is my working parsing code

pp.preq.add_argument('input', metavar=" INPUT.ext", type=argparse.FileType('rt'))
pp.preq.add_argument('output', metavar=" OUTPUT.ext", type=argparse.FileType('wt'))

值得注意的是,我希望在执行的文件中定义'description'和'usage',而不是导入的文件。以下是导入文件(par.py)的子集:

Of note, I am hoping to define the 'description' and 'usage' in the executed file, not the imported file. Here's a subset of what the imported file (par.py) has:

class USAGEformat(argparse.RawTextHelpFormatter):
        def add_usage(self, usage, actions, groups, prefix=None):
                if prefix is None:
                        prefix = 'nameconstant '+v.VERSION+'\n\nUSAGE:  '+prog
                super(USAGEformat, self).add_usage(usage, actions, groups, prefix)

parse = argparse.ArgumentParser(add_help=False, formatter_class=USAGEformat)

preq = parse.add_argument_group("___________________\n++ COMPULSORY ARGS")

当我尝试添加时除了ArgumentParser之外,它还无法识别我的USAGEformat类。在某些时候,我让我的脚本部分识别它,并采取我定义的用法,但不是我定义的描述或强制参数。无法弄明白我做了什么..

When I try to add in anything more with ArgumentParser, it doesn't recognize my USAGEformat class. At some point I got my script to recognize it partially, and take my defined usage but not my defined description or compulsory arguments. Can't figure out what I did though..

为什么我的变量仅在导入时才变得简单:

Why are my variables only working when importing when they are simple:

VERSION = "v1.0"

但是不是当它们更复杂的时候,为什么我的课程也没有延续?我假设我缺少一些基本的理解,如何工作,并没有弄清楚那是什么?我一直在用自己的教程/谷歌教这门语言,因为我不是计算机科学家,我会非常感谢你的某些方向。

but not when they are more complex, and why are my classes not carrying over either? I'm assuming I'm missing some basic understanding of how this works and failing to figure out what that is? I've been teaching this language to myself with tutorials/Google because I'm not a computer scientist, and some direction would be greatly appreciated.

推荐答案

您之前的问题和代码:

完全自定义的Python帮助使用

使用 import par as pp ,所有变量,必须使用 pp。前缀访问该模块中定义的类和函数。因此 pp.parser 是导入时创建的解析器对象。 pp.preq 是其中一个参数组。

With import par as pp, the all variables, classes and functions defined in that module have to be accessed with the pp. prefix. Thus pp.parser is the parser object that was created on import. pp.preq is one of the argument groups.

pp.USAGEformat 是自定义格式化程序类。但是,在创建解析器时以及导入 par 时,不会创建格式化程序,此类的对象。当您要求 help 用法('-h',<$ c)时,会从指定的类创建格式化程序$ C> pp.parser.print_help())。

pp.USAGEformat is the custom formatter class. But a formatter, an object of this class is not created when the parser is created nor when par is imported. A formatter is created from the specified class when you ask for help or usage ('-h', pp.parser.print_help()).

(编辑)这是错误的:



此格式化程序将从当前命名空间(导入的命名空间)中获取全局变量,如 version prog ,而不是 pp one。

This formatter will take globals variables like version and prog from the current namespace (the importing one), not the pp one.


格式化程序仍然需要导入的命名空间中的 version 。但我可以用以下方式改变:

The formatter still takes version from the imported namespace. But I can change that with:

pp.version = '3.0'

(请参阅 https://stackoverflow.com/中的编辑a / 47118317/901925

您可以通过更改<$来在 pre 中测试此行为c $ c> version 在文件的末尾并执行一个新的 print_help()。你会看到显示的变化。

You can test this behavior in pre itself by changing version at the end of the file and doing a new print_help(). You'll see a change in the display.

pp.parser 另一方面是在导入时创建的。您可以修改 pp.parser.description ,即创建时设置的属性。设置或更改 pp.description 不会这样做。

pp.parser on the other hand is created on import. You could modify pp.parser.description, the attribute that was set on creation. Setting or changing pp.description doesn't do it.

当您执行解析器时.print_help(),它调用 parser.format_help ,后者又调用 parser._get_formatter 。该方法使用 parser.formatter_class 创建一个 formatter 对象,然后填充并执行。这种方法有很多灵活性,但可能令人困惑。

When you do parser.print_help(), it calls parser.format_help, which in turn calls parser._get_formatter. That method uses parser.formatter_class to create a formatter object, which is then 'filled' and 'executed'. There's a lot of flexibility in this approach, but it can be confusing.

这篇关于导入自定义python模块..为什么只有一些元素可以继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆