1
+
2
+ def parse_last_component (last_component_str ):
3
+ """
4
+ Parse the last component of the version string __version__
5
+ In:
6
+ last_component_str: str, last component of version string e.g. for '0.12.3a5' is '3a5'
7
+ Returns:
8
+ a: str, e.g. for '3a5' is 3
9
+ b: str, e.g. for '3a5' is 'alpha'
10
+ c: str, e.g. for '3a5' is 5
11
+ """
12
+ spec_inv = {v :k for k ,v in _specifier_ .items ()}
13
+ for specifier in _specifier_ .values ():
14
+ splt = last_component_str .split (specifier )
15
+ if len (splt ) == 1 :
16
+ continue
17
+ return int (splt [0 ]), spec_inv [specifier ], int (splt [1 ])
18
+
19
+
20
+ def parse_version (v_str ):
21
+ """
22
+ Convert __version__ -> version_info format
23
+
24
+ In:
25
+ v_str: str, version string e.g. '0.12.3a5'
26
+
27
+ Out:
28
+ tuple, len == 5; format == version_info (see below)
29
+ """
30
+ tmp = v_str .split ('.' )
31
+ a ,b ,c = parse_last_component (tmp [2 ])
32
+ return (
33
+ int (tmp [0 ]), int (tmp [1 ]), a , b , c
34
+ )
35
+
36
+
37
+ def compare_versions (v_str1 , v_str2 ):
38
+ """
39
+ In:
40
+ v_str1: str, __version__ string parsable by parse_version
41
+ v_str2: str, __version__ string parsable by parse_version
42
+
43
+ Returns:
44
+ '>' | '==' | '<', reads v_str1 (x) v_str2
45
+ """
46
+
47
+ p_str1 = parse_version (v_str1 )
48
+ p_str2 = parse_version (v_str2 )
49
+ p_str1 [3 ] = _specifier_order_ [p_str1 [3 ]]
50
+ p_str2 [3 ] = _specifier_order_ [p_str2 [3 ]]
51
+ for i , comp_i in enumerate (p_str1 ):
52
+ comp_j = p_str2 [i ]
53
+ if comp_i > comp_j :
54
+ return '>'
55
+ elif comp_i == comp_j :
56
+ return '=='
57
+ elif comp_i < comp_j :
58
+ return '<'
59
+
60
+
1
61
version_info = (0 , 12 , 3 , 'alpha' , 5 )
2
62
3
- _specifier_ = {'alpha' : 'a' , 'beta' : 'b' , 'candidate' : 'rc' , 'final' : '' }
63
+ _specifier_ = {
64
+ 'alpha' : 'a' ,
65
+ 'beta' : 'b' ,
66
+ 'candidate' : 'rc' ,
67
+ 'final' : ''
68
+ }
69
+
70
+ _specifier_order_ = {
71
+ 'alpha' : 1 ,
72
+ 'beta' : 2 ,
73
+ 'candidate' : 3 ,
74
+ 'final' : 4
75
+ }
4
76
5
77
__version__ = '%s.%s.%s%s' % (version_info [0 ], version_info [1 ], version_info [2 ],
6
- '' if version_info [3 ]== 'final' else _specifier_ [version_info [3 ]]+ str (version_info [4 ]))
78
+ '' if version_info [3 ]== 'final' else _specifier_ [version_info [3 ]]+ str (version_info [4 ]))
0 commit comments