New version

- Make Size instances hashable (vpodzime)
This commit is contained in:
Vratislav Podzimek 2016-03-07 12:07:08 +01:00
parent dd8a76ab02
commit acef3dd342
2 changed files with 54 additions and 1 deletions

View File

@ -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 <vpodzime@redhat.com> - 0.3-2
- Make Size instances hashable (vpodzime)
* Fri Feb 26 2016 Vratislav Podzimek <vpodzime@redhat.com> - 0.3-1
- Packaging changes related to getting rid of GLib/GObject (vpodzime)
- Adapt the python bindings and tests (vpodzime)

View File

@ -0,0 +1,47 @@
From 5463719b1a0c27e9c7f6017531a1d90ac762b31b Mon Sep 17 00:00:00 2001
From: Vratislav Podzimek <vpodzime@redhat.com>
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