Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
distutils.version.StrictVersion Class Reference
Inheritance diagram for distutils.version.StrictVersion:
Inheritance graph
[legend]
Collaboration diagram for distutils.version.StrictVersion:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def parse
def __str__
def __repr__
def __eq__
def __ne__
def __lt__
def __le__
def __gt__
def __ge__

Public Attributes

 version
 prerelease

Static Public Attributes

tuple version_re

Private Member Functions

def _cmp

Detailed Description

Version numbering for anal retentives and software idealists.
Implements the standard interface for version number classes as
described above.  A version number consists of two or three
dot-separated numeric components, with an optional "pre-release" tag
on the end.  The pre-release tag consists of the letter 'a' or 'b'
followed by a number.  If the numeric components of two version
numbers are equal, then one with a pre-release tag will always
be deemed earlier (lesser) than one without.

The following are valid version numbers (shown in the order that
would be obtained by sorting according to the supplied cmp function):

    0.4       0.4.0  (these two are equivalent)
    0.4.1
    0.5a1
    0.5b3
    0.5
    0.9.6
    1.0
    1.0.4a3
    1.0.4b1
    1.0.4

The following are examples of invalid version numbers:

    1
    2.7.2.2
    1.3.a4
    1.3pl1
    1.3c4

The rationale for this version numbering system will be explained
in the distutils documentation.

Definition at line 99 of file version.py.


Member Function Documentation

def distutils.version.Version.__eq__ (   self,
  other 
) [inherited]

Definition at line 45 of file version.py.

00045 
00046     def __eq__(self, other):
00047         c = self._cmp(other)
00048         if c is NotImplemented:
00049             return c
00050         return c == 0

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.version.Version.__ge__ (   self,
  other 
) [inherited]

Definition at line 75 of file version.py.

00075 
00076     def __ge__(self, other):
00077         c = self._cmp(other)
00078         if c is NotImplemented:
00079             return c
00080         return c >= 0
00081 
00082 
00083 # Interface for version-number classes -- must be implemented
00084 # by the following classes (the concrete ones -- Version should
00085 # be treated as an abstract class).
00086 #    __init__ (string) - create and take same action as 'parse'
00087 #                        (string parameter is optional)
00088 #    parse (string)    - convert a string representation to whatever
00089 #                        internal representation is appropriate for
00090 #                        this style of version numbering
00091 #    __str__ (self)    - convert back to a string; should be very similar
00092 #                        (if not identical to) the string supplied to parse
00093 #    __repr__ (self)   - generate Python code to recreate
00094 #                        the instance
00095 #    _cmp (self, other) - compare two version numbers ('other' may
00096 #                        be an unparsed version string, or another
00097 #                        instance of your version class)
00098 

Here is the call graph for this function:

def distutils.version.Version.__gt__ (   self,
  other 
) [inherited]

Definition at line 69 of file version.py.

00069 
00070     def __gt__(self, other):
00071         c = self._cmp(other)
00072         if c is NotImplemented:
00073             return c
00074         return c > 0

Here is the call graph for this function:

def distutils.version.Version.__le__ (   self,
  other 
) [inherited]

Definition at line 63 of file version.py.

00063 
00064     def __le__(self, other):
00065         c = self._cmp(other)
00066         if c is NotImplemented:
00067             return c
00068         return c <= 0

Here is the call graph for this function:

def distutils.version.Version.__lt__ (   self,
  other 
) [inherited]

Definition at line 57 of file version.py.

00057 
00058     def __lt__(self, other):
00059         c = self._cmp(other)
00060         if c is NotImplemented:
00061             return c
00062         return c < 0

Here is the call graph for this function:

def distutils.version.Version.__ne__ (   self,
  other 
) [inherited]

Definition at line 51 of file version.py.

00051 
00052     def __ne__(self, other):
00053         c = self._cmp(other)
00054         if c is NotImplemented:
00055             return c
00056         return c != 0

Here is the call graph for this function:

def distutils.version.Version.__repr__ (   self) [inherited]

Reimplemented in distutils.version.LooseVersion.

Definition at line 42 of file version.py.

00042 
00043     def __repr__ (self):
00044         return "%s ('%s')" % (self.__class__.__name__, str(self))

def distutils.version.StrictVersion.__str__ (   self)

Definition at line 159 of file version.py.

00159 
00160     def __str__ (self):
00161 
00162         if self.version[2] == 0:
00163             vstring = '.'.join(map(str, self.version[0:2]))
00164         else:
00165             vstring = '.'.join(map(str, self.version))
00166 
00167         if self.prerelease:
00168             vstring = vstring + self.prerelease[0] + str(self.prerelease[1])
00169 
00170         return vstring
00171 

def distutils.version.StrictVersion._cmp (   self,
  other 
) [private]

Definition at line 172 of file version.py.

