Skip to content

Commit

Permalink
Merge pull request #1029 from ocefpaf/remove_MemoizedDataset
Browse files Browse the repository at this point in the history
remove MemoizedDataset
  • Loading branch information
benjwadams authored Jun 6, 2023
2 parents b75a1a9 + 4d66c02 commit 1f4532f
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 27 deletions.
16 changes: 0 additions & 16 deletions compliance_checker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
from contextlib import contextmanager
from functools import lru_cache
from tempfile import NamedTemporaryFile
from typing import BinaryIO, Generator

from netCDF4 import Dataset

try:
from ._version import __version__
except ImportError:
__version__ = "unknown"


class MemoizedDataset(Dataset):
"""
A NetCDF dataset which has its get_variables_by_attributes call memoized in
order to speed up repeated calls to the function. This should only really
be used against netCDF Datasets opened in 'r' mode, as the attributes should
not change upon reading the files.
"""

@lru_cache(128)
def get_variables_by_attributes(self, **kwargs):
return super().get_variables_by_attributes(**kwargs)


@contextmanager
def tempnc(data: BinaryIO) -> Generator[str, None, None]:
"""
Expand Down
4 changes: 2 additions & 2 deletions compliance_checker/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from owslib.swe.sensor.sml import SensorML

import compliance_checker.cfutil as cfutil
from compliance_checker import MemoizedDataset, __version__
from compliance_checker import __version__
from compliance_checker.util import kvp_convert

# Python 3.5+ should work, also have a fallback
Expand Down Expand Up @@ -201,7 +201,7 @@ class BaseNCCheck:
Base Class for NetCDF Dataset supporting Check Suites.
"""

supported_ds = {Dataset, MemoizedDataset}
supported_ds = [Dataset]

@classmethod
def std_check_in(cls, dataset, name, allowed_vals):
Expand Down
3 changes: 1 addition & 2 deletions compliance_checker/cf/cf_1_8.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from netCDF4 import Dataset
from shapely.geometry import Polygon

from compliance_checker import MemoizedDataset
from compliance_checker.base import BaseCheck, TestCtx
from compliance_checker.cf.cf_1_7 import CF1_7Check
from compliance_checker.cf.util import reference_attr_variables, string_from_var_type
Expand All @@ -46,7 +45,7 @@ def __init__(self, options=None):
},
)

def check_groups(self, ds: MemoizedDataset):
def check_groups(self, ds: Dataset):
"""
2.7.2. Application of attributes
Expand Down
8 changes: 4 additions & 4 deletions compliance_checker/suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from owslib.swe.sensor.sml import SensorML
from pkg_resources import working_set

from compliance_checker import MemoizedDataset, __version__, tempnc
from compliance_checker import __version__, tempnc
from compliance_checker.base import BaseCheck, GenericFile, Result, fix_return_value
from compliance_checker.protocols import cdl, netcdf, opendap

Expand Down Expand Up @@ -828,15 +828,15 @@ def check_remote_netcdf(self, ds_str):
if netcdf.is_remote_netcdf(ds_str):
response = requests.get(ds_str, allow_redirects=True, timeout=60)
try:
return MemoizedDataset(
return Dataset(
urlparse(response.url).path,
memory=response.content,
)
except OSError:
# handle case when netCDF C libs weren't compiled with
# in-memory support by using tempfile
with tempnc(response.content) as _nc:
return MemoizedDataset(_nc)
return Dataset(_nc)

def load_remote_dataset(self, ds_str):
"""
Expand Down Expand Up @@ -893,7 +893,7 @@ def load_local_dataset(self, ds_str):
ds_str = self.generate_dataset(ds_str)

if netcdf.is_netcdf(ds_str):
return MemoizedDataset(ds_str)
return Dataset(ds_str)

# Assume this is just a Generic File if it exists
if os.path.isfile(ds_str):
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ ignore = [
"E402",
"A001",
]
"compliance_checker/__init__.py" = ["B019"]
"compliance_checker/cfutil.py" = ["B028"]
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cftime>=1.1.0
isodate>=0.6.1
jinja2>=2.7.3
lxml>=3.2.1
netcdf4>=1.5.7
netcdf4>=1.6.4
owsLib>=0.8.3
pendulum>=1.2.4
pygeoif>=0.6
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pip_requirements(fname="requirements.txt"):
url="https://github.com/ioos/compliance-checker",
packages=find_packages(),
install_requires=pip_requirements(),
python_requires="~=3.5",
python_requires="~=3.7",
tests_require=["pytest"],
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down

0 comments on commit 1f4532f

Please sign in to comment.