Skip to content

Commit

Permalink
Blue fixes (mostly docstring triple quotes)
Browse files Browse the repository at this point in the history
  • Loading branch information
grantjenks committed Sep 14, 2021
1 parent a2e461a commit ab1484d
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 92 deletions.
1 change: 0 additions & 1 deletion diskcache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
=======================
The :doc:`tutorial` provides a helpful walkthrough of most methods.
"""

from .core import (
Expand Down
2 changes: 1 addition & 1 deletion diskcache/cli.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"Command line interface to disk cache."
"""Command line interface to disk cache."""
29 changes: 14 additions & 15 deletions diskcache/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Core disk and file backed cache API.
"""

import codecs
Expand All @@ -22,12 +21,12 @@


def full_name(func):
"Return full name of `func` by adding the module and function name."
"""Return full name of `func` by adding the module and function name."""
return func.__module__ + '.' + func.__qualname__


class Constant(tuple):
"Pretty display of immutable constant."
"""Pretty display of immutable constant."""

def __new__(cls, name):
return tuple.__new__(cls, (name,))
Expand Down Expand Up @@ -102,7 +101,7 @@ def __repr__(self):


class Disk:
"Cache key and value serialization for SQLite database and files."
"""Cache key and value serialization for SQLite database and files."""

def __init__(self, directory, min_file_size=0, pickle_protocol=0):
"""Initialize disk instance.
Expand Down Expand Up @@ -333,7 +332,7 @@ def remove(self, file_path):


class JSONDisk(Disk):
"Cache key and value using JSON serialization with zlib compression."
"""Cache key and value using JSON serialization with zlib compression."""

def __init__(self, directory, compress_level=1, **kwargs):
"""Initialize JSON disk instance.
Expand Down Expand Up @@ -374,15 +373,15 @@ def fetch(self, mode, filename, value, read):


class Timeout(Exception):
"Database timeout expired."
"""Database timeout expired."""


class UnknownFileWarning(UserWarning):
"Warning used by Cache.check for unknown files."
"""Warning used by Cache.check for unknown files."""


class EmptyDirWarning(UserWarning):
"Warning used by Cache.check for empty directories."
"""Warning used by Cache.check for empty directories."""


def args_to_key(base, args, kwargs, typed):
Expand Down Expand Up @@ -414,7 +413,7 @@ def args_to_key(base, args, kwargs, typed):


class Cache:
"Disk and file backed cache."
"""Disk and file backed cache."""

def __init__(self, directory=None, timeout=60, disk=Disk, **settings):
"""Initialize cache instance.
Expand Down Expand Up @@ -1859,12 +1858,12 @@ def memoize(self, name=None, typed=False, expire=None, tag=None):
raise TypeError('name cannot be callable')

def decorator(func):
"Decorator created by memoize() for callable `func`."
"""Decorator created by memoize() for callable `func`."""
base = (full_name(func),) if name is None else (name,)

@ft.wraps(func)
def wrapper(*args, **kwargs):
"Wrapper for callable to cache arguments and return values."
"""Wrapper for callable to cache arguments and return values."""
key = wrapper.__cache_key__(*args, **kwargs)
result = self.get(key, default=ENOVAL, retry=True)

Expand All @@ -1876,7 +1875,7 @@ def wrapper(*args, **kwargs):
return result

def __cache_key__(*args, **kwargs):
"Make key for cache given function arguments."
"""Make key for cache given function arguments."""
return args_to_key(base, args, kwargs, typed)

wrapper.__cache_key__ = __cache_key__
Expand Down Expand Up @@ -2291,13 +2290,13 @@ def _iter(self, ascending=True):
yield _disk_get(key, raw)

def __iter__(self):
"Iterate keys in cache including expired items."
"""Iterate keys in cache including expired items."""
iterator = self._iter()
next(iterator)
return iterator

def __reversed__(self):
"Reverse iterate keys in cache including expired items."
"""Reverse iterate keys in cache including expired items."""
iterator = self._iter(ascending=False)
next(iterator)
return iterator
Expand Down Expand Up @@ -2355,7 +2354,7 @@ def __exit__(self, *exception):
self.close()

def __len__(self):
"Count of items in cache including expired items."
"""Count of items in cache including expired items."""
return self.reset('count')

def __getstate__(self):
Expand Down
14 changes: 7 additions & 7 deletions diskcache/djangocache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Django-compatible disk and file backed cache."
"""Django-compatible disk and file backed cache."""

from functools import wraps

Expand All @@ -15,7 +15,7 @@


class DjangoCache(BaseCache):
"Django-compatible disk and file backed cache."
"""Django-compatible disk and file backed cache."""

def __init__(self, directory, params):
"""Initialize DjangoCache instance.
Expand Down Expand Up @@ -344,11 +344,11 @@ def cull(self):
return self._cache.cull()

