Skip to content

Commit

Permalink
Ignore error option in device load from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
oscgonfer committed Nov 11, 2024
1 parent 7b37603 commit d94ec1e
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions scdata/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from scdata.io.device_api import *
from scdata.models import Blueprint, Metric, Source, APIParams, CSVParams, DeviceOptions, Sensor

from os.path import join, basename
from os.path import join, basename, exists
from urllib.parse import urlparse
from pandas import DataFrame, to_timedelta, Timedelta
from numpy import nan
Expand Down Expand Up @@ -256,7 +256,8 @@ def del_metric(self, metric_name = ''):
return True
return False

async def load(self, cache=None, convert_units=True, convert_names=True, max_amount=None):
async def load(self, cache=None, convert_units=True,
convert_names=True, max_amount=None, ignore_error = True):
'''
Loads the device with some options
Expand All @@ -274,6 +275,9 @@ async def load(self, cache=None, convert_units=True, convert_names=True, max_amo
max_amount: int
Default: None
Trim dataframe to this amount for processing and forwarding purposes (workaround)
ignore_error: bool
Default: True
Ignore if the cache does not exist
Returns
----------
True if loaded correctly
Expand All @@ -289,16 +293,22 @@ async def load(self, cache=None, convert_units=True, convert_names=True, max_amo
# Only case where cache makes sense
if self.source.type == 'api':
if cache is not None and cache:
if cache.endswith('.csv'):
cached_data = read_csv_file(
path = cache,
timezone = timezone,
frequency = frequency,
clean_na = clean_na,
resample = resample,
index_name = 'TIME')
if not exists(cache):
if not ignore_error:
raise FileExistsError(f'Cache does not exist: {cache}')
else:
logger.warning(f'Cache file does not exist: {cache}')
else:
raise NotImplementedError(f'Cache needs to be a .csv file. Got {cache}.')
if cache.endswith('.csv'):
cached_data = read_csv_file(
path = cache,
timezone = timezone,
frequency = frequency,
clean_na = clean_na,
resample = resample,
index_name = 'TIME')
else:
raise NotImplementedError(f'Cache needs to be a .csv file. Got {cache}.')

# Make request with a logical min_date
if not cached_data.empty:
Expand Down

0 comments on commit d94ec1e

Please sign in to comment.