Remove caching and safety utilities (#5653)

* Remove caching

* Remove safety

* Remove unused private usage
This commit is contained in:
Kreusada 2022-04-03 03:31:53 +01:00 committed by GitHub
parent 2995a457f6
commit 0b8dec77c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 0 additions and 103 deletions

View File

@ -2,7 +2,6 @@ import discord as _discord
from .. import __version__, version_info, VersionInfo from .. import __version__, version_info, VersionInfo
from .config import Config from .config import Config
from .utils.safety import warn_unsafe as _warn_unsafe
__all__ = ["Config", "__version__", "version_info", "VersionInfo"] __all__ = ["Config", "__version__", "version_info", "VersionInfo"]

View File

@ -1,53 +0,0 @@
import collections
class LRUDict:
"""
dict with LRU-eviction and max-size
This is intended for caching, it may not behave how you want otherwise
This uses collections.OrderedDict under the hood, but does not directly expose
all of it's methods (intentional)
"""
def __init__(self, *keyval_pairs, size):
self.size = size
self._dict = collections.OrderedDict(*keyval_pairs)
def __contains__(self, key):
if key in self._dict:
self._dict.move_to_end(key, last=True)
return True
return False
def __getitem__(self, key):
ret = self._dict.__getitem__(key)
self._dict.move_to_end(key, last=True)
return ret
def __setitem__(self, key, value):
if key in self._dict:
self._dict.move_to_end(key, last=True)
self._dict[key] = value
if len(self._dict) > self.size:
self._dict.popitem(last=False)
def __delitem__(self, key):
return self._dict.__delitem__(key)
def clear(self):
return self._dict.clear()
def pop(self, key):
return self._dict.pop(key)
# all of the below access all of the items, and therefore shouldn't modify the ordering for eviction
def keys(self):
return self._dict.keys()
def items(self):
return self._dict.items()
def values(self):
return self._dict.values()

View File

@ -1,49 +0,0 @@
import warnings
import functools
def unsafe(f, message=None):
"""
Decorator form for marking a function as unsafe.
This form may not get used much, but there are a few cases
we may want to add something unsafe generally, but safe in specific uses.
The warning can be suppressed in the safe context with warnings.catch_warnings
This should be used sparingly at most.
"""
def wrapper(func):
@functools.wraps(func)
def get_wrapped(*args, **kwargs):
actual_message = message or f"{func.__name__} is unsafe for use"
warnings.warn(actual_message, stacklevel=3, category=RuntimeWarning)
return func(*args, **kwargs)
return get_wrapped
return wrapper
def warn_unsafe(f, message=None):
"""
Function to mark function from dependencies as unsafe for use.
Warning: There is no check that a function has already been modified.
This form should only be used in init, if you want to mark an internal function
as unsafe, use the decorator form above.
The warning can be suppressed in safe contexts with warnings.catch_warnings
This should be used sparingly at most.
"""
def wrapper(func):
@functools.wraps(func)
def get_wrapped(*args, **kwargs):
actual_message = message or f"{func.__name__} is unsafe for use"
warnings.warn(actual_message, stacklevel=3, category=RuntimeWarning)
return func(*args, **kwargs)
return get_wrapped
return wrapper(f)