From acef3dd342c21638feba9e6ae97a684fb980a170 Mon Sep 17 00:00:00 2001 From: Vratislav Podzimek Date: Mon, 7 Mar 2016 12:07:08 +0100 Subject: [PATCH] New version - Make Size instances hashable (vpodzime) --- libbytesize.spec | 8 +++++- size_instances_hashable.patch | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 size_instances_hashable.patch diff --git a/libbytesize.spec b/libbytesize.spec index 29e56e6..c96c7c5 100644 --- a/libbytesize.spec +++ b/libbytesize.spec @@ -1,11 +1,13 @@ Name: libbytesize Version: 0.3 -Release: 1%{?dist} +Release: 2%{?dist} Summary: A library for working with sizes in bytes License: LGPLv2+ URL: https://github.com/rhinstaller/libbytesize Source0: https://github.com/rhinstaller/libbytesize/archive/%{name}-%{version}.tar.gz +Patch0: size_instances_hashable.patch + %define realname bytesize BuildRequires: gmp-devel @@ -52,6 +54,7 @@ the library from Python 3 easier and more convenient. %prep %setup -q -n %{name}-%{version} +%patch0 -p1 %build %configure @@ -86,6 +89,9 @@ find %{buildroot} -type f -name "*.la" | xargs %{__rm} %{python3_sitearch}/bytesize/__pycache__/bytesize.* %changelog +* Mon Mar 7 2016 Vratislav Podzimek - 0.3-2 +- Make Size instances hashable (vpodzime) + * Fri Feb 26 2016 Vratislav Podzimek - 0.3-1 - Packaging changes related to getting rid of GLib/GObject (vpodzime) - Adapt the python bindings and tests (vpodzime) diff --git a/size_instances_hashable.patch b/size_instances_hashable.patch new file mode 100644 index 0000000..a351913 --- /dev/null +++ b/size_instances_hashable.patch @@ -0,0 +1,47 @@ +From 5463719b1a0c27e9c7f6017531a1d90ac762b31b Mon Sep 17 00:00:00 2001 +From: Vratislav Podzimek +Date: Mon, 7 Mar 2016 11:56:14 +0100 +Subject: [PATCH] Make Size instances hashable + +This is useful e.g. for getting rid of duplicate items in lists and/or creation +of sets of sizes in general. +--- + src/python/bytesize.py | 3 +++ + tests/lbs_py_override_unittest.py | 8 ++++++++ + 2 files changed, 11 insertions(+) + +diff --git a/src/python/bytesize.py b/src/python/bytesize.py +index e3c9098..179719c 100644 +--- a/src/python/bytesize.py ++++ b/src/python/bytesize.py +@@ -547,5 +547,8 @@ class Size(object): + def __reduce__(self): + return (self.__class__, (self.get_bytes(),)) + ++ def __hash__(self): ++ return self.get_bytes() ++ + def __del__(self): + del(self._c_size) +diff --git a/tests/lbs_py_override_unittest.py b/tests/lbs_py_override_unittest.py +index 17043ac..b502b30 100755 +--- a/tests/lbs_py_override_unittest.py ++++ b/tests/lbs_py_override_unittest.py +@@ -244,6 +244,14 @@ class SizeTestCase(unittest.TestCase): + self.assertIsNot(size1, size2) + self.assertEqual(size1, size2) + ++ def testHashable(self): ++ size = Size("1 KiB") ++ hs = hash(size) ++ self.assertIsNotNone(hs) ++ ++ size_set = set((Size("1 KiB"), Size("1 KiB"), Size("1 KiB"), Size("2 KiB"), Size(0))) ++ self.assertEqual(len(size_set), 3) ++ + #endclass + + # script entry point +-- +2.5.0 +