00172 
00173     def _cmp (self, other):
00174         if isinstance(other, str):
00175             other = StrictVersion(other)
00176 
00177         if self.version != other.version:
00178             # numeric versions don't match
00179             # prerelease stuff doesn't matter
00180             if self.version < other.version:
00181                 return -1
00182             else:
00183                 return 1
00184 
00185         # have to compare prerelease
00186         # case 1: neither has prerelease; they're equal
00187         # case 2: self has prerelease, other doesn't; other is greater
00188         # case 3: self doesn't have prerelease, other does: self is greater
00189         # case 4: both have prerelease: must compare them!
00190 
00191         if (not self.prerelease and not other.prerelease):
00192             return 0
00193         elif (self.prerelease and not other.prerelease):
00194             return -1
00195         elif (not self.prerelease and other.prerelease):
00196             return 1
00197         elif (self.prerelease and other.prerelease):
00198             if self.prerelease == other.prerelease:
00199                 return 0
00200             elif self.prerelease < other.prerelease:
00201                 return -1
00202             else:
00203                 return 1
00204         else:
00205             assert False, "never get here"
00206 
00207 # end class StrictVersion
00208 
00209 
00210 # The rules according to Greg Stein:
00211 # 1) a version number has 1 or more numbers separated by a period or by
00212 #    sequences of letters. If only periods, then these are compared
00213 #    left-to-right to determine an ordering.
00214 # 2) sequences of letters are part of the tuple for comparison and are
00215 #    compared lexicographically
00216 # 3) recognize the numeric components may have leading zeroes
00217 #
00218 # The LooseVersion class below implements these rules: a version number
00219 # string is split up into a tuple of integer and string components, and
00220 # comparison is a simple tuple comparison.  This means that version
00221 # numbers behave in a predictable and obvious way, but a way that might
00222 # not necessarily be how people *want* version numbers to behave.  There
00223 # wouldn't be a problem if people could stick to purely numeric version
00224 # numbers: just split on period and compare the numbers as tuples.
00225 # However, people insist on putting letters into their version numbers;
00226 # the most common purpose seems to be:
00227 #   - indicating a "pre-release" version
00228 #     ('alpha', 'beta', 'a', 'b', 'pre', 'p')
00229 #   - indicating a post-release patch ('p', 'pl', 'patch')
00230 # but of course this can't cover all version number schemes, and there's
00231 # no way to know what a programmer means without asking him.
00232 #
00233 # The problem is what to do with letters (and other non-numeric
00234 # characters) in a version number.  The current implementation does the
00235 # obvious and predictable thing: keep them as strings and compare
00236 # lexically within a tuple comparison.  This has the desired effect if
00237 # an appended letter sequence implies something "post-release":
00238 # eg. "0.99" < "0.99pl14" < "1.0", and "5.001" < "5.001m" < "5.002".
00239 #
00240 # However, if letters in a version number imply a pre-release version,
00241 # the "obvious" thing isn't correct.  Eg. you would expect that
00242 # "1.5.1" < "1.5.2a2" < "1.5.2", but under the tuple/lexical comparison
00243 # implemented here, this just isn't so.
00244 #
00245 # Two possible solutions come to mind.  The first is to tie the
00246 # comparison algorithm to a particular set of semantic rules, as has
00247 # been done in the StrictVersion class above.  This works great as long
00248 # as everyone can go along with bondage and discipline.  Hopefully a
00249 # (large) subset of Python module programmers will agree that the
00250 # particular flavour of bondage and discipline provided by StrictVersion
00251 # provides enough benefit to be worth using, and will submit their
00252 # version numbering scheme to its domination.  The free-thinking
00253 # anarchists in the lot will never give in, though, and something needs
00254 # to be done to accommodate them.
00255 #
00256 # Perhaps a "moderately strict" version class could be implemented that
00257 # lets almost anything slide (syntactically), and makes some heuristic
00258 # assumptions about non-digits in version number strings.  This could
00259 # sink into special-case-hell, though; if I was as talented and
00260 # idiosyncratic as Larry Wall, I'd go ahead and implement a class that
00261 # somehow knows that "1.2.1" < "1.2.2a2" < "1.2.2" < "1.2.2pl3", and is
00262 # just as happy dealing with things like "2g6" and "1.13++".  I don't
00263 # think I'm smart enough to do it right though.
00264 #
00265 # In any case, I've coded the test suite for this module (see
00266 # ../test/test_version.py) specifically to fail on things like comparing
00267 # "1.2a2" and "1.2".  That's not because the *code* is doing anything
00268 # wrong, it's because the simple, obvious design doesn't match my
00269 # complicated, hairy expectations for real-world version numbers.  It
00270 # would be a snap to fix the test suite to say, "Yep, LooseVersion does
00271 # the Right Thing" (ie. the code matches the conception).  But I'd rather
00272 # have a conception that matches common notions about version numbers.

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.version.StrictVersion.parse (   self,
  vstring 
)

Definition at line 140 of file version.py.

00140 
00141     def parse (self, vstring):
00142         match = self.version_re.match(vstring)
00143         if not match:
00144             raise ValueError("invalid version number '%s'" % vstring)
00145 
00146         (major, minor, patch, prerelease, prerelease_num) = \
00147             match.group(1, 2, 4, 5, 6)
00148 
00149         if patch:
00150             self.version = tuple(map(int, [major, minor, patch]))
00151         else:
00152             self.version = tuple(map(int, [major, minor])) + (0,)
00153 
00154         if prerelease:
00155             self.prerelease = (prerelease[0], int(prerelease_num))
00156         else:
00157             self.prerelease = None
00158 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

distutils.version.StrictVersion.prerelease

Definition at line 154 of file version.py.

Definition at line 149 of file version.py.

tuple distutils.version.StrictVersion.version_re [static]
Initial value:
re.compile(r'^(\d+) \. (\d+) (\. (\d+))? ([ab](\d+))?$',
                            re.VERBOSE | re.ASCII)

Definition at line 136 of file version.py.


The documentation for this class was generated from the following file: