From 6cc79f81420a2b576999007c7132e58e92041ce7 Mon Sep 17 00:00:00 2001 From: Chris Green Date: Thu, 26 Apr 2018 17:38:43 -0500 Subject: Allow len(Version) to return number of split components. (#7918) --- lib/spack/spack/test/versions.py | 8 ++++++++ lib/spack/spack/version.py | 3 +++ 2 files changed, 11 insertions(+) (limited to 'lib') diff --git a/lib/spack/spack/test/versions.py b/lib/spack/spack/test/versions.py index e89d39553f..50c1dcb83a 100644 --- a/lib/spack/spack/test/versions.py +++ b/lib/spack/spack/test/versions.py @@ -488,6 +488,14 @@ def test_repr_and_str(): check_repr_and_str('R2016a.2-3_4') +def test_len(): + a = Version('1.2.3.4') + assert len(a) == len(a.version) + assert(len(a) == 4) + b = Version('2018.0') + assert(len(b) == 2) + + def test_get_item(): a = Version('0.1_2-3') assert isinstance(a[1], int) diff --git a/lib/spack/spack/version.py b/lib/spack/spack/version.py index 59041d8009..c863f52525 100644 --- a/lib/spack/spack/version.py +++ b/lib/spack/spack/version.py @@ -253,6 +253,9 @@ class Version(object): def __iter__(self): return iter(self.version) + def __len__(self): + return len(self.version) + def __getitem__(self, idx): cls = type(self) -- cgit v1.2.3-70-g09d2