-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Common base class for para, calc, visi
- Loading branch information
Showing
4 changed files
with
88 additions
and
133 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,60 @@ | ||
"""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): | ||
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 entry by either id or name.""" | ||
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