本文整理汇总了Python中pydoc.ErrorDuringImport方法的典型用法代码示例。如果您正苦于以下问题:Python pydoc.ErrorDuringImport方法的具体用法?Python pydoc.ErrorDuringImport怎么用?Python pydoc.ErrorDuringImport使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pydoc
的用法示例。
在下文中一共展示了pydoc.ErrorDuringImport方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_string_to_type
# 需要导入模块: import pydoc [as 别名]
# 或者: from pydoc import ErrorDuringImport [as 别名]
def convert_string_to_type(string_value):
"""Converts a string into a type or class
:param string_value: the string to be converted, e.g. "int"
:return: The type derived from string_value, e.g. int
"""
# If the parameter is already a type, return it
if string_value in ['None', type(None).__name__]:
return type(None)
if isinstance(string_value, type) or isclass(string_value):
return string_value
# Get object associated with string
# First check whether we are having a built in type (int, str, etc)
if sys.version_info >= (3,):
import builtins as builtins23
else:
import __builtin__ as builtins23
if hasattr(builtins23, string_value):
obj = getattr(builtins23, string_value)
if type(obj) is type:
return obj
# If not, try to locate the module
try:
obj = locate(string_value)
except ErrorDuringImport as e:
raise ValueError("Unknown type '{0}'".format(e))
# Check whether object is a type
if type(obj) is type:
return locate(string_value)
# Check whether object is a class
if isclass(obj):
return obj
# Raise error if none is the case
raise ValueError("Unknown type '{0}'".format(string_value))
示例2: generatedocs
# 需要导入模块: import pydoc [as 别名]
# 或者: from pydoc import ErrorDuringImport [as 别名]
def generatedocs(module, filename):
try:
sys.path.insert(0, os.getcwd() + '/..')
# Attempt import
mod = pydoc.safeimport(module)
if mod is None:
print("Module not found")
# Module imported correctly, let's create the docs
with open(filename, 'w') as f:
f.write(getmarkdown(mod))
except pydoc.ErrorDuringImport as e:
print("Error while trying to import " + module)
# if __name__ == '__main__':
示例3: main
# 需要导入模块: import pydoc [as 别名]
# 或者: from pydoc import ErrorDuringImport [as 别名]
def main(argv=None):
parser = ArgumentParser()
parser.add_argument('function')
parser.add_argument('args', nargs=REMAINDER)
args = parser.parse_args(argv)
try:
func = pydoc.locate(args.function)
except pydoc.ErrorDuringImport as exc:
raise exc.value from None
if func is None:
raise ImportError('Failed to locate {!r}'.format(args.function))
argparse_kwargs = (
{'prog': ' '.join(sys.argv[:2])} if argv is None else {})
retval = run(func, argv=args.args, argparse_kwargs=argparse_kwargs)
sys.displayhook(retval)
示例4: generatedocs
# 需要导入模块: import pydoc [as 别名]
# 或者: from pydoc import ErrorDuringImport [as 别名]
def generatedocs(module):
try:
sys.path.append(os.getcwd())
# Attempt import
mod = pydoc.safeimport(module)
if mod is None:
print("Module not found")
# Module imported correctly, let's create the docs
return getmarkdown(mod)
except pydoc.ErrorDuringImport as e:
print("Error while trying to import " + module)
示例5: generatedocs
# 需要导入模块: import pydoc [as 别名]
# 或者: from pydoc import ErrorDuringImport [as 别名]
def generatedocs(module):
try:
sys.path.append(os.getcwd())
# Attempt import
mod = pydoc.safeimport(module)
if mod is None:
print("Module not found")
# Module imported correctly, let's create the docs
return getmarkdown(mod)
except pydoc.ErrorDuringImport as e:
print("Error while trying to import " + module)