本文整理汇总了Python中transaction.TransactionManager.get方法的典型用法代码示例。如果您正苦于以下问题:Python TransactionManager.get方法的具体用法?Python TransactionManager.get怎么用?Python TransactionManager.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transaction.TransactionManager
的用法示例。
在下文中一共展示了TransactionManager.get方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_explicit_mode
# 需要导入模块: from transaction import TransactionManager [as 别名]
# 或者: from transaction.TransactionManager import get [as 别名]
def test_explicit_mode(self):
from .. import TransactionManager
from ..interfaces import AlreadyInTransaction, NoTransaction
tm = TransactionManager()
self.assertFalse(tm.explicit)
tm = TransactionManager(explicit=True)
self.assertTrue(tm.explicit)
for name in 'get', 'commit', 'abort', 'doom', 'isDoomed', 'savepoint':
with self.assertRaises(NoTransaction):
getattr(tm, name)()
t = tm.begin()
with self.assertRaises(AlreadyInTransaction):
tm.begin()
self.assertTrue(t is tm.get())
self.assertFalse(tm.isDoomed())
tm.doom()
self.assertTrue(tm.isDoomed())
tm.abort()
for name in 'get', 'commit', 'abort', 'doom', 'isDoomed', 'savepoint':
with self.assertRaises(NoTransaction):
getattr(tm, name)()
t = tm.begin()
self.assertFalse(tm.isDoomed())
with self.assertRaises(AlreadyInTransaction):
tm.begin()
tm.savepoint()
tm.commit()
示例2: QueryCacheBackend
# 需要导入模块: from transaction import TransactionManager [as 别名]
# 或者: from transaction.TransactionManager import get [as 别名]
class QueryCacheBackend(object):
"""This class is the engine behind the query cache. It reads the queries
going through the django Query and returns from the cache using
the generation keys, or on a miss from the database and caches the results.
Each time a model is updated the table keys for that model are re-created,
invalidating all cached querysets for that model.
There are different QueryCacheBackend's for different versions of django;
call ``johnny.cache.get_backend`` to automatically get the proper class.
"""
__shared_state = {}
def __init__(self, cache_backend=None, keyhandler=None, keygen=None):
self.__dict__ = self.__shared_state
self.prefix = settings.MIDDLEWARE_KEY_PREFIX
if keyhandler:
self.kh_class = keyhandler
if keygen:
self.kg_class = keygen
if not cache_backend and not hasattr(self, 'cache_backend'):
cache_backend = settings._get_backend()
if not keygen and not hasattr(self, 'kg_class'):
self.kg_class = KeyGen
if keyhandler is None and not hasattr(self, 'kh_class'):
self.kh_class = KeyHandler
if cache_backend:
self.cache_backend = TransactionManager(cache_backend,
self.kg_class)
self.keyhandler = self.kh_class(self.cache_backend,
self.kg_class, self.prefix)
self._patched = getattr(self, '_patched', False)
def _monkey_select(self, original):
from django.db.models.sql.constants import MULTI
from django.db.models.sql.datastructures import EmptyResultSet
@wraps(original, assigned=available_attrs(original))
def newfun(cls, *args, **kwargs):
if args:
result_type = args[0]
else:
result_type = kwargs.get('result_type', MULTI)
if any([isinstance(cls, c) for c in self._write_compilers]):
return original(cls, *args, **kwargs)
try:
sql, params = cls.as_sql()
if not sql:
raise EmptyResultSet
except EmptyResultSet:
if result_type == MULTI:
# this was moved in 1.2 to compiler
return empty_iter()
else:
return
db = getattr(cls, 'using', 'default')
key, val = None, NotInCache()
# check the blacklist for any of the involved tables; if it's not
# there, then look for the value in the cache.
tables = get_tables_for_query(cls.query)
# if the tables are blacklisted, send a qc_skip signal
blacklisted = disallowed_table(*tables)
if blacklisted:
signals.qc_skip.send(sender=cls, tables=tables,
query=(sql, params, cls.ordering_aliases),
key=key)
if tables and not blacklisted:
gen_key = self.keyhandler.get_generation(*tables, **{'db': db})
key = self.keyhandler.sql_key(gen_key, sql, params,
cls.get_ordering(),
result_type, db)
val = self.cache_backend.get(key, NotInCache(), db)
if not isinstance(val, NotInCache):
if val == no_result_sentinel:
val = []
signals.qc_hit.send(sender=cls, tables=tables,
query=(sql, params, cls.ordering_aliases),
size=len(val), key=key)
return val
if not blacklisted:
signals.qc_miss.send(sender=cls, tables=tables,
query=(sql, params, cls.ordering_aliases),
key=key)
val = original(cls, *args, **kwargs)
if hasattr(val, '__iter__'):
#Can't permanently cache lazy iterables without creating
#a cacheable data structure. Note that this makes them
#no longer lazy...
#todo - create a smart iterable wrapper
val = list(val)
if key is not None:
if not val:
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from transaction import TransactionManager [as 别名]
# 或者: from transaction.TransactionManager import get [as 别名]
class Context:
"""
Base class for Contexts of a ConfiguredCtxModule. Do not use this class
directly, use the :attr:`Context <.ConfiguredCtxModule.Context>` member of a
ConfiguredCtxModule instead.
Every Context object needs to be destroyed manually by calling its
:meth:`.destroy` method. Although this method will be called in the
destructor of this class, that might already be too late. This is the reason
why the preferred way of using this class is within a `with` statement:
>>> with ctx_conf.Context() as ctx:
... ctx.logout_user()
...
"""
def __init__(self):
if not hasattr(self, '_conf'):
raise Exception('Unconfigured Context')
self.log = self._conf.log
self.log.debug('Initializing')
self._constructed_attrs = OrderedDict()
self._active = True
self.tx_manager = TransactionManager()
for callback in self._conf._create_callbacks:
callback(self)
def __del__(self):
if self._active:
self.destroy()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.destroy(value)
def __hasattr__(self, attr):
if self._active:
return attr in self._conf.registrations
return attr in self.__dict__
def __getattr__(self, attr):
if '_active' in self.__dict__ and '_conf' in self.__dict__ and \
self._active and attr in self._conf.registrations:
value = self._conf.registrations[attr].constructor(self)
if self._conf.registrations[attr].cached:
self._constructed_attrs[attr] = value
self.__dict__[attr] = value
else:
self._constructed_attrs[attr] = None
self.log.debug('Creating member %s' % attr)
return value
raise AttributeError(attr)
def __setattr__(self, attr, value):
try:
# call registered contructor, if there is one, so the destructor
# gets called with this new value
getattr(self, attr)
except AttributeError:
pass
self.__dict__[attr] = value
def __delattr__(self, attr):
if attr in self._conf.registrations:
if attr in self._constructed_attrs:
self.__delattr(attr, None)
elif attr in self.__dict__:
del self.__dict__[attr]
else:
del self.__dict__[attr]
def __delattr(self, attr, exception):
"""
Deletes a previously constructed *attr*. Its destructor will receive the
given *exception*.
Note: this function assumes that the *attr* is indeed a registered
context member. It will behave unexpectedly when called with an *attr*
that has no registration.
"""
constructor_value = self._constructed_attrs[attr]
del self._constructed_attrs[attr]
self.log.debug('Deleting member %s' % attr)
destructor = self._conf.registrations[attr].destructor
if destructor:
self.log.debug('Calling destructor of %s' % attr)
if self._conf.registrations[attr].cached:
destructor(self, constructor_value, None)
else:
destructor(self, None)
try:
del self.__dict__[attr]
except KeyError:
# destructor might have deleted self.attr already
pass
def destroy(self, exception=None):
"""
#.........这里部分代码省略.........