From 2ef7b2fa5d5e908c6ef5e7a56d797a7ccd503b0d Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Tue, 6 Feb 2018 11:49:52 +0100 Subject: [PATCH] Removed hash wrapper from Python. --- python/ecl/util/CMakeLists.txt | 1 - python/ecl/util/__init__.py | 1 - python/ecl/util/hash.py | 134 ------------------------- python/ert/util/__init__.py | 1 - python/tests/legacy_tests/test_util.py | 1 - python/tests/util_tests/CMakeLists.txt | 2 - python/tests/util_tests/test_hash.py | 68 ------------- 7 files changed, 208 deletions(-) delete mode 100644 python/ecl/util/hash.py delete mode 100644 python/tests/util_tests/test_hash.py diff --git a/python/ecl/util/CMakeLists.txt b/python/ecl/util/CMakeLists.txt index f093f2e183..3616b95259 100644 --- a/python/ecl/util/CMakeLists.txt +++ b/python/ecl/util/CMakeLists.txt @@ -3,7 +3,6 @@ set(PYTHON_SOURCES bool_vector.py ctime.py double_vector.py - hash.py int_vector.py install_abort_signals.py lookup_table.py diff --git a/python/ecl/util/__init__.py b/python/ecl/util/__init__.py index b40611b85f..9a8f1ebf14 100644 --- a/python/ecl/util/__init__.py +++ b/python/ecl/util/__init__.py @@ -61,7 +61,6 @@ from .matrix import Matrix from .stat import quantile, quantile_sorted, polyfit from .lookup_table import LookupTable -from .hash import Hash, StringHash, DoubleHash, IntegerHash from .thread_pool import ThreadPool from .cthread_pool import CThreadPool, startCThreadPool from .install_abort_signals import installAbortSignals, updateAbortSignals diff --git a/python/ecl/util/hash.py b/python/ecl/util/hash.py deleted file mode 100644 index b82da26e5b..0000000000 --- a/python/ecl/util/hash.py +++ /dev/null @@ -1,134 +0,0 @@ -# Copyright (C) 2011 Statoil ASA, Norway. -# -# The file 'hash.py' is part of ERT - Ensemble based Reservoir Tool. -# -# ERT is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# ERT is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. -# -# See the GNU General Public License at -# for more details. -from ctypes import c_void_p - -from cwrap import BaseCClass -from ecl import EclPrototype -from ecl.util import StringList - - -class Hash(BaseCClass): - _alloc = EclPrototype("void* hash_alloc()" , bind = False) - _free = EclPrototype("void hash_free(hash)") - _size = EclPrototype("int hash_get_size(hash)") - _keys = EclPrototype("stringlist_obj hash_alloc_stringlist(hash)") - _has_key = EclPrototype("bool hash_has_key(hash, char*)") - _get = EclPrototype("void* hash_get(hash, char*)") - _insert_ref = EclPrototype("void hash_insert_ref(hash, char*, void*)") - - """ - Base hash class that supports string:void* values - """ - - def __init__(self): - c_ptr = self._alloc() - super(Hash, self).__init__(c_ptr) - - def __len__(self): - return self._size() - - def __getitem__(self, key): - if self._has_key(key): - return self._get(key) - else: - raise KeyError("Hash does not have key: %s" % key) - - def __setitem__(self, key, value): - if isinstance(value, c_void_p): - self._insert_ref(key, value) - else: - raise ValueError("Hash does not support type: %s" % value.__class__) - - def __contains__(self, key): - """ @rtype: bool """ - return self._has_key(key) - - def __iter__(self): - for key in self.keys(): - yield key - - def keys(self): - """ @rtype: StringList """ - return self._keys() - - def free(self): - self._free() - - def __str__(self): - return str(["%s: %s" % (key, self[key]) for key in self.keys()]) - - -class StringHash(Hash): - _get_string = EclPrototype("char* hash_get_string(hash, char*)") - _insert_string = EclPrototype("void hash_insert_string(hash, char*, char*)") - - def __init__(self): - super(StringHash, self).__init__() - - def __setitem__(self, key, value): - if isinstance(value, str): - self._insert_string(key, value) - else: - raise ValueError("StringHash does not support type: %s" % value.__class__) - - def __getitem__(self, key): - if key in self: - return self._get_string(key) - else: - raise KeyError("Hash does not have key: %s" % key) - - -class IntegerHash(Hash): - _get_int = EclPrototype("int hash_get_int(hash, char*)") - _insert_int = EclPrototype("void hash_insert_int(hash, char*, int)") - - def __init__(self): - super(IntegerHash, self).__init__() - - def __setitem__(self, key, value): - if isinstance(value, int): - self._insert_int(key, value) - else: - raise ValueError("IntegerHash does not support type: %s" % value.__class__) - - def __getitem__(self, key): - if key in self: - return self._get_int(key) - else: - raise KeyError("Hash does not have key: %s" % key) - - -class DoubleHash(Hash): - _get_double = EclPrototype("double hash_get_double(hash, char*)") - _insert_double = EclPrototype("void hash_insert_double(hash, char*, double)") - - def __init__(self): - super(DoubleHash, self).__init__() - - def __setitem__(self, key, value): - if isinstance(value, int): - value = float(value) - - if isinstance(value, float): - self._insert_double(key, value) - else: - raise ValueError("DoubleHash does not support type: %s" % value.__class__) - - def __getitem__(self, key): - if key in self: - return self._get_double( key) - else: - raise KeyError("Hash does not have key: %s" % key) diff --git a/python/ert/util/__init__.py b/python/ert/util/__init__.py index 525bf88b5e..b3ee46f4c0 100644 --- a/python/ert/util/__init__.py +++ b/python/ert/util/__init__.py @@ -12,7 +12,6 @@ from ecl.util import Matrix from ecl.util import quantile, quantile_sorted, polyfit from ecl.util import LookupTable -from ecl.util import Hash, StringHash, DoubleHash, IntegerHash from ecl.util import ThreadPool from ecl.util import CThreadPool, startCThreadPool from ecl.util import installAbortSignals, updateAbortSignals diff --git a/python/tests/legacy_tests/test_util.py b/python/tests/legacy_tests/test_util.py index 75eb2eac70..679025cbcd 100644 --- a/python/tests/legacy_tests/test_util.py +++ b/python/tests/legacy_tests/test_util.py @@ -12,7 +12,6 @@ from ert.util import Matrix from ert.util import quantile, quantile_sorted, polyfit from ert.util import LookupTable -from ert.util import Hash, StringHash, DoubleHash, IntegerHash from ert.util import ThreadPool from ert.util import CThreadPool, startCThreadPool from ert.util import installAbortSignals, updateAbortSignals diff --git a/python/tests/util_tests/CMakeLists.txt b/python/tests/util_tests/CMakeLists.txt index 05c67d7eb5..f5a02ce48d 100644 --- a/python/tests/util_tests/CMakeLists.txt +++ b/python/tests/util_tests/CMakeLists.txt @@ -1,7 +1,6 @@ set(TEST_SOURCES __init__.py test_ctime.py - test_hash.py test_lookup_table.py test_matrix.py test_rng.py @@ -21,7 +20,6 @@ set(TEST_SOURCES add_python_package("python.tests.util_tests" ${PYTHON_INSTALL_PREFIX}/tests/util_tests "${TEST_SOURCES}" False) addPythonTest(tests.util_tests.test_ctime.CTimeTest) -addPythonTest(tests.util_tests.test_hash.HashTest) addPythonTest(tests.util_tests.test_lookup_table.LookupTableTest ) addPythonTest(tests.util_tests.test_matrix.MatrixTest ) addPythonTest(tests.util_tests.test_rng.RngTest ) diff --git a/python/tests/util_tests/test_hash.py b/python/tests/util_tests/test_hash.py deleted file mode 100644 index 707742070f..0000000000 --- a/python/tests/util_tests/test_hash.py +++ /dev/null @@ -1,68 +0,0 @@ -from ctypes import c_void_p - -from tests import EclTest -from ecl.util import Hash, StringHash, DoubleHash, IntegerHash - - -class HashTest(EclTest): - def test_string_hash(self): - hash = StringHash() - - self.assertEqual(len(hash), 0) - - hash["hipp"] = "" - - self.assertEqual(len(hash), 1) - - with self.assertRaises(ValueError): - hash["hopp"] = 55 - - with self.assertRaises(KeyError): - hopp = hash["hopp"] - - self.assertTrue("hipp" in hash) - - self.assertEqual(list(hash.keys()), ["hipp"]) - - def test_int_hash(self): - hash = IntegerHash() - - with self.assertRaises(ValueError): - hash["one"] = "ein" - - with self.assertRaises(ValueError): - hash["one"] = 1.0 - - hash["two"] = 2 - - self.assertEqual(hash["two"], 2) - - def test_double_hash(self): - hash = DoubleHash() - - with self.assertRaises(ValueError): - hash["one"] = "ein" - - hash["two"] = 2 - hash["three"] = 3.0 - - self.assertEqual(hash["two"], 2) - self.assertEqual(hash["three"], 3.0) - - def test_c_void_p_hash(self): - hash = Hash() - - cp = c_void_p(512) - hash["1"] = cp - - self.assertEqual(hash["1"], cp.value) - - def test_for_in_hash(self): - hash = StringHash() - - hash["one"] = "one" - hash["two"] = "two" - hash["three"] = "three" - - for key in hash: - self.assertTrue(key in hash)