From c38363ef326973b6bb9c9af55cc6be1c551e9dd2 Mon Sep 17 00:00:00 2001 From: Eshan Agarwal Date: Sat, 25 Apr 2020 09:37:43 +0530 Subject: [PATCH 1/4] Update __init__.py --- tensorflow_datasets/core/utils/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_datasets/core/utils/__init__.py b/tensorflow_datasets/core/utils/__init__.py index f3363c436f1..c0ef13b5c9f 100644 --- a/tensorflow_datasets/core/utils/__init__.py +++ b/tensorflow_datasets/core/utils/__init__.py @@ -23,4 +23,5 @@ from tensorflow_datasets.core.utils.tqdm_utils import * from tensorflow_datasets.core.utils.version import Experiment from tensorflow_datasets.core.utils.version import Version +from tensorflow_datasets.core.utils.dummy_beam import DummyBeam # pylint: enable=wildcard-import From 0251701aadcfd3a897f92b3d2feee244e351bc16 Mon Sep 17 00:00:00 2001 From: Eshan-Agarwal Date: Sat, 25 Apr 2020 04:12:59 +0000 Subject: [PATCH 2/4] Add dummy beam wrapper --- tensorflow_datasets/core/utils/dummy_beam.py | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 tensorflow_datasets/core/utils/dummy_beam.py diff --git a/tensorflow_datasets/core/utils/dummy_beam.py b/tensorflow_datasets/core/utils/dummy_beam.py new file mode 100644 index 00000000000..f602a1f35da --- /dev/null +++ b/tensorflow_datasets/core/utils/dummy_beam.py @@ -0,0 +1,39 @@ +# coding=utf-8 +# Copyright 2020 The TensorFlow Datasets Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Lint as: python3 + + +_MSG = ("Failed importing {name}. Please install {name}." + " Using pip install {name}") + + +class DoFn(object): + def __call__(self, *args, **kwargs): + return None + +class Pipeline(object): + def __call__(self, *args, **kwargs): + return None + + +class DummyBeam(object): + + def __init__(self): + self.module_name = "apache_beam" + + def __getattr__(self, _): + err_msg = _MSG.format(name=self.module_name) + raise ImportError(err_msg) From a63d6be9683604e20bc1a2133afbee5495d63597 Mon Sep 17 00:00:00 2001 From: Eshan-Agarwal Date: Sun, 26 Apr 2020 08:39:08 +0000 Subject: [PATCH 3/4] minor update dummy_beam.py --- tensorflow_datasets/core/utils/dummy_beam.py | 84 +++++++++++--------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/tensorflow_datasets/core/utils/dummy_beam.py b/tensorflow_datasets/core/utils/dummy_beam.py index f602a1f35da..228d3e426e8 100644 --- a/tensorflow_datasets/core/utils/dummy_beam.py +++ b/tensorflow_datasets/core/utils/dummy_beam.py @@ -1,39 +1,45 @@ -# coding=utf-8 -# Copyright 2020 The TensorFlow Datasets Authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Lint as: python3 - - -_MSG = ("Failed importing {name}. Please install {name}." - " Using pip install {name}") - - -class DoFn(object): - def __call__(self, *args, **kwargs): - return None - -class Pipeline(object): - def __call__(self, *args, **kwargs): - return None - - -class DummyBeam(object): - - def __init__(self): - self.module_name = "apache_beam" - - def __getattr__(self, _): - err_msg = _MSG.format(name=self.module_name) - raise ImportError(err_msg) +# coding=utf-8 +# Copyright 2020 The TensorFlow Datasets Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Lint as: python3 +"""Utilities for beam wrapper.""" + +import types + +_MSG = ("Failed importing {name}. Please install {name}." + " Using pip install {name}") + +class Empty(object): + """Empty class for beam API.""" + def __call__(self, *args, **kwargs): + return None + + +class DummyBeam(types.ModuleType): + """Dummy class. + Raise: + ImportError, when calling beam API and apache_beam was not installed. + """ + DoFn = Empty + Pipeline = Empty + + def __init__(self): + self.module_name = "apache_beam" + + def __getattribute__(self,_): + if getattr(DummyBeam, _, None) is Empty: + _name= super().__getattribute__('module_name') + err_msg = _MSG.format(name=_name) + raise ImportError(err_msg) From c5b890ae7d8bb96d2c28d56de50bc0ccd1242586 Mon Sep 17 00:00:00 2001 From: Eshan-Agarwal Date: Sun, 26 Apr 2020 10:07:55 +0000 Subject: [PATCH 4/4] Add dummy_beam_test.py and fix pylint --- tensorflow_datasets/core/utils/dummy_beam.py | 19 ++++----- .../core/utils/dummy_beam_test.py | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 tensorflow_datasets/core/utils/dummy_beam_test.py diff --git a/tensorflow_datasets/core/utils/dummy_beam.py b/tensorflow_datasets/core/utils/dummy_beam.py index 228d3e426e8..4568c17909a 100644 --- a/tensorflow_datasets/core/utils/dummy_beam.py +++ b/tensorflow_datasets/core/utils/dummy_beam.py @@ -19,15 +19,14 @@ import types _MSG = ("Failed importing {name}. Please install {name}." - " Using pip install {name}") + " Using pip install {name}") -class Empty(object): +class Empty(): # pylint: disable=too-few-public-methods """Empty class for beam API.""" def __call__(self, *args, **kwargs): return None - -class DummyBeam(types.ModuleType): +class DummyBeam(types.ModuleType): # pylint: disable=too-few-public-methods """Dummy class. Raise: ImportError, when calling beam API and apache_beam was not installed. @@ -36,10 +35,10 @@ class DummyBeam(types.ModuleType): Pipeline = Empty def __init__(self): - self.module_name = "apache_beam" + self.module_name = "apache_beam" - def __getattribute__(self,_): - if getattr(DummyBeam, _, None) is Empty: - _name= super().__getattribute__('module_name') - err_msg = _MSG.format(name=_name) - raise ImportError(err_msg) + def __getattribute__(self, _): + if getattr(DummyBeam, _, None) is Empty: + _name = super().__getattribute__('module_name') + err_msg = _MSG.format(name=_name) + raise ImportError(err_msg) diff --git a/tensorflow_datasets/core/utils/dummy_beam_test.py b/tensorflow_datasets/core/utils/dummy_beam_test.py new file mode 100644 index 00000000000..cd24b31e700 --- /dev/null +++ b/tensorflow_datasets/core/utils/dummy_beam_test.py @@ -0,0 +1,42 @@ +# coding=utf-8 +# Copyright 2020 The TensorFlow Datasets Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Lint as: python3 +"""Test for dummy_beam.""" + +import tensorflow_datasets as tfds +from tensorflow_datasets.core.utils import dummy_beam + + +class DummyBeamTest(tfds.testing.TestCase): # pylint: disable=too-few-public-methods + """Testing class for dummy_beam.""" + + def test_dummy_beam(self): + """Test for DummyBeam.""" + _err_msg = ("Failed importing apache_beam." + " Please install apache_beam. Using pip install apache_beam") + try: + import apache_beam as beam + except ImportError: + beam = dummy_beam.DummyBeam() + with self.assertRaisesWithPredicateMatch(ImportError, _err_msg): + beam = dummy_beam.DummyBeam() + + class SomeFn(beam.DoFn): # pylint: disable=unused-variable, too-few-public-methods + """Empty class for test beam Wrapper""" + pass # pylint: disable=unnecessary-pass + +if __name__ == '__main__': + tfds.testing.test_main()