-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from gerw/common_base_class
Common base class for Parameters,Calculations,Visibilities
- Loading branch information
Showing
7 changed files
with
219 additions
and
137 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
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,67 @@ | ||
"""Provides a base class for parameters, calculations, visibilities.""" | ||
import logging | ||
|
||
from luxtronik.datatypes import Unknown | ||
|
||
|
||
class DataVector: | ||
"""Class that holds a vector of data entries.""" | ||
|
||
logger = logging.getLogger("Luxtronik.DataVector") | ||
name = "DataVector" | ||
|
||
def __init__(self): | ||
"""Initialize DataVector class.""" | ||
self._data = {} | ||
|
||
def __iter__(self): | ||
"""Iterator for the data entries.""" | ||
return iter(self._data.items()) | ||
|
||
def parse(self, raw_data): | ||
"""Parse raw data.""" | ||
for index, data in enumerate(raw_data): | ||
entry = self._data.get(index, None) | ||
if entry is not None: | ||
entry.raw = data | ||
else: | ||
# self.logger.warning(f"Entry '%d' not in list of {self.name}", index) | ||
entry = Unknown(f"Unknown_{self.name}_{index}") | ||
entry.raw = data | ||
self._data[index] = entry | ||
|
||
def _lookup(self, target, with_index=False): | ||
""" | ||
Lookup an entry | ||
"target" could either be its id or its name. | ||
In case "with_index" is set, also the index is returned. | ||
""" | ||
if isinstance(target, str): | ||
try: | ||
# Try to get entry by id | ||
target_index = int(target) | ||
except ValueError: | ||
# Get entry by name | ||
target_index = None | ||
for index, entry in self._data.items(): | ||
if entry.name == target: | ||
target_index = index | ||
elif isinstance(target, int): | ||
# Get entry by id | ||
target_index = target | ||
else: | ||
target_index = None | ||
|
||
target_entry = self._data.get(target_index, None) | ||
if target_entry is None: | ||
self.logger.warning("entry '%s' not found", target) | ||
if with_index: | ||
return target_index, target_entry | ||
return target_entry | ||
|
||
def get(self, target): | ||
"""Get entry by id or name.""" | ||
entry = self._lookup(target) | ||
return entry |
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
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
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,14 @@ | ||
"""Test suite for parameters module""" | ||
|
||
# pylint: disable=too-few-public-methods | ||
|
||
from luxtronik.calculations import Calculations | ||
|
||
|
||
class TestCalculations: | ||
"""Test suite for Calculations""" | ||
|
||
def test_init(self): | ||
"""Test cases for initialization""" | ||
calculations = Calculations() | ||
assert calculations.name == "Calculation" |
Oops, something went wrong.
300a08a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report