本文整理汇总了Python中cache.Cache.get_import_value_by_id方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.get_import_value_by_id方法的具体用法?Python Cache.get_import_value_by_id怎么用?Python Cache.get_import_value_by_id使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.get_import_value_by_id方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: export_dict
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_import_value_by_id [as 别名]
def export_dict(data, skip_None=True, recursive=True, cache=None, **args):
"""
Export an object instance (data) into a dictionary of basic data types (including pymel.Pynode and pymel.Attribute).
Args:
data: An instance of the build-in python class object.
skip_None: Don't store an attribute if is value is None.
recursive: Export recursively embedded instances of object in (excluding protected and private properties).
**args:
Returns: A dict instance containing only basic data types.
"""
if cache is None:
from cache import Cache
cache = Cache()
# Check if we already exported this data.
# This allow us to support cyclic references.
data_id = id(data)
result = cache.get_import_value_by_id(data_id)
if result is not None:
print("Using cache for {0}".format(data))
return result
data_type = get_data_type(data)
# object instance
if data_type == TYPE_COMPLEX:
data_cls = data.__class__
result = {
'_class': data_cls.__name__,
'_class_namespace': get_class_namespace(data_cls),
'_class_module': get_class_module_root(data_cls),
'_uid': id(data)
}
# Cache it as soon as possible since we might use recursivity.
cache.set_import_value_by_id(data_id, result)
for key, val in (data.items() if isinstance(data, dict) else data.__dict__.items()): # TODO: Clean
# Ignore private keys (starting with an underscore)
if key[0] == '_':
continue
if not skip_None or val is not None:
if (data_type == TYPE_COMPLEX and recursive is True) or data_type == TYPE_LIST:
val = export_dict(val, skip_None=skip_None, recursive=recursive, cache=cache, **args)
if not skip_None or val is not None:
result[key] = val
else:
# Handle other types of data
if data_type == TYPE_BASIC:
result = data
# Handle iterable
elif data_type == TYPE_LIST:
result = [export_dict(v, skip_None=skip_None, cache=cache, **args) for v in data if not skip_None or v is not None]
elif data_type == TYPE_DAGNODE:
result = data
else:
logging.warning("[exportToBasicData] Unsupported type {0} ({1}) for {2}".format(type(data), data_type, data))
result = None
cache.set_import_value_by_id(data_id, result)
return result
示例2: import_network
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import get_import_value_by_id [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