diff options
author | Todd Gamblin <tgamblin@llnl.gov> | 2016-07-06 09:01:19 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-06 09:01:19 -0700 |
commit | 03fdb2058a42c1a05f78bb6f05d8546fccf332a2 (patch) | |
tree | ac0849932d1dce0b7a44e44d4839d95312be3854 | |
parent | 2b9d02d59414a6584c569989d806d48019aa5832 (diff) | |
parent | 9b926a480f84c62cf4ed23c26b950dd7c89fb1ad (diff) | |
download | spack-03fdb2058a42c1a05f78bb6f05d8546fccf332a2.tar.gz spack-03fdb2058a42c1a05f78bb6f05d8546fccf332a2.tar.bz2 spack-03fdb2058a42c1a05f78bb6f05d8546fccf332a2.tar.xz spack-03fdb2058a42c1a05f78bb6f05d8546fccf332a2.zip |
Merge pull request #1176 from epfl-scitas/features/version_minor_improvements
spack.Version : like a duck!
-rw-r--r-- | lib/spack/spack/test/versions.py | 36 | ||||
-rw-r--r-- | lib/spack/spack/version.py | 19 |
2 files changed, 53 insertions, 2 deletions
diff --git a/lib/spack/spack/test/versions.py b/lib/spack/spack/test/versions.py index 4624f901c8..a3a328fb14 100644 --- a/lib/spack/spack/test/versions.py +++ b/lib/spack/spack/test/versions.py @@ -389,3 +389,39 @@ class VersionsTest(unittest.TestCase): self.assertEqual(v.dotted, '1.2.3') self.assertEqual(v.dashed, '1-2-3') self.assertEqual(v.underscored, '1_2_3') + + def test_repr_and_str(self): + + def check_repr_and_str(vrs): + a = Version(vrs) + self.assertEqual(repr(a), 'Version(\'' + vrs + '\')') + b = eval(repr(a)) + self.assertEqual(a, b) + self.assertEqual(str(a), vrs) + self.assertEqual(str(a), str(b)) + + check_repr_and_str('1.2.3') + check_repr_and_str('R2016a') + check_repr_and_str('R2016a.2-3_4') + + def test_get_item(self): + a = Version('0.1_2-3') + self.assertTrue(isinstance(a[1], int)) + # Test slicing + b = a[0:2] + self.assertTrue(isinstance(b, Version)) + self.assertEqual(b, Version('0.1')) + self.assertEqual(repr(b), 'Version(\'0.1\')') + self.assertEqual(str(b), '0.1') + b = a[0:3] + self.assertTrue(isinstance(b, Version)) + self.assertEqual(b, Version('0.1_2')) + self.assertEqual(repr(b), 'Version(\'0.1_2\')') + self.assertEqual(str(b), '0.1_2') + b = a[1:] + self.assertTrue(isinstance(b, Version)) + self.assertEqual(b, Version('1_2-3')) + self.assertEqual(repr(b), 'Version(\'1_2-3\')') + self.assertEqual(str(b), '1_2-3') + # Raise TypeError on tuples + self.assertRaises(TypeError, b.__getitem__, 1, 2) diff --git a/lib/spack/spack/version.py b/lib/spack/spack/version.py index 858d581472..6839643941 100644 --- a/lib/spack/spack/version.py +++ b/lib/spack/spack/version.py @@ -44,6 +44,7 @@ be called on any of the types:: concrete """ import re +import numbers from bisect import bisect_left from functools import wraps @@ -194,10 +195,24 @@ class Version(object): return iter(self.version) def __getitem__(self, idx): - return tuple(self.version[idx]) + cls = type(self) + if isinstance(idx, numbers.Integral): + return self.version[idx] + elif isinstance(idx, slice): + # Currently len(self.separators) == len(self.version) - 1 + extendend_separators = self.separators + ('',) + string_arg = [] + for token, sep in zip(self.version, extendend_separators)[idx]: + string_arg.append(str(token)) + string_arg.append(str(sep)) + string_arg.pop() # We don't need the last separator + string_arg = ''.join(string_arg) + return cls(string_arg) + message = '{cls.__name__} indices must be integers' + raise TypeError(message.format(cls=cls)) def __repr__(self): - return self.string + return 'Version(' + repr(self.string) + ')' def __str__(self): return self.string |