Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle units and descriptions for empty tags #162

Merged
merged 8 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tagreader-python
Submodule tagreader-python added at 13d0e2
55 changes: 35 additions & 20 deletions tagreader/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd

from .cache import BucketCache, SmartCache

from .utils import (ReaderType, ensure_datetime_with_tz, find_registry_key,
find_registry_key_from_name, is_windows, logging)
from .web_handlers import (AspenHandlerWeb, PIHandlerWeb, get_auth_aspen,
Expand All @@ -15,8 +16,12 @@
if is_windows():
import pyodbc

from .odbc_handlers import (AspenHandlerODBC, PIHandlerODBC,
list_aspen_sources, list_pi_sources)
from .odbc_handlers import (
AspenHandlerODBC,
PIHandlerODBC,
list_aspen_sources,
list_pi_sources,
)
from .utils import winreg

logging.basicConfig(
Expand Down Expand Up @@ -402,31 +407,41 @@ def get_units(self, tags):
tags = [tags]
units = {}
for tag in tags:
if self.cache is not None:
r = self.cache.fetch_tag_metadata(tag, "unit")
if "unit" in r:
units[tag] = r["unit"]
if tag not in units:
unit = self.handler._get_tag_unit(tag)
if self.cache is not None and unit is not None:
self.cache.store_tag_metadata(tag, {"unit": unit})
units[tag] = unit
try:
if self.cache is not None:
r = self.cache.fetch_tag_metadata(tag, "unit")
if "unit" in r:
units[tag] = r["unit"]
if tag not in units:
unit = self.handler._get_tag_unit(tag)
if self.cache is not None and unit is not None:
self.cache.store_tag_metadata(tag, {"unit": unit})
units[tag] = unit
except:
if self.search(tag) == []: # check for nonexisting string
print("Tag not found: " + str(tag))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really have used a logger, but since we haven't configured one I suggest to do:
warnings.warn(f"Tag not found: {tag}")
Same for get_description()

break
return units

def get_descriptions(self, tags):
if isinstance(tags, str):
tags = [tags]
descriptions = {}
for tag in tags:
if self.cache is not None:
r = self.cache.fetch_tag_metadata(tag, "description")
if "description" in r:
descriptions[tag] = r["description"]
if tag not in descriptions:
desc = self.handler._get_tag_description(tag)
if self.cache is not None and desc is not None:
self.cache.store_tag_metadata(tag, {"description": desc})
descriptions[tag] = desc
try:
if self.cache is not None:
r = self.cache.fetch_tag_metadata(tag, "description")
if "description" in r:
descriptions[tag] = r["description"]
if tag not in descriptions:
desc = self.handler._get_tag_description(tag)
if self.cache is not None and desc is not None:
self.cache.store_tag_metadata(tag, {"description": desc})
descriptions[tag] = desc
except:
if self.search(tag) == []: # check for nonexisting string
print("Tag not found: " + str(tag))
break
return descriptions

def read_tags(self, tags, start_time, stop_time, ts, read_type=ReaderType.INT):
Expand Down