forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_info.py
More file actions
73 lines (49 loc) · 1.89 KB
/
version_info.py
File metadata and controls
73 lines (49 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''
Defines the `VersionInfo` class.
See its documentation for more details.
'''
from operator import itemgetter as _itemgetter
class VersionInfo(tuple):
'''
Version number. This is a variation on a `namedtuple`.
Example:
VersionInfo(1, 2, 0) == \
VersionInfo(major=1, minor=2, micro=0, modifier='release') == \
(1, 2, 0)
'''
__slots__ = ()
_fields = ('major', 'minor', 'micro', 'modifier')
def __new__(cls, major, minor=0, micro=0, modifier='release'):
'''
Create new instance of `VersionInfo(major, minor, micro, modifier)`.
'''
assert isinstance(major, int)
assert isinstance(minor, int)
assert isinstance(micro, int)
assert isinstance(modifier, str)
return tuple.__new__(cls, (major, minor, micro, modifier))
def __repr__(self):
'''Return a nicely formatted representation string.'''
return 'VersionInfo(major=%r, minor=%r, micro=%r, modifier=%r)' % self
def _asdict(self):
'''
Return a new `OrderedDict` which maps field names to their values.
'''
from python_toolbox.nifty_collections import OrderedDict
return OrderedDict(zip(self._fields, self))
def __getnewargs__(self):
'''Return self as a plain tuple. Used by copy and pickle.'''
return tuple(self)
@property
def version_text(self):
'''A textual description of the version, like '1.4.2 beta'.'''
version_text = '%s.%s.%s' % (self.major, self.minor, self.micro)
if self.modifier != 'release':
version_text += ' %s' % self.modifier
return version_text
major = property(_itemgetter(0))
minor = property(_itemgetter(1))
micro = property(_itemgetter(2))
modifier = property(_itemgetter(3))