本文整理汇总了Python中types.ModuleType.__getattribute__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__getattribute__方法的具体用法?Python ModuleType.__getattribute__怎么用?Python ModuleType.__getattribute__使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.ModuleType
的用法示例。
在下文中一共展示了ModuleType.__getattribute__方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: proxysuper
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def proxysuper():
if verbose:
print "Testing super() for a proxy object..."
class Proxy(object):
def __init__(self, obj):
self.__obj = obj
def __getattribute__(self, name):
if name.startswith("_Proxy__"):
return object.__getattribute__(self, name)
else:
return getattr(self.__obj, name)
class B(object):
def f(self):
return "B.f"
class C(B):
def f(self):
return super(C, self).f() + "->C.f"
obj = C()
p = Proxy(obj)
vereq(C.__dict__["f"](p), "B.f->C.f")
示例2: _reset_lazymodule
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def _reset_lazymodule(module, cls_attrs):
"""Resets a module's lazy state from cached data.
"""
modclass = type(module)
del modclass.__getattribute__
del modclass.__setattr__
try:
del modclass._LOADING
except AttributeError:
pass
for cls_attr in _CLS_ATTRS:
try:
setattr(modclass, cls_attr, cls_attrs[cls_attr])
except KeyError:
pass
_reset_lazy_submod_refs(module)
示例3: __getattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def __getattr__(self, name):
if name in object_origins:
module = __import__(object_origins[name], None, None, [name])
for extra_name in all_by_module[module.__name__]:
setattr(self, extra_name, getattr(module, extra_name))
return getattr(module, name)
elif name in attribute_modules:
__import__("werkzeug." + name)
return ModuleType.__getattribute__(self, name)
示例4: __getattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def __getattr__(self, name):
if name in object_origins:
module = __import__(object_origins[name], None, None, [name])
for extra_name in all_by_module[module.__name__]:
setattr(self, extra_name, getattr(module, extra_name))
return getattr(module, name)
elif name in attribute_modules:
__import__('werkzeug.' + name)
return ModuleType.__getattribute__(self, name)
示例5: __getattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def __getattr__(self, name):
if name in _shorcuts.keys():
submodule = __import__(
"tempy." + _shorcuts[name], globals(), locals(), [name]
)
return getattr(submodule, name)
r = ModuleType.__getattribute__(self, name)
return r
示例6: pymods
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def pymods():
if verbose: print "Testing Python subclass of module..."
log = []
from types import ModuleType as MT
class MM(MT):
def __init__(self, name):
MT.__init__(self, name)
def __getattribute__(self, name):
log.append(("getattr", name))
return MT.__getattribute__(self, name)
def __setattr__(self, name, value):
log.append(("setattr", name, value))
MT.__setattr__(self, name, value)
def __delattr__(self, name):
log.append(("delattr", name))
MT.__delattr__(self, name)
a = MM("a")
a.foo = 12
x = a.foo
del a.foo
vereq(log, [("setattr", "foo", 12),
("getattr", "foo"),
("delattr", "foo")])
# http://python.org/sf/1174712
try:
class Module(types.ModuleType, str):
pass
except TypeError:
pass
else:
raise TestFailed("inheriting from ModuleType and str at the "
"same time should fail")
示例7: isinst_isclass
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def isinst_isclass():
if verbose:
print "Testing proxy isinstance() and isclass()..."
class Proxy(object):
def __init__(self, obj):
self.__obj = obj
def __getattribute__(self, name):
if name.startswith("_Proxy__"):
return object.__getattribute__(self, name)
else:
return getattr(self.__obj, name)
# Test with a classic class
class C:
pass
a = C()
pa = Proxy(a)
verify(isinstance(a, C)) # Baseline
verify(isinstance(pa, C)) # Test
# Test with a classic subclass
class D(C):
pass
a = D()
pa = Proxy(a)
verify(isinstance(a, C)) # Baseline
verify(isinstance(pa, C)) # Test
# Test with a new-style class
class C(object):
pass
a = C()
pa = Proxy(a)
verify(isinstance(a, C)) # Baseline
verify(isinstance(pa, C)) # Test
# Test with a new-style subclass
class D(C):
pass
a = D()
pa = Proxy(a)
verify(isinstance(a, C)) # Baseline
verify(isinstance(pa, C)) # Test
示例8: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def __setattr__(self, attr, value):
logger.debug("Setting attr {} to value {}, in LazyModule instance "
"of {}".format(attr, value, super(LazyModule, self)
.__getattribute__("__name__")))
_load_module(self)
return super(LazyModule, self).__setattr__(attr, value)
示例9: _clean_lazymodule
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __getattribute__ [as 别名]
def _clean_lazymodule(module):
"""Removes all lazy behavior from a module's class, for loading.
Also removes all module attributes listed under the module's class deletion
dictionaries. Deletion dictionaries are class attributes with names
specified in `_DELETION_DICT`.
Parameters
----------
module: LazyModule
Returns
-------
dict
A dictionary of deleted class attributes, that can be used to reset the
lazy state using :func:`_reset_lazymodule`.
"""
modclass = type(module)
_clean_lazy_submod_refs(module)
modclass.__getattribute__ = ModuleType.__getattribute__
modclass.__setattr__ = ModuleType.__setattr__
cls_attrs = {}
for cls_attr in _CLS_ATTRS:
try:
cls_attrs[cls_attr] = getattr(modclass, cls_attr)
delattr(modclass, cls_attr)
except AttributeError:
pass
return cls_attrs