def clear(self):
"Remove *all* values from the cache at once."
"""Remove *all* values from the cache at once."""
return self._cache.clear()

def close(self, **kwargs):
"Close the cache connection."
"""Close the cache connection."""
# pylint: disable=unused-argument
self._cache.close()

Expand Down Expand Up @@ -415,12 +415,12 @@ def memoize(
raise TypeError('name cannot be callable')

def decorator(func):
"Decorator created by memoize() for callable `func`."
"""Decorator created by memoize() for callable `func`."""
base = (full_name(func),) if name is None else (name,)

@wraps(func)
def wrapper(*args, **kwargs):
"Wrapper for callable to cache arguments and return values."
"""Wrapper for callable to cache arguments and return values."""
key = wrapper.__cache_key__(*args, **kwargs)
result = self.get(key, ENOVAL, version, retry=True)

Expand All @@ -444,7 +444,7 @@ def wrapper(*args, **kwargs):
return result

def __cache_key__(*args, **kwargs):
"Make key for cache given function arguments."
"""Make key for cache given function arguments."""
return args_to_key(base, args, kwargs, typed)

wrapper.__cache_key__ = __cache_key__
Expand Down
12 changes: 6 additions & 6 deletions diskcache/fanout.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"Fanout cache automatically shards keys and values."
"""Fanout cache automatically shards keys and values."""

import contextlib as cl
import functools
Expand All @@ -14,7 +14,7 @@


class FanoutCache:
"Cache that shards keys and values."
"""Cache that shards keys and values."""

def __init__(
self, directory=None, shards=8, timeout=0.010, disk=Disk, **settings
Expand Down Expand Up @@ -512,7 +512,7 @@ def volume(self):
return sum(shard.volume() for shard in self._shards)

def close(self):
"Close database connection."
"""Close database connection."""
for shard in self._shards:
shard.close()
self._caches.clear()
Expand All @@ -532,17 +532,17 @@ def __setstate__(self, state):
self.__init__(*state)

def __iter__(self):
"Iterate keys in cache including expired items."
"""Iterate keys in cache including expired items."""
iterators = (iter(shard) for shard in self._shards)
return it.chain.from_iterable(iterators)

def __reversed__(self):
"Reverse iterate keys in cache including expired items."
"""Reverse iterate keys in cache including expired items."""
iterators = (reversed(shard) for shard in reversed(self._shards))
return it.chain.from_iterable(iterators)

def __len__(self):
"Count of items in cache including expired items."
"""Count of items in cache including expired items."""
return sum(len(shard) for shard in self._shards)

def reset(self, key, value=ENOVAL):
Expand Down
13 changes: 6 additions & 7 deletions diskcache/persistent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Persistent Data Types
"""

import operator as op
Expand All @@ -18,10 +17,10 @@


def _make_compare(seq_op, doc):
"Make compare method with Sequence semantics."
"""Make compare method with Sequence semantics."""

def compare(self, that):
"Compare method for deque and sequence."
"""Compare method for deque and sequence."""
if not isinstance(that, Sequence):
return NotImplemented

Expand Down Expand Up @@ -117,12 +116,12 @@ def fromcache(cls, cache, iterable=()):

@property
def cache(self):
"Cache used by deque."
"""Cache used by deque."""
return self._cache

@property
def directory(self):
"Directory path where deque is stored."
"""Directory path where deque is stored."""
return self._cache.directory

def _index(self, index, func):
Expand Down Expand Up @@ -699,12 +698,12 @@ def fromcache(cls, cache, *args, **kwargs):

@property
def cache(self):
"Cache used by index."
"""Cache used by index."""
return self._cache

@property
def directory(self):
"Directory path where items are stored."
"""Directory path where items are stored."""
return self._cache.directory

def __getitem__(self, key):
Expand Down
29 changes: 14 additions & 15 deletions diskcache/recipes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Disk Cache Recipes
"""

import functools
Expand Down Expand Up @@ -40,7 +39,7 @@ def __init__(self, cache, key, expire=None, tag=None):
self._tag = tag

def add(self, value):
"Add `value` to average."
"""Add `value` to average."""
with self._cache.transact(retry=True):
total, count = self._cache.get(self._key, default=(0.0, 0))
total += value
Expand All @@ -53,12 +52,12 @@ def add(self, value):
)

def get(self):
"Get current average or return `None` if count equals zero."
"""Get current average or return `None` if count equals zero."""
total, count = self._cache.get(self._key, default=(0.0, 0), retry=True)
return None if count == 0 else total / count

def pop(self):
"Return current average and delete key."
"""Return current average and delete key."""
total, count = self._cache.pop(self._key, default=(0.0, 0), retry=True)
return None if count == 0 else total / count

Expand All @@ -83,7 +82,7 @@ def __init__(self, cache, key, expire=None, tag=None):
self._tag = tag

def acquire(self):
"Acquire lock using spin-lock algorithm."
"""Acquire lock using spin-lock algorithm."""
while True:
added = self._cache.add(
self._key,
Expand All @@ -97,11 +96,11 @@ def acquire(self):
time.sleep(0.001)

def release(self):
"Release lock by deleting key."
"""Release lock by deleting key."""
self._cache.delete(self._key, retry=True)

def locked(self):
"Return true if the lock is acquired."
"""Return true if the lock is acquired."""
return self._key in self._cache

def __enter__(self):
Expand Down Expand Up @@ -137,7 +136,7 @@ def __init__(self, cache, key, expire=None, tag=None):
self._tag = tag

def acquire(self):
"Acquire lock by incrementing count using spin-lock algorithm."
"""Acquire lock by incrementing count using spin-lock algorithm."""
pid = os.getpid()
tid = threading.get_ident()
pid_tid = '{}-{}'.format(pid, tid)
Expand All @@ -156,7 +155,7 @@ def acquire(self):
time.sleep(0.001)

def release(self):
"Release lock by decrementing count."
"""Release lock by decrementing count."""
pid = os.getpid()
tid = threading.get_ident()
pid_tid = '{}-{}'.format(pid, tid)
Expand Down Expand Up @@ -206,7 +205,7 @@ def __init__(self, cache, key, value=1, expire=None, tag=None):
self._tag = tag

def acquire(self):
"Acquire semaphore by decrementing value using spin-lock algorithm."
"""Acquire semaphore by decrementing value using spin-lock algorithm."""
while True:
with self._cache.transact(retry=True):
value = self._cache.get(self._key, default=self._value)
Expand All @@ -221,7 +220,7 @@ def acquire(self):
time.sleep(0.001)

def release(self):
"Release semaphore by incrementing value."
"""Release semaphore by incrementing value."""
with self._cache.transact(retry=True):
value = self._cache.get(self._key, default=self._value)
assert self._value > value, 'cannot release un-acquired semaphore'
Expand Down Expand Up @@ -396,19 +395,19 @@ def memoize_stampede(cache, expire, name=None, typed=False, tag=None, beta=1):
"""
# Caution: Nearly identical code exists in Cache.memoize
def decorator(func):
"Decorator created by memoize call for callable."
"""Decorator created by memoize call for callable."""
base = (full_name(func),) if name is None else (name,)

def timer(*args, **kwargs):
"Time execution of `func` and return result and time delta."
"""Time execution of `func` and return result and time delta."""
start = time.time()
result = func(*args, **kwargs)
delta = time.time() - start
return result, delta

@functools.wraps(func)
def wrapper(*args, **kwargs):
"Wrapper for callable to cache arguments and return values."
"""Wrapper for callable to cache arguments and return values."""
key = wrapper.__cache_key__(*args, **kwargs)
pair, expire_time = cache.get(
key,
Expand Down Expand Up @@ -459,7 +458,7 @@ def recompute():
return pair[0]

def __cache_key__(*args, **kwargs):
"Make key for cache given function arguments."
"""Make key for cache given function arguments."""
return args_to_key(base, args, kwargs, typed)

wrapper.__cache_key__ = __cache_key__
Expand Down
Loading

0 comments on commit ab1484d

Please sign in to comment.