Skip to content

Commit

Permalink
Merge pull request #35 from francof2a/dev-0.4.2
Browse files Browse the repository at this point in the history
Config template
  • Loading branch information
francof2a committed Jul 21, 2021
2 parents a8a46d7 + 6f781dd commit 6ab7322
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion fxpmath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.4.1'
__version__ = '0.4.2'

import sys
import os
Expand Down
25 changes: 22 additions & 3 deletions fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def __init__(self, val=None, signed=None, n_word=None, n_frac=None, n_int=None,
_initialized = False
# ---

# init config
self.config = Config()

# if `template` is in kwarg, the reference template is updated
if 'template' in kwargs: self.template = kwargs.pop('template')

Expand Down Expand Up @@ -146,11 +149,11 @@ def __init__(self, val=None, signed=None, n_word=None, n_frac=None, n_int=None,
'inaccuracy': False,
'extended_prec': False}

# update config from kwargs
self.config.update(**kwargs)

# callbacks
if self.callbacks is None: self.callbacks = kwargs.pop('callbacks', [])

# config
self.config = Config(**kwargs)

# scaling
if self.scale is None: self.scale = kwargs.pop('scale', 1)
Expand Down Expand Up @@ -1596,6 +1599,8 @@ def dot(self, x, **kwargs):
return dot(self, x, out=out, out_like=out_like, sizing=sizing, method=method, **kwargs)

class Config():
template = None

def __init__(self, **kwargs):
# size limits
self.max_error = kwargs.pop('max_error', 1 / 2**63)
Expand Down Expand Up @@ -1627,6 +1632,14 @@ def __init__(self, **kwargs):
# notation
self.dtype_notation = kwargs.pop('dtype_notation', 'fxp')

# update from template
# if `template` is in kwarg, the reference template is updated
if 'template' in kwargs: self.template = kwargs.pop('template')

if self.template is not None:
if isinstance(self.template, Config):
self.__dict__ = copy.deepcopy(self.template.__dict__)

# ---
# properties
# ---
Expand Down Expand Up @@ -1876,6 +1889,12 @@ def print(self):
for k, v in self.__dict__.items():
print('\t{: <24}:\t{}'.format(k.strip('_'), v))

def update(self, **kwargs):
for k, v in kwargs.items():
if hasattr(self, k):
setattr(self, k, v)


# endregion

# ----------------------------------------------------------------------------------------
Expand Down

0 comments on commit 6ab7322

Please sign in to comment.