Skip to content

Commit

Permalink
Removes more deprecated datetime.utcnow and always check if a cache
Browse files Browse the repository at this point in the history
entry isn't None before slicing it

Signed-off-by: Alexis Jeandet <[email protected]>
  • Loading branch information
jeandet committed Apr 17, 2024
1 parent 0462597 commit c26e967
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
22 changes: 14 additions & 8 deletions speasy/core/cache/_providers_caches.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import math
from datetime import datetime, timedelta
from datetime import datetime, timedelta, UTC
from functools import wraps
from typing import List, Tuple

Expand Down Expand Up @@ -180,8 +180,10 @@ def wrapped(wrapped_self, product, start_time, stop_time, **kwargs):
if len(data_chunks):
if len(data_chunks) == 1:
return data_chunks[0][dt_range.start_time:dt_range.stop_time].copy()
data_chunks[0] = data_chunks[0][dt_range.start_time:]
data_chunks[-1] = data_chunks[-1][:dt_range.stop_time]
if data_chunks[0] is not None:
data_chunks[0] = data_chunks[0][dt_range.start_time:]
if data_chunks[-1] is not None:
data_chunks[-1] = data_chunks[-1][:dt_range.stop_time]
return merge_variables(data_chunks)[dt_range.start_time:dt_range.stop_time]
return None

Expand All @@ -196,7 +198,7 @@ def __init__(self, prefix, cache_instance=_cache, start_time_arg='start_time', s
cache_retention=None):
self._cache = _Cacheable(prefix, cache_instance=cache_instance, start_time_arg=start_time_arg,
stop_time_arg=stop_time_arg,
version=lambda x, y: datetime.utcnow().isoformat(),
version=lambda x, y: datetime.now(tz=UTC).isoformat(),
fragment_hours=fragment_hours, cache_margins=cache_margins, leak_cache=leak_cache,
entry_name=entry_name)
self.cache_retention = cache_retention or timedelta(days=14)
Expand All @@ -209,7 +211,7 @@ def split_fragments(self, fragments, product, fragment_duration, **kwargs):
for fragment, entry in zip(fragments, entries):
if entry is None:
missing_fragments.append(fragment)
elif (entry.version + self.cache_retention) > datetime.utcnow():
elif (entry.version + self.cache_retention) > datetime.now(tz=UTC):
try:
data_chunks.append(from_dictionary(entry.data))
except Exception as e:
Expand Down Expand Up @@ -250,7 +252,7 @@ def wrapped(wrapped_self, product, start_time, stop_time, **kwargs):
wrapped_self, product=product, start_time=fragment_group[0],
stop_time=fragment_group[-1] + fragment_duration, **kwargs),
fragments=fragment_group, product=product, fragment_duration_hours=fragment_hours,
version=datetime.utcnow(), **kwargs)
version=datetime.now(tz=UTC), **kwargs)
for fragment_group
in progress_bar(leave=False, desc="Downloading missing fragments from cache", **kwargs)(
missing_fragments)]))
Expand All @@ -273,8 +275,12 @@ def wrapped(wrapped_self, product, start_time, stop_time, **kwargs):

if len(data_chunks):
if len(data_chunks) == 1:
return data_chunks[0][dt_range.start_time:dt_range.stop_time].copy()
data_chunks[0] = data_chunks[0][dt_range.start_time:]
if data_chunks[0] is not None:
return data_chunks[0][dt_range.start_time:dt_range.stop_time].copy()
else:
return None
if data_chunks[0] is not None:
data_chunks[0] = data_chunks[0][dt_range.start_time:]
if data_chunks[-1] is not None:
data_chunks[-1] = data_chunks[-1][:dt_range.stop_time]
return merge_variables(data_chunks)[dt_range.start_time:dt_range.stop_time]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tempfile
import time
import unittest
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta, timezone, UTC

import dateutil.parser as dt_parser
import numpy as np
Expand Down Expand Up @@ -50,7 +50,7 @@ def _make_data(self, product, start_time, stop_time):
@UnversionedProviderCache(prefix="", cache_instance=cache, leak_cache=True,
cache_retention=timedelta(microseconds=5e5))
def _make_unversioned_data(self, product, start_time, stop_time, if_newer_than=None):
if if_newer_than is None or (if_newer_than + timedelta(seconds=1)) < datetime.utcnow():
if if_newer_than is None or (if_newer_than + timedelta(seconds=1)) < datetime.now(tz=UTC):
self._make_unversioned_data_cntr += 1
return data_generator(start_time, stop_time)
return None
Expand Down

0 comments on commit c26e967

Please sign in to comment.