Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_log_durations(monkeypatch):
timestamps = iter([0, 0.01, 1, 1.000025])
monkeypatch.setattr('time.time', lambda: next(timestamps))
log = []
f = log_durations(log.append)(lambda: None)
f()
with log_durations(log.append, 'hello'):
pass
assert lmap(r'^\s*(\d+\.\d+ mk?s) in (?:\(\)|hello)$', log) == ['10.00 ms', '25.00 mks']
def _coerce_filenames(filenames):
if isinstance(filenames, (basestring, pathlib.PurePath)):
filenames = [filenames]
return lmap(fspath, filenames)
def test_log_durations_ex(monkeypatch):
timestamps = [0, 0.01, 1, 1.001, 2, 2.02]
timestamps_iter = iter(timestamps)
monkeypatch.setattr('time.time', lambda: next(timestamps_iter))
log = []
f = log_durations(log.append, unit='ms', threshold=1.1e-3)(lambda: None)
f(); f(); f()
assert len(log) == 2
assert lmap(r'^\s*(\d+\.\d+) ms in', log) == ['10.00', '20.00']
elif len(where) == 0:
return [[]]
else:
chilren_dnfs = lmap(_dnf, where.children)
if len(chilren_dnfs) == 0:
return [[]]
elif len(chilren_dnfs) == 1:
result = chilren_dnfs[0]
else:
# Just unite children joined with OR
if where.connector == OR:
result = lcat(chilren_dnfs)
# Use Cartesian product to AND children
else:
result = lmap(lcat, product(*chilren_dnfs))
# Negating and expanding brackets
if where.negated:
result = [lmap(negate, p) for p in product(*result)]
return result
if len(samples) == 1 and isinstance(samples[0], list):
return lambda func: func
def _get_queryset(sample):
if isinstance(sample, Model):
queryset = sample.__class__.objects.filter(pk=sample.pk)
elif isinstance(sample, type) and issubclass(sample, Model):
queryset = sample.objects.all()
else:
queryset = sample
queryset._require_cacheprofile()
return queryset
querysets = lmap(_get_queryset, samples)
dbs = list({qs.db for qs in querysets})
cond_dnfs = join_with(lcat, map(dnfs, querysets))
key_extra = [qs._cache_key(prefix=False) for qs in querysets]
key_extra.append(extra)
if timeout is None:
timeout = min(qs._cacheprofile['timeout'] for qs in querysets)
if lock is None:
lock = any(qs._cacheprofile['lock'] for qs in querysets)
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
if not settings.CACHEOPS_ENABLED or transaction_states.is_dirty(dbs):
return func(*args, **kwargs)
prefix = get_prefix(func=func, _cond_dnfs=cond_dnfs, dbs=dbs)