Skip to content

Commit 1535c20

Browse files
scriptatorSebastian Böck
authored andcommitted
Move lazyprop decorator to the utils module
1 parent 0959182 commit 1535c20

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

madmom/audio/cepstrogram.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .filters import MelFilterbank
2020
from .spectrogram import Spectrogram
2121
from ..processors import Processor
22+
from ..utils import lazyprop
2223

2324

2425
class Cepstrogram(np.ndarray):
@@ -37,7 +38,6 @@ class applies some transformation (usually a DCT) on a spectrogram.
3738
one is instantiated with these additional keyword arguments.
3839
3940
"""
40-
4141
# pylint: disable=super-on-old-class
4242
# pylint: disable=super-init-not-called
4343
# pylint: disable=attribute-defined-outside-init
@@ -130,19 +130,6 @@ def process(self, data, **kwargs):
130130
MFCC_DELTADELTA_FILTER = np.linspace(1, -1, 3) / 2
131131

132132

133-
# https://stackoverflow.com/questions/3012421/python-memoising-deferred-lookup-property-decorator#3013910
134-
def lazyprop(fn):
135-
attr_name = '_lazy_' + fn.__name__
136-
137-
@property
138-
def _lazyprop(self):
139-
if not hasattr(self, attr_name):
140-
setattr(self, attr_name, fn(self))
141-
return getattr(self, attr_name)
142-
143-
return _lazyprop
144-
145-
146133
class MFCC(Cepstrogram):
147134
"""
148135
MFCC class.

madmom/utils/__init__.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,35 @@ def segment_axis(signal, frame_size, hop_size, axis=None, end='cut',
591591
dtype=signal.dtype)
592592

593593

594+
# taken from: https://stackoverflow.com/questions/3012421/
595+
def lazyprop(fn):
596+
"""
597+
A decorator for a caching, lazily evaluated property. If a function is
598+
decorated with @lazyprop, the original function of the resulting property
599+
is only called on the first access. Afterwards the result which was
600+
produced then is returned again.
601+
602+
Parameters
603+
----------
604+
fn: Function
605+
A function without argument which returns the value of the property
606+
607+
Returns
608+
-------
609+
property
610+
A property which wraps the original one and caches it first result
611+
"""
612+
attr_name = '_lazy_' + fn.__name__
613+
614+
@property
615+
def _lazyprop(self):
616+
if not hasattr(self, attr_name):
617+
setattr(self, attr_name, fn(self))
618+
return getattr(self, attr_name)
619+
620+
return _lazyprop
621+
622+
594623
# keep namespace clean
595624
del contextlib
596625

0 commit comments

Comments
 (0)