本文整理汇总了Python中django.utils.functional.cached_property方法的典型用法代码示例。如果您正苦于以下问题:Python functional.cached_property方法的具体用法?Python functional.cached_property怎么用?Python functional.cached_property使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.utils.functional
的用法示例。
在下文中一共展示了functional.cached_property方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __reduce__
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def __reduce__(self):
"""
Pickling should return the model._meta.fields instance of the field,
not a new copy of that field. So, use the app registry to load the
model and then the field back.
"""
if not hasattr(self, 'model'):
# Fields are sometimes used without attaching them to models (for
# example in aggregation). In this case give back a plain field
# instance. The code below will create a new empty instance of
# class self.__class__, then update its dict with self.__dict__
# values - so, this is very close to normal pickle.
state = self.__dict__.copy()
# The _get_default cached_property can't be pickled due to lambda
# usage.
state.pop('_get_default', None)
return _empty, (self.__class__,), state
return _load_field, (self.model._meta.app_label, self.model._meta.object_name,
self.name)
示例2: __reduce__
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def __reduce__(self):
"""
Pickling should return the model._meta.fields instance of the field,
not a new copy of that field. So, we use the app registry to load the
model and then the field back.
"""
if not hasattr(self, 'model'):
# Fields are sometimes used without attaching them to models (for
# example in aggregation). In this case give back a plain field
# instance. The code below will create a new empty instance of
# class self.__class__, then update its dict with self.__dict__
# values - so, this is very close to normal pickle.
state = self.__dict__.copy()
# The _get_default cached_property can't be pickled due to lambda
# usage.
state.pop('_get_default', None)
return _empty, (self.__class__,), state
return _load_field, (self.model._meta.app_label, self.model._meta.object_name,
self.name)
示例3: _get_model_property_names
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def _get_model_property_names(instance) -> List[str]:
"""
Gather up properties and cached_properties which may be methods
that were decorated. Need to inspect class versions b/c doing
getattr on them could cause unwanted side effects.
"""
property_names = []
for name in dir(instance):
try:
attr = getattr(type(instance), name)
if isinstance(attr, property) or isinstance(attr, cached_property):
property_names.append(name)
except AttributeError:
pass
return property_names
示例4: test_config
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def test_config(self):
self.assertTrue(issubclass(self.ext_class, Extension))
self.assertEqual(self.ext_class.key, self.ext_class_key)
self.assertEqual(self.ext_class.name, self.ext_class_name)
# Test some basic properties (just to be sure)
self.assertIsInstance(self.ext_class.oid, ObjectIdentifier)
self.assertIsInstance(self.ext_class.key, str)
self.assertGreater(len(self.ext_class.key), 0)
self.assertIsInstance(self.ext_class.name, str)
self.assertGreater(len(self.ext_class.name), 0)
# Test mapping dicts
self.assertEqual(KEY_TO_EXTENSION[self.ext_class.key], self.ext_class)
self.assertEqual(OID_TO_EXTENSION[self.ext_class.oid], self.ext_class)
# test that the model matches
self.assertTrue(hasattr(X509CertMixin, self.ext_class.key))
self.assertIsInstance(getattr(X509CertMixin, self.ext_class.key), cached_property)
示例5: test_cached_property
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def test_cached_property(self):
"""cached_property caches its value and behaves like a property."""
class Class:
@cached_property
def value(self):
"""Here is the docstring..."""
return 1, object()
@cached_property
def __foo__(self):
"""Here is the docstring..."""
return 1, object()
def other_value(self):
"""Here is the docstring..."""
return 1, object()
other = cached_property(other_value, name='other')
attrs = ['value', 'other', '__foo__']
for attr in attrs:
self.assertCachedPropertyWorks(attr, Class)
示例6: test_cached_property_reuse_different_names
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def test_cached_property_reuse_different_names(self):
"""Disallow this case because the decorated function wouldn't be cached."""
with self.assertRaises(RuntimeError) as ctx:
class ReusedCachedProperty:
@cached_property
def a(self):
pass
b = a
self.assertEqual(
str(ctx.exception.__context__),
str(TypeError(
"Cannot assign the same cached_property to two different "
"names ('a' and 'b')."
))
)
示例7: _populate_directed_relation_graph
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def _populate_directed_relation_graph(self):
"""
This method is used by each model to find its reverse objects. As this
method is very expensive and is accessed frequently (it looks up every
field in a model, in every app), it is computed on first access and then
is set as a property on every model.
"""
related_objects_graph = defaultdict(list)
all_models = self.apps.get_models(include_auto_created=True)
for model in all_models:
# Abstract model's fields are copied to child models, hence we will
# see the fields from the child models.
if model._meta.abstract:
continue
fields_with_relations = (
f for f in model._meta._get_fields(reverse=False, include_parents=False)
if f.is_relation and f.related_model is not None
)
for f in fields_with_relations:
if not isinstance(f.rel.to, six.string_types):
related_objects_graph[f.rel.to._meta].append(f)
for model in all_models:
# Set the relation_tree using the internal __dict__. In this way
# we avoid calling the cached property. In attribute lookup,
# __dict__ takes precedence over a data descriptor (such as
# @cached_property). This means that the _meta._relation_tree is
# only called if related_objects is not in __dict__.
related_objects = related_objects_graph[model._meta]
model._meta.__dict__['_relation_tree'] = related_objects
# It seems it is possible that self is not in all_models, so guard
# against that with default for get().
return self.__dict__.get('_relation_tree', EMPTY_RELATION_TREE)
示例8: _populate_directed_relation_graph
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def _populate_directed_relation_graph(self):
"""
This method is used by each model to find its reverse objects. As this
method is very expensive and is accessed frequently (it looks up every
field in a model, in every app), it is computed on first access and then
is set as a property on every model.
"""
related_objects_graph = defaultdict(list)
all_models = self.apps.get_models(include_auto_created=True)
for model in all_models:
opts = model._meta
# Abstract model's fields are copied to child models, hence we will
# see the fields from the child models.
if opts.abstract:
continue
fields_with_relations = (
f for f in opts._get_fields(reverse=False, include_parents=False)
if f.is_relation and f.related_model is not None
)
for f in fields_with_relations:
if not isinstance(f.remote_field.model, str):
related_objects_graph[f.remote_field.model._meta.concrete_model._meta].append(f)
for model in all_models:
# Set the relation_tree using the internal __dict__. In this way
# we avoid calling the cached property. In attribute lookup,
# __dict__ takes precedence over a data descriptor (such as
# @cached_property). This means that the _meta._relation_tree is
# only called if related_objects is not in __dict__.
related_objects = related_objects_graph[model._meta.concrete_model._meta]
model._meta.__dict__['_relation_tree'] = related_objects
# It seems it is possible that self is not in all_models, so guard
# against that with default for get().
return self.__dict__.get('_relation_tree', EMPTY_RELATION_TREE)
示例9: access_token
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def access_token(self):
"""
Returns the access token for this service.
We don't use a cached_property decorator because we aren't sure how to
set custom expiration dates for those.
Returns:
str: JWT access token
"""
key = 'oauth2_access_token'
access_token = cache.get(key)
if not access_token:
url = '{root}/access_token'.format(root=self.oauth2_provider_url)
access_token, expiration_datetime = EdxRestApiClient.get_oauth_access_token(
url,
self.oauth2_client_id,
self.oauth2_client_secret,
token_type='jwt'
)
expires = (expiration_datetime - datetime.datetime.utcnow()).seconds
cache.set(key, access_token, expires)
return access_token
示例10: _populate_directed_relation_graph
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def _populate_directed_relation_graph(self):
"""
This method is used by each model to find its reverse objects. As this
method is very expensive and is accessed frequently (it looks up every
field in a model, in every app), it is computed on first access and then
is set as a property on every model.
"""
related_objects_graph = defaultdict(list)
all_models = self.apps.get_models(include_auto_created=True)
for model in all_models:
opts = model._meta
# Abstract model's fields are copied to child models, hence we will
# see the fields from the child models.
if opts.abstract:
continue
fields_with_relations = (
f for f in opts._get_fields(reverse=False, include_parents=False)
if f.is_relation and f.related_model is not None
)
for f in fields_with_relations:
if not isinstance(f.remote_field.model, six.string_types):
related_objects_graph[f.remote_field.model._meta.concrete_model._meta].append(f)
for model in all_models:
# Set the relation_tree using the internal __dict__. In this way
# we avoid calling the cached property. In attribute lookup,
# __dict__ takes precedence over a data descriptor (such as
# @cached_property). This means that the _meta._relation_tree is
# only called if related_objects is not in __dict__.
related_objects = related_objects_graph[model._meta.concrete_model._meta]
model._meta.__dict__['_relation_tree'] = related_objects
# It seems it is possible that self is not in all_models, so guard
# against that with default for get().
return self.__dict__.get('_relation_tree', EMPTY_RELATION_TREE)
示例11: _populate_directed_relation_graph
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def _populate_directed_relation_graph(self):
"""
This method is used by each model to find its reverse objects. As this
method is very expensive and is accessed frequently (it looks up every
field in a model, in every app), it is computed on first access and then
is set as a property on every model.
"""
related_objects_graph = defaultdict(list)
all_models = self.apps.get_models(include_auto_created=True)
for model in all_models:
# Abstract model's fields are copied to child models, hence we will
# see the fields from the child models.
if model._meta.abstract:
continue
fields_with_relations = (
f for f in model._meta._get_fields(reverse=False, include_parents=False)
if f.is_relation and f.related_model is not None
)
for f in fields_with_relations:
if not isinstance(f.remote_field.model, six.string_types):
related_objects_graph[f.remote_field.model._meta].append(f)
for model in all_models:
# Set the relation_tree using the internal __dict__. In this way
# we avoid calling the cached property. In attribute lookup,
# __dict__ takes precedence over a data descriptor (such as
# @cached_property). This means that the _meta._relation_tree is
# only called if related_objects is not in __dict__.
related_objects = related_objects_graph[model._meta]
model._meta.__dict__['_relation_tree'] = related_objects
# It seems it is possible that self is not in all_models, so guard
# against that with default for get().
return self.__dict__.get('_relation_tree', EMPTY_RELATION_TREE)
示例12: test_cached_property
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def test_cached_property(self):
"""
cached_property caches its value and that it behaves like a property
"""
class A:
@cached_property
def value(self):
"""Here is the docstring..."""
return 1, object()
def other_value(self):
return 1
other = cached_property(other_value, name='other')
# docstring should be preserved
self.assertEqual(A.value.__doc__, "Here is the docstring...")
a = A()
# check that it is cached
self.assertEqual(a.value, a.value)
# check that it returns the right thing
self.assertEqual(a.value[0], 1)
# check that state isn't shared between instances
a2 = A()
self.assertNotEqual(a.value, a2.value)
# check that it behaves like a property when there's no instance
self.assertIsInstance(A.value, cached_property)
# check that overriding name works
self.assertEqual(a.other, 1)
self.assertTrue(callable(a.other_value))
示例13: assertCachedPropertyWorks
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def assertCachedPropertyWorks(self, attr, Class):
with self.subTest(attr=attr):
def get(source):
return getattr(source, attr)
obj = Class()
class SubClass(Class):
pass
subobj = SubClass()
# Docstring is preserved.
self.assertEqual(get(Class).__doc__, 'Here is the docstring...')
self.assertEqual(get(SubClass).__doc__, 'Here is the docstring...')
# It's cached.
self.assertEqual(get(obj), get(obj))
self.assertEqual(get(subobj), get(subobj))
# The correct value is returned.
self.assertEqual(get(obj)[0], 1)
self.assertEqual(get(subobj)[0], 1)
# State isn't shared between instances.
obj2 = Class()
subobj2 = SubClass()
self.assertNotEqual(get(obj), get(obj2))
self.assertNotEqual(get(subobj), get(subobj2))
# It behaves like a property when there's no instance.
self.assertIsInstance(get(Class), cached_property)
self.assertIsInstance(get(SubClass), cached_property)
# 'other_value' doesn't become a property.
self.assertTrue(callable(obj.other_value))
self.assertTrue(callable(subobj.other_value))
示例14: test_cached_property_auto_name
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def test_cached_property_auto_name(self):
"""
cached_property caches its value and behaves like a property
on mangled methods or when the name kwarg isn't set.
"""
class Class:
@cached_property
def __value(self):
"""Here is the docstring..."""
return 1, object()
def other_value(self):
"""Here is the docstring..."""
return 1, object()
other = cached_property(other_value)
other2 = cached_property(other_value, name='different_name')
attrs = ['_Class__value', 'other']
for attr in attrs:
self.assertCachedPropertyWorks(attr, Class)
# An explicit name is ignored.
obj = Class()
obj.other2
self.assertFalse(hasattr(obj, 'different_name'))
示例15: test_cached_property_set_name_not_called
# 需要导入模块: from django.utils import functional [as 别名]
# 或者: from django.utils.functional import cached_property [as 别名]
def test_cached_property_set_name_not_called(self):
cp = cached_property(lambda s: None)
class Foo:
pass
Foo.cp = cp
msg = 'Cannot use cached_property instance without calling __set_name__() on it.'
with self.assertRaisesMessage(TypeError, msg):
Foo().cp