本文整理汇总了Python中cache.Cache.get_class_by_name方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.get_class_by_name方法的具体用法?Python Cache.get_class_by_name怎么用?Python Cache.get_class_by_name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.get_class_by_name方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_dict
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_class_by_name [as 别名]
def import_dict(data, cache=None, **kwargs):
"""
Rebuild any instance of a python object instance that have been serialized using export_dict.
Args:
_data: A dict instance containing only basic data types.
**kwargs:
Returns:
"""
if cache is None:
from cache import Cache
cache = Cache()
#assert (data is not None)
if isinstance(data, dict) and '_class' in data:
# Handle Serializable object
cls_path = data['_class']
cls_name = cls_path.split('.')[-1]
cls_module = data.get('_class_module', None)
#cls_namespace = data.get('_class_namespace')
# HACK: Previously we were storing the complete class namespace.
# However this was not very flexible when we played with the class hierarchy.
# If we find a '_class_module' attribute, it mean we are doing thing the new way.
# Otherwise we'll let it slip for now.
if cls_module:
cls_def = cache.get_class_by_name(cls_name, module_name=cls_module)
else:
cls_def = cache.get_class_by_namespace(cls_name)
if cls_def is None:
logging.error("Can't create class instance for {0}, did you import to module?".format(cls_path))
return None
instance = create_class_instance(cls_def)
for key, val in data.items():
if key != '_class':
instance.__dict__[key] = import_dict(val, cache=cache)
return instance
# Handle array
elif is_data_list(data):
return [import_dict(v, cache=cache) for v in data]
# Handle other types of data
else:
return data
示例2: import_network
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_class_by_name [as 别名]
def import_network(network, fn_skip=None, cache=None, **kwargs):
"""
Recursively create class instances from provided network.
:param network: The network to read from.
:param fn_skip: A function taken a pymel.nodetypes.Network as argument that return True if we need to ignore a specific network.
:param cache: Used internally.
:return: An object instance corresponding to the provided network.
"""
if cache is None:
from cache import Cache
cache = Cache()
# Duck-type the network, if the '_class' attribute exist, it is a class instance representation.
# Otherwise it is a simple pymel.PyNode datatypes.
if not network.hasAttr('_class'):
return network
network_id = hash(network)
# Check if the object related to the network already exist in the cache and return it if found
cached_obj = cache.get_import_value_by_id(network_id)
if cached_obj is not None:
return cached_obj
# Check if the object is blacklisted. If it is, we'll still add it to the cache in case we encounter it again.
if fn_skip and fn_skip(network):
cache.set_import_value_by_id(network_id, None)
return None
cls_name = network.getAttr('_class')
# HACK: Previously we were storing the complete class namespace.
# However this was not very flexible when we played with the class hierarchy.
# If we find a '_class_module' attribute, it mean we are doing thing the new way.
# Otherwise we'll let it slip for now.
cls_module = network.getAttr('_class_module') if network.hasAttr('_class_module') else None
if cls_module:
cls_def = cache.get_class_by_name(cls_name, module_name=cls_module)
else:
cls_def = cache.get_class_by_namespace(cls_name)
if cls_def is None:
log.warning("Can't find class definiton for {0}. Returning None".format(cls_name))
return None
# HACK: Get latest definition
cls_def = getattr(sys.modules[cls_def.__module__], cls_def.__name__)
obj = cls_def()
# Monkey patch the network if supported
if isinstance(obj, object) and not isinstance(obj, dict):
obj._network = network
# Fill the import cache to make sure that self reference doesn't try to infinitly loop in it's import
cache.set_import_value_by_id(network_id, obj)
# Resolve wich attribute we'll want to import
attrs_by_longname = {}
for attr_name in pymel.listAttr(network, userDefined=True):
if '_' != attr_name[0]: # Attribute longName starting with '_' are considered private
attrs_by_longname[attr_name] = network.attr(attr_name)
# Filter compound children as we are only interested the compound value itself.
# ex: import translate and skip translateX, translateY, translateZ
for attr in attrs_by_longname.values():
if attr.isCompound():
for child in attr.getChildren():
child_longname = child.longName()
try:
attrs_by_longname.pop(child_longname)
except KeyError:
pass
for attr_name, attr in attrs_by_longname.iteritems():
# logging.debug('Importing attribute {0} from {1}'.format(key, _network.name()))
val = _get_network_attr(attr, fn_skip=fn_skip, cache=cache)
# if hasattr(obj, key):
if isinstance(obj, dict):
obj[attr_name.longName()] = val
else:
setattr(obj, attr_name, val)
# else:
# #logging.debug("Can't set attribute {0} to {1}, attribute does not exists".format(key, obj))
# Update network _uid to the current python variable context
# if _network.hasAttr('_uid'):
# _network._uid.set(id(obj))
# Hack: Find implemented class via duck-typing
# Implement a __callbackNetworkPostBuild__ method in your class instances as a callback.
try:
obj.__callbackNetworkPostBuild__()
except (AttributeError, TypeError):
pass
return obj