Skip to content

Commit

Permalink
move decorators out of utilities and into decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
ddale committed Apr 8, 2010
1 parent 47cb41f commit d1b7ae9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 52 deletions.
51 changes: 49 additions & 2 deletions quantities/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,54 @@
import string
import sys
import warnings
from functools import partial
from functools import partial, wraps

from .quantity import Quantity

def memoize(f, cache={}):
@wraps(f)
def g(*args, **kwargs):
key = (f, tuple(args), frozenset(kwargs.items()))
if key not in cache:
cache[key] = f(*args, **kwargs)
return cache[key].copy()
return g


class with_doc:

"""
This decorator combines the docstrings of the provided and decorated objects
to produce the final docstring for the decorated object.
"""

def __init__(self, method, use_header=True):
self.method = method
if use_header:
self.header = \
"""
Notes
-----
"""
else:
self.header = ''

def __call__(self, new_method):
new_doc = new_method.__doc__
original_doc = self.method.__doc__
header = self.header

if original_doc and new_doc:
new_method.__doc__ = """
%s
%s
%s
""" % (original_doc, header, new_doc)

elif original_doc:
new_method.__doc__ = original_doc

return new_method

def quantitizer(base_function,
handler_function = lambda *args, **kwargs: 1.0):
Expand All @@ -28,6 +72,9 @@ def quantitizer(base_function,
and works with physical quantities. It will have almost the same
__name__ and almost the same __doc__.
"""

from .quantity import Quantity

# define a function which will wrap the base function so that it works
# with Quantities
def wrapped_function(*args , **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion quantities/dimensionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from . import markup
from .registry import unit_registry
from .utilities import memoize
from .decorators import memoize

def assert_isinstance(obj, types):
try:
Expand Down
2 changes: 1 addition & 1 deletion quantities/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from . import markup
from .dimensionality import Dimensionality, p_dict
from .registry import unit_registry
from .utilities import with_doc
from .decorators import with_doc


def validate_unit_quantity(value):
Expand Down
2 changes: 1 addition & 1 deletion quantities/umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .quantity import Quantity
from .units import dimensionless, radian, degree
from .utilities import with_doc
from .decorators import with_doc


#__all__ = [
Expand Down
2 changes: 1 addition & 1 deletion quantities/uncertainquantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from . import markup
from .quantity import Quantity, scale_other_units
from .registry import unit_registry
from .utilities import with_doc
from .decorators import with_doc

class UncertainQuantity(Quantity):

Expand Down
2 changes: 1 addition & 1 deletion quantities/unitquantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from . import markup
from .quantity import Quantity, get_conversion_factor
from .registry import unit_registry
from .utilities import memoize, with_doc
from .decorators import memoize, with_doc


__all__ = [
Expand Down
45 changes: 0 additions & 45 deletions quantities/utilities.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
from functools import wraps


def memoize(f, cache={}):
@wraps(f)
def g(*args, **kwargs):
key = (f, tuple(args), frozenset(kwargs.items()))
if key not in cache:
cache[key] = f(*args, **kwargs)
return cache[key].copy()
return g


class with_doc:

"""
This decorator combines the docstrings of the provided and decorated objects
to produce the final docstring for the decorated object.
"""

def __init__(self, method, use_header=True):
self.method = method
if use_header:
self.header = \
"""
Notes
-----
"""
else:
self.header = ''

def __call__(self, new_method):
new_doc = new_method.__doc__
original_doc = self.method.__doc__
header = self.header

if original_doc and new_doc:
new_method.__doc__ = """
%s
%s
%s
""" % (original_doc, header, new_doc)

elif original_doc:
new_method.__doc__ = original_doc

return new_method

0 comments on commit d1b7ae9

Please sign in to comment.