本文整理汇总了Python中types.ModuleType.__setattr__方法的典型用法代码示例。如果您正苦于以下问题:Python ModuleType.__setattr__方法的具体用法?Python ModuleType.__setattr__怎么用?Python ModuleType.__setattr__使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.ModuleType
的用法示例。
在下文中一共展示了ModuleType.__setattr__方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _reset_lazymodule
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [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)
示例2: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self, name, value):
#if hasattr(self, "instances"): self.instances[name] = value
ModuleType.__setattr__(self, name, value)
示例3: pymods
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [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")
示例4: carloverre
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def carloverre():
if verbose:
print "Testing prohibition of Carlo Verre's hack..."
try:
object.__setattr__(str, "foo", 42)
except TypeError:
pass
else:
raise TestFailed, "Carlo Verre __setattr__ suceeded!"
try:
object.__delattr__(str, "lower")
except TypeError:
pass
else:
raise TestFailed, "Carlo Verre __delattr__ succeeded!"
示例5: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [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)
示例6: _clean_lazymodule
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [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
示例7: _reset_lazy_submod_refs
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def _reset_lazy_submod_refs(module):
modclass = type(module)
for deldict in _DELETION_DICT:
try:
resetnames = getattr(modclass, deldict)
except AttributeError:
continue
for name, submod in resetnames.items():
super(LazyModule, module).__setattr__(name, submod)
示例8: __setattr__
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def __setattr__(self, name, value):
if hasattr(self, "env"):
self.env[name] = value
ModuleType.__setattr__(self, name, value)
示例9: overloading
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def overloading():
if verbose: print "Testing operator overloading..."
class B(object):
"Intermediate class because object doesn't have a __setattr__"
class C(B):
def __getattr__(self, name):
if name == "foo":
return ("getattr", name)
else:
raise AttributeError
def __setattr__(self, name, value):
if name == "foo":
self.setattr = (name, value)
else:
return B.__setattr__(self, name, value)
def __delattr__(self, name):
if name == "foo":
self.delattr = name
else:
return B.__delattr__(self, name)
def __getitem__(self, key):
return ("getitem", key)
def __setitem__(self, key, value):
self.setitem = (key, value)
def __delitem__(self, key):
self.delitem = key
def __getslice__(self, i, j):
return ("getslice", i, j)
def __setslice__(self, i, j, value):
self.setslice = (i, j, value)
def __delslice__(self, i, j):
self.delslice = (i, j)
a = C()
vereq(a.foo, ("getattr", "foo"))
a.foo = 12
vereq(a.setattr, ("foo", 12))
del a.foo
vereq(a.delattr, "foo")
vereq(a[12], ("getitem", 12))
a[12] = 21
vereq(a.setitem, (12, 21))
del a[12]
vereq(a.delitem, 12)
vereq(a[0:10], ("getslice", 0, 10))
a[0:10] = "foo"
vereq(a.setslice, (0, 10, "foo"))
del a[0:10]
vereq(a.delslice, (0, 10))
示例10: _lazy_module
# 需要导入模块: from types import ModuleType [as 别名]
# 或者: from types.ModuleType import __setattr__ [as 别名]
def _lazy_module(modname, error_strings, lazy_mod_class):
with _ImportLockContext():
fullmodname = modname
fullsubmodname = None
# ensure parent module/package is in sys.modules
# and parent.modname=module, as soon as the parent is imported
while modname:
try:
mod = sys.modules[modname]
# We reached a (base) module that's already loaded. Let's stop
# the cycle. Can't use 'break' because we still want to go
# through the fullsubmodname check below.
modname = ''
except KeyError:
err_s = error_strings.copy()
err_s.setdefault('module', modname)
class _LazyModule(lazy_mod_class):
_lazy_import_error_msgs = {'msg': err_s.pop('msg')}
try:
_lazy_import_error_msgs['msg_callable'] = \
err_s.pop('msg_callable')
except KeyError:
pass
_lazy_import_error_strings = err_s
_lazy_import_callables = {}
_lazy_import_submodules = {}
def __repr__(self):
return "Lazily-loaded module {}".format(self.__name__)
# A bit of cosmetic, to make AttributeErrors read more natural
_LazyModule.__name__ = 'module'
# Actual module instantiation
mod = sys.modules[modname] = _LazyModule(modname)
# No need for __spec__. Maybe in the future.
#if ModuleSpec:
# ModuleType.__setattr__(mod, '__spec__',
# ModuleSpec(modname, None))
if fullsubmodname:
submod = sys.modules[fullsubmodname]
ModuleType.__setattr__(mod, submodname, submod)
_LazyModule._lazy_import_submodules[submodname] = submod
fullsubmodname = modname
modname, _, submodname = modname.rpartition('.')
return sys.modules[fullmodname]