Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/_version.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 """Utility to compare (Numpy) version strings. | |
2 | |
3 The NumpyVersion class allows properly comparing numpy version strings. | |
4 The LooseVersion and StrictVersion classes that distutils provides don't | |
5 work; they don't recognize anything like alpha/beta/rc/dev versions. | |
6 | |
7 """ | |
8 from __future__ import division, absolute_import, print_function | |
9 | |
10 import re | |
11 | |
12 from numpy.compat import basestring | |
13 | |
14 | |
15 __all__ = ['NumpyVersion'] | |
16 | |
17 | |
18 class NumpyVersion(): | |
19 """Parse and compare numpy version strings. | |
20 | |
21 Numpy has the following versioning scheme (numbers given are examples; they | |
22 can be > 9) in principle): | |
23 | |
24 - Released version: '1.8.0', '1.8.1', etc. | |
25 - Alpha: '1.8.0a1', '1.8.0a2', etc. | |
26 - Beta: '1.8.0b1', '1.8.0b2', etc. | |
27 - Release candidates: '1.8.0rc1', '1.8.0rc2', etc. | |
28 - Development versions: '1.8.0.dev-f1234afa' (git commit hash appended) | |
29 - Development versions after a1: '1.8.0a1.dev-f1234afa', | |
30 '1.8.0b2.dev-f1234afa', | |
31 '1.8.1rc1.dev-f1234afa', etc. | |
32 - Development versions (no git hash available): '1.8.0.dev-Unknown' | |
33 | |
34 Comparing needs to be done against a valid version string or other | |
35 `NumpyVersion` instance. Note that all development versions of the same | |
36 (pre-)release compare equal. | |
37 | |
38 .. versionadded:: 1.9.0 | |
39 | |
40 Parameters | |
41 ---------- | |
42 vstring : str | |
43 Numpy version string (``np.__version__``). | |
44 | |
45 Examples | |
46 -------- | |
47 >>> from numpy.lib import NumpyVersion | |
48 >>> if NumpyVersion(np.__version__) < '1.7.0'): | |
49 ... print('skip') | |
50 skip | |
51 | |
52 >>> NumpyVersion('1.7') # raises ValueError, add ".0" | |
53 | |
54 """ | |
55 | |
56 def __init__(self, vstring): | |
57 self.vstring = vstring | |
58 ver_main = re.match(r'\d[.]\d+[.]\d+', vstring) | |
59 if not ver_main: | |
60 raise ValueError("Not a valid numpy version string") | |
61 | |
62 self.version = ver_main.group() | |
63 self.major, self.minor, self.bugfix = [int(x) for x in | |
64 self.version.split('.')] | |
65 if len(vstring) == ver_main.end(): | |
66 self.pre_release = 'final' | |
67 else: | |
68 alpha = re.match(r'a\d', vstring[ver_main.end():]) | |
69 beta = re.match(r'b\d', vstring[ver_main.end():]) | |
70 rc = re.match(r'rc\d', vstring[ver_main.end():]) | |
71 pre_rel = [m for m in [alpha, beta, rc] if m is not None] | |
72 if pre_rel: | |
73 self.pre_release = pre_rel[0].group() | |
74 else: | |
75 self.pre_release = '' | |
76 | |
77 self.is_devversion = bool(re.search(r'.dev', vstring)) | |
78 | |
79 def _compare_version(self, other): | |
80 """Compare major.minor.bugfix""" | |
81 if self.major == other.major: | |
82 if self.minor == other.minor: | |
83 if self.bugfix == other.bugfix: | |
84 vercmp = 0 | |
85 elif self.bugfix > other.bugfix: | |
86 vercmp = 1 | |
87 else: | |
88 vercmp = -1 | |
89 elif self.minor > other.minor: | |
90 vercmp = 1 | |
91 else: | |
92 vercmp = -1 | |
93 elif self.major > other.major: | |
94 vercmp = 1 | |
95 else: | |
96 vercmp = -1 | |
97 | |
98 return vercmp | |
99 | |
100 def _compare_pre_release(self, other): | |
101 """Compare alpha/beta/rc/final.""" | |
102 if self.pre_release == other.pre_release: | |
103 vercmp = 0 | |
104 elif self.pre_release == 'final': | |
105 vercmp = 1 | |
106 elif other.pre_release == 'final': | |
107 vercmp = -1 | |
108 elif self.pre_release > other.pre_release: | |
109 vercmp = 1 | |
110 else: | |
111 vercmp = -1 | |
112 | |
113 return vercmp | |
114 | |
115 def _compare(self, other): | |
116 if not isinstance(other, (basestring, NumpyVersion)): | |
117 raise ValueError("Invalid object to compare with NumpyVersion.") | |
118 | |
119 if isinstance(other, basestring): | |
120 other = NumpyVersion(other) | |
121 | |
122 vercmp = self._compare_version(other) | |
123 if vercmp == 0: | |
124 # Same x.y.z version, check for alpha/beta/rc | |
125 vercmp = self._compare_pre_release(other) | |
126 if vercmp == 0: | |
127 # Same version and same pre-release, check if dev version | |
128 if self.is_devversion is other.is_devversion: | |
129 vercmp = 0 | |
130 elif self.is_devversion: | |
131 vercmp = -1 | |
132 else: | |
133 vercmp = 1 | |
134 | |
135 return vercmp | |
136 | |
137 def __lt__(self, other): | |
138 return self._compare(other) < 0 | |
139 | |
140 def __le__(self, other): | |
141 return self._compare(other) <= 0 | |
142 | |
143 def __eq__(self, other): | |
144 return self._compare(other) == 0 | |
145 | |
146 def __ne__(self, other): | |
147 return self._compare(other) != 0 | |
148 | |
149 def __gt__(self, other): | |
150 return self._compare(other) > 0 | |
151 | |
152 def __ge__(self, other): | |
153 return self._compare(other) >= 0 | |
154 | |
155 def __repr(self): | |
156 return "NumpyVersion(%s)" % self.vstring |