Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def loadclass(module_and_name):
"""Loads the module and returns the class.
>>> cls = loadclass('datetime.datetime')
>>> cls.__name__
'datetime'
>>> loadclass('does.not.exist')
>>> loadclass('__builtin__.int')()
0
"""
try:
module, name = module_and_name.rsplit('.', 1)
module = util.untranslate_module_name(module)
__import__(module)
return getattr(sys.modules[module], name)
except:
return None
def loadclass(module_and_name):
"""Loads the module and returns the class.
>>> cls = loadclass('datetime.datetime')
>>> cls.__name__
'datetime'
>>> loadclass('does.not.exist')
>>> loadclass('__builtin__.int')()
0
"""
try:
module, name = module_and_name.rsplit('.', 1)
module = util.untranslate_module_name(module)
__import__(module)
return getattr(sys.modules[module], name)
except:
return ClassRegistry.get(module_and_name)
def _mktyperef(obj):
"""Return a typeref dictionary
>>> _mktyperef(AssertionError)
{'py/type': '__builtin__.AssertionError'}
"""
return {tags.TYPE: util.importable_name(obj)}
def _flatten_obj_instance(self, obj):
"""Recursively flatten an instance and return a json-friendly dict
"""
data = {}
has_class = hasattr(obj, '__class__')
has_dict = hasattr(obj, '__dict__')
has_slots = not has_dict and hasattr(obj, '__slots__')
has_getnewargs = util.has_method(obj, '__getnewargs__')
has_getnewargs_ex = util.has_method(obj, '__getnewargs_ex__')
has_getinitargs = util.has_method(obj, '__getinitargs__')
has_reduce, has_reduce_ex = util.has_reduce(obj)
# Support objects with __getstate__(); this ensures that
# both __setstate__() and __getstate__() are implemented
has_getstate = hasattr(obj, '__getstate__')
# not using has_method since __getstate__() is handled separately below
if has_class:
cls = obj.__class__
else:
cls = type(obj)
# Check for a custom handler
class_name = util.importable_name(cls)
handler = handlers.get(cls, handlers.get(class_name))
method = _obj_setattr
for k, v in sorted(obj.items(), key=util.itemgetter):
# ignore the reserved attribute
if ignorereserved and k in tags.RESERVED:
continue
if isinstance(k, numeric_types):
str_k = unicode(k)
else:
str_k = k
self._namestack.append(str_k)
k = restore_key(k)
# step into the namespace
value = self._restore(v)
if (util.is_noncomplex(instance) or
util.is_dictionary_subclass(instance)):
instance[k] = value
else:
setattr(instance, k, value)
# This instance has an instance variable named `k` that is
# currently a proxy and must be replaced
if isinstance(value, _Proxy):
self._proxies.append((instance, k, value, method))
# step out
self._namestack.pop()
def _get_flattener(self, obj):
if PY2 and isinstance(obj, file):
return self._flatten_file
if util.is_primitive(obj):
return lambda obj: obj
if util.is_bytes(obj):
return self._flatten_bytestring
list_recurse = self._list_recurse
if util.is_list(obj):
if self._mkref(obj):
return list_recurse
else:
self._push()
return self._getref
# We handle tuples and sets by encoding them in a "(tuple|set)dict"
if util.is_tuple(obj):
def _flatten_dict_obj(self, obj, data=None):
"""Recursively call flatten() and return json-friendly dict
"""
if data is None:
data = obj.__class__()
flatten = self._flatten_key_value_pair
for k, v in sorted(obj.items(), key=util.itemgetter):
flatten(k, v, data)
# the collections.defaultdict protocol
if hasattr(obj, 'default_factory') and callable(obj.default_factory):
factory = obj.default_factory
if util.is_type(factory):
# Reference the type
value = _mktyperef(factory)
else:
# Create an instance from the factory and assume that the
# resulting instance is a suitable examplar.
value = self._flatten(handlers.CloneFactory(factory()))
data['default_factory'] = value
return data