From 619620c6720435f8848aff01c1e747df934f6ee1 Mon Sep 17 00:00:00 2001 From: jonson Date: Sat, 2 May 2020 14:55:12 -0400 Subject: [PATCH] Initialize thread local value on each thread --- btalib/indicator.py | 2 +- btalib/meta/lines.py | 4 ++-- btalib/meta/metadata.py | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/btalib/indicator.py b/btalib/indicator.py index 7cc6abc..4417ff2 100644 --- a/btalib/indicator.py +++ b/btalib/indicator.py @@ -16,7 +16,7 @@ ] -metadata.callstack = [] # keep indicators which are currently being executed +metadata.register('callstack', list) # keep indicators which are currently being executed _NAMES_IND = {} diff --git a/btalib/meta/lines.py b/btalib/meta/lines.py index 3c501c8..d4f2a04 100644 --- a/btalib/meta/lines.py +++ b/btalib/meta/lines.py @@ -607,8 +607,8 @@ def _applymulti(self, func, *args, raw=False, **kwargs): # instances, to avoid having them declared as attributes. Or else __setattr__ # would set them as Line objects (or logic would be needed in __setattr__ to # avoid assigning an object not the real value -metadata.minperiods = {} -metadata.minperiod = {} +metadata.register('minperiods', dict) +metadata.register('minperiod', dict) class Lines: diff --git a/btalib/meta/metadata.py b/btalib/meta/metadata.py index 4180a6a..0a54fbb 100644 --- a/btalib/meta/metadata.py +++ b/btalib/meta/metadata.py @@ -8,4 +8,21 @@ __all__ = ['metadata'] -metadata = threading.local() + +class Metadata(object): + + def __init__(self): + self._metadata = threading.local() + + @classmethod + def register(cls, name, default=None): + + def get_or_default(self): + if not hasattr(self._metadata, name): + setattr(self._metadata, name, default()) + return getattr(self._metadata, name) + + setattr(cls, name, property(get_or_default)) + + +metadata = Metadata()