本文整理汇总了Python中builtins.type方法的典型用法代码示例。如果您正苦于以下问题:Python builtins.type方法的具体用法?Python builtins.type怎么用?Python builtins.type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类builtins
的用法示例。
在下文中一共展示了builtins.type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: scope_lookup
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def scope_lookup(self, node, name, offset=0):
"""Lookup where the given variable is assigned.
:param node: The node to look for assignments up to.
Any assignments after the given node are ignored.
:type node: NodeNG
:param name: The name of the variable to find assignments for.
:type name: str
:param offset: The line offset to filter statements up to.
:type offset: int
:returns: This scope node and the list of assignments associated to the
given name according to the scope where it has been found (locals,
globals or builtin).
:rtype: tuple(str, list(NodeNG))
"""
if name in self.scope_attrs and name not in self.locals:
try:
return self, self.getattr(name)
except exceptions.AttributeInferenceError:
return self, ()
return self._scope_lookup(node, name, offset)
示例2: igetattr
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def igetattr(self, name, context=None):
"""Infer the possible values of the given variable.
:param name: The name of the variable to infer.
:type name: str
:returns: The inferred possible values.
:rtype: iterable(NodeNG) or None
"""
# set lookup name since this is necessary to infer on import nodes for
# instance
context = contextmod.copy_context(context)
context.lookupname = name
try:
return bases._infer_stmts(self.getattr(name, context), context, frame=self)
except exceptions.AttributeInferenceError as error:
raise exceptions.InferenceError(
error.message, target=self, attribute=name, context=context
) from error
示例3: __init__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def __init__(self, lineno=None, col_offset=None, parent=None):
"""
:param lineno: The line that this node appears on in the source code.
:type lineno: int or None
:param col_offset: The column that this node appears on in the
source code.
:type col_offset: int or None
:param parent: The parent node in the syntax tree.
:type parent: NodeNG or None
"""
self.locals = {}
"""A map of the name of a local variable to the node defining the local.
:type: dict(str, NodeNG)
"""
super(GeneratorExp, self).__init__(lineno, col_offset, parent)
示例4: postinit
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def postinit(self, key=None, value=None, generators=None):
"""Do some setup after initialisation.
:param key: What produces the keys.
:type key: NodeNG or None
:param value: What produces the values.
:type value: NodeNG or None
:param generators: The generators that are looped through.
:type generators: list(Comprehension) or None
"""
self.key = key
self.value = value
if generators is None:
self.generators = []
else:
self.generators = generators
示例5: is_subtype_of
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def is_subtype_of(self, type_name, context=None):
"""Whether this class is a subtype of the given type.
:param type_name: The name of the type of check against.
:type type_name: str
:returns: True if this class is a subtype of the given type,
False otherwise.
:rtype: bool
"""
if self.qname() == type_name:
return True
for anc in self.ancestors(context=context):
if anc.qname() == type_name:
return True
return False
示例6: local_attr_ancestors
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def local_attr_ancestors(self, name, context=None):
"""Iterate over the parents that define the given name.
:param name: The name to find definitions for.
:type name: str
:returns: The parents that define the given name.
:rtype: iterable(NodeNG)
"""
# Look up in the mro if we can. This will result in the
# attribute being looked up just as Python does it.
try:
ancestors = self.mro(context)[1:]
except exceptions.MroError:
# Fallback to use ancestors, we can't determine
# a sane MRO.
ancestors = self.ancestors(context=context)
for astroid in ancestors:
if name in astroid:
yield astroid
示例7: __init__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def __init__(self, name, default=None, type=None, validate=None, transform=None, descr=None, long_descr=None,
alias=None, short=None, flag=False, yaml_only=False, command_line=False, subcommand=False, plugins=PLUGINS):
"""Defines an option.
See the documentation of the function option.
"""
if isinstance(validate, str):
for val in validate.split(","):
val = val.strip()
assert re.match(_VALIDATE_REGEX, val)
self.name = name
self.default = default
# as a shortcut, type can be a string.
if isinstance(type, str):
# @ is a shortcut for a trained model
type = type.replace("@", "TrainedModel")
# convert the string to an object representing the type
type = eval(type) # pylint: disable=W0123
self.type = type
if not self.type and default is not None:
self.type = builtins.type(default)
self.validate = validate
self.transform = transform
self.descr = descr or glossary.short_param_descr(name)
self.long_descr = long_descr or glossary.long_descr(name)
self.alias = alias
self.plugins = plugins
self.short = short
self.flag = flag
self.yaml_only = yaml_only
self.command_line = command_line
self.subcommand = subcommand
示例8: has_type
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def has_type(self, *types):
"""Check if the option is of a type in the list types"""
return _has_type(self.type, *types)
示例9: has_optional_type
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def has_optional_type(self):
"""Check if the type of the option permits None values.
"""
return getattr(self.type, '__origin__', None) == Union and \
type(None) in self.type.__args__ # pylint: disable=C0123
示例10: _type_descr
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def _type_descr(self, tp):
tp_descr = ""
if tp == TrainedModel:
tp_descr = "trained model"
elif tp == Optional[TrainedModel]:
tp_descr = "optional trained model"
elif tp == File:
tp_descr = "file"
elif tp == Optional[File]:
tp_descr = "optional file"
elif tp == List[File]:
tp_descr = "a list of files"
elif hasattr(tp, '__origin__'):
if tp.__origin__ in (list, List):
tp_descr = "a list of " + self._type_descr(tp.__args__[0])
elif tp.__origin__ == Union:
if len(tp.__args__) == 2 and type(None) in tp.__args__:
other = list(filter(lambda t: not isinstance(t, type(None)), tp.__args__))[0]
tp_descr = 'optional ' + self._type_descr(other)
else:
names = [self._type_descr(t) for t in tp.__args__]
if len(names) <= 2:
tp_descr = " or ".join(names)
else:
tp_descr = ", ".join(names[:-1])
tp_descr += " or " + names[-1]
elif tp:
if tp == str:
tp_descr = "string"
else:
tp_descr = getattr(tp, '__name__', str(tp))
return tp_descr
示例11: human_type
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def human_type(self):
tp_descr = self._type_descr(self.type)
if tp_descr and isinstance(self.validate, str):
tp_descr += " " + self.validate
elif tp_descr and isinstance(self.validate, (tuple, list)):
tp_descr = "one of (" + ", ".join(map(lambda e: str(e), self.validate)) + ")"
if self.default:
tp_descr += ", default: " + str(self.default)
return tp_descr
示例12: function_to_method
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def function_to_method(n, klass):
if isinstance(n, FunctionDef):
if n.type == "classmethod":
return bases.BoundMethod(n, klass)
if n.type != "staticmethod":
return bases.UnboundMethod(n)
return n
示例13: set_local
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def set_local(self, name, stmt):
"""Define that the given name is declared in the given statement node.
.. seealso:: :meth:`scope`
:param name: The name that is being defined.
:type name: str
:param stmt: The statement that defines the given name.
:type stmt: NodeNG
"""
# assert not stmt in self.locals.get(name, ()), (self, stmt)
self.locals.setdefault(name, []).append(stmt)
示例14: add_local_node
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def add_local_node(self, child_node, name=None):
"""Append a child that should alter the locals of this scope node.
:param child_node: The child node that will alter locals.
:type child_node: NodeNG
:param name: The name of the local that will be altered by
the given child node.
:type name: str or None
"""
if name != "__class__":
# add __class__ node as a child will cause infinite recursion later!
self._append_node(child_node)
self.set_local(name or child_node.name, child_node)
示例15: __getitem__
# 需要导入模块: import builtins [as 别名]
# 或者: from builtins import type [as 别名]
def __getitem__(self, item):
"""The first node the defines the given local.
:param item: The name of the locally defined object.
:type item: str
:raises KeyError: If the name is not defined.
"""
return self.locals[item][0]