-
-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move some generic util functions from vacuumcontainers to utils module
- Loading branch information
Showing
2 changed files
with
101 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import inspect | ||
import functools | ||
import warnings | ||
from datetime import timedelta, datetime | ||
|
||
|
||
def deprecated(reason): | ||
""" | ||
This is a decorator which can be used to mark functions and classes | ||
as deprecated. It will result in a warning being emitted | ||
when the function is used. | ||
From https://stackoverflow.com/a/40301488 | ||
""" | ||
|
||
string_types = (type(b''), type(u'')) | ||
if isinstance(reason, string_types): | ||
|
||
# The @deprecated is used with a 'reason'. | ||
# | ||
# .. code-block:: python | ||
# | ||
# @deprecated("please, use another function") | ||
# def old_function(x, y): | ||
# pass | ||
|
||
def decorator(func1): | ||
|
||
if inspect.isclass(func1): | ||
fmt1 = "Call to deprecated class {name} ({reason})." | ||
else: | ||
fmt1 = "Call to deprecated function {name} ({reason})." | ||
|
||
@functools.wraps(func1) | ||
def new_func1(*args, **kwargs): | ||
warnings.simplefilter('always', DeprecationWarning) | ||
warnings.warn( | ||
fmt1.format(name=func1.__name__, reason=reason), | ||
category=DeprecationWarning, | ||
stacklevel=2 | ||
) | ||
warnings.simplefilter('default', DeprecationWarning) | ||
return func1(*args, **kwargs) | ||
|
||
return new_func1 | ||
|
||
return decorator | ||
|
||
elif inspect.isclass(reason) or inspect.isfunction(reason): | ||
|
||
# The @deprecated is used without any 'reason'. | ||
# | ||
# .. code-block:: python | ||
# | ||
# @deprecated | ||
# def old_function(x, y): | ||
# pass | ||
|
||
func2 = reason | ||
|
||
if inspect.isclass(func2): | ||
fmt2 = "Call to deprecated class {name}." | ||
else: | ||
fmt2 = "Call to deprecated function {name}." | ||
|
||
@functools.wraps(func2) | ||
def new_func2(*args, **kwargs): | ||
warnings.simplefilter('always', DeprecationWarning) | ||
warnings.warn( | ||
fmt2.format(name=func2.__name__), | ||
category=DeprecationWarning, | ||
stacklevel=2 | ||
) | ||
warnings.simplefilter('default', DeprecationWarning) | ||
return func2(*args, **kwargs) | ||
|
||
return new_func2 | ||
|
||
else: | ||
raise TypeError(repr(type(reason))) | ||
|
||
|
||
def pretty_seconds(x: float) -> timedelta: | ||
"""Return a timedelta object from seconds.""" | ||
return timedelta(seconds=x) | ||
|
||
|
||
def pretty_time(x: float) -> datetime: | ||
"""Return a datetime object from unix timestamp.""" | ||
return datetime.fromtimestamp(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters