Anonymous avatar Anonymous committed 571f6e9

cleaning + consistent names

Comments (0)

Files changed (1)

 
 class IrrationalVersionError(Exception):
     """This is an irrational version."""
+    pass
 
 class HugeMajorVersionNumError(IrrationalVersionError):
     """An irrational version because the major version number is huge
     See `error_on_huge_major_num` option in `RationalVersion` for details.
     This guard can be disabled by setting that option False.
     """
+    pass
+
+# A marker used in the second and third parts of the `parts` tuple, for
+# versions that don't have those segments, to sort properly. A example
+# of versions in sort order ('highest' last):
+#   1.0b1                 ((1,0), ('b',1), ('f',))
+#   1.0.dev345            ((1,0), ('f',),  ('dev', 345))
+#   1.0                   ((1,0), ('f',),  ('f',))
+#   1.0.dev256post345     ((1,0), ('f',),  ('f', 'dev', 256, 'post', 345))
+#   1.0.post345           ((1,0), ('f',),  ('f', 'post', 345))
+#                                   ^        ^
+#   'f' < 'b' ---------------------/         |
+#                                            |
+#   'dev' < 'f' < 'post' -------------------/
+# Other letters would do, bug 'f' for 'final' is kind of nice.
+FINAL_MARKER = ('f',)
+
+VERSION_RE = re.compile(r'''
+    ^
+    (?P<version>\d+\.\d+)          # minimum 'N.N'
+    (?P<extraversion>(?:\.\d+)*)   # any number of extra '.N' segments
+    (?:
+        (?P<prerel>[abc])             # 'a'=alpha, 'b'=beta, 'c'=release candidate
+        (?P<prerelversion>\d+(?:\.\d+)*)
+    )?
+    (?P<devpost>\.((dev(?P<devpost1>\d+)post(?P<devpost2>\d+))|
+                            (dev(?P<dev>\d+))|
+                            (post(?P<post>\d+))))?
+    $''', re.VERBOSE)
 
 class RationalVersion(object):
     """A rational version.
         self._parse(s, error_on_huge_major_num)
 
     @classmethod
-    def from_parts(cls, version, abc=None, devpostmarker=None):
-        if abc is None:
-            abc = tuple()
-        if devpostmarker is None:
-            devpostmarker = tuple()
-        return cls(cls.parts_to_str((version, abc, devpostmarker)))
-
-    _version_re = re.compile(r'''
-        ^
-        (?P<version>\d+\.\d+)              # minimum 'N.N'
-        (?P<extraversion>(?:\.\d+)*)            # any number of extra '.N' segments
-        (?:
-          (?P<abc>[abc])               # 'a'=alpha, 'b'=beta, 'c'=release candidate
-          (?P<abcversion>\d+(?:\.\d+)*)
-        )?
-        (?P<devpostmarker>\.((dev(?P<devpost1>\d+)post(?P<devpost2>\d+))|
-                             (dev(?P<dev>\d+))|
-                             (post(?P<post>\d+))))?
-       $
-    # A marker used in the second and third parts of the `parts` tuple, for
-        ''', re.VERBOSE)
-    # versions that don't have those segments, to sort properly. A example
-    # of versions in sort order ('highest' last):
-    #   1.0b1                 ((1,0), ('b',1), ('f',))
-    #   1.0.dev345            ((1,0), ('f',),  ('dev', 345))
-    #   1.0                   ((1,0), ('f',),  ('f',))
-    #   1.0.dev256post345     ((1,0), ('f',),  ('f', 'dev', 256, 'post', 345))
-    #   1.0.post345           ((1,0), ('f',),  ('f', 'post', 345))
-    #                                   ^        ^
-    #   'f' < 'b' ---------------------/         |
-    #                                            |
-    #   'dev' < 'f' < 'post' -------------------/
-    # Other letters would do, bug 'f' for 'final' is kind of nice.
-    _final_marker = ('f',)
+    def from_parts(cls, version, prerelease=None, devpost=None):
+        if prerelease is None:
+            prerelease = tuple()
+        if devpost is None:
+            devpost = tuple()
+        return cls(cls.parts_to_str((version, prerelease, devpost)))
 
     def _parse(self, s, error_on_huge_major_num=True):
-        match = self._version_re.search(s)
+        """Parses a string version into parts."""
+        match = VERSION_RE.search(s)
         if not match:
             raise IrrationalVersionError(s)
 
         groups = match.groupdict()
         parts = []
+
+        # main version
         block = self._parse_numdots(groups['version'], s, False, 2)
         extraversion = groups.get('extraversion')
         if extraversion not in ('', None):
             block += self._parse_numdots(extraversion[1:], s)
         parts.append(tuple(block))
 
-        abc = groups.get('abc')
-        if abc is not None:
-            block = [abc]
-            block += self._parse_numdots(groups.get('abcversion'), s,
+        # prerelease
+        prerel = groups.get('prerel')
+        if prerel is not None:
+            block = [prerel]
+            block += self._parse_numdots(groups.get('prerelversion'), s,
                                          pad_zeros_length=1)
             parts.append(tuple(block))
         else:
-            parts.append(self._final_marker)
+            parts.append(FINAL_MARKER)
 
-        if groups.get('devpostmarker') is not None:
+        if groups.get('devpost') is not None:
             dev = groups.get('dev')
             post = groups.get('post')
             if dev is not None:
                 parts.append(('dev', dev))
             elif post is not None:
-                parts.append((self._final_marker[0], 'post', post))
+                parts.append((FINAL_MARKER[0], 'post', post))
             else:
-                parts.append((self._final_marker[0],
+                parts.append((FINAL_MARKER[0],
                               'dev', groups.get('devpost1'),
                               'post', groups.get('devpost2')))
         else:
-            parts.append(self._final_marker)
+            parts.append(FINAL_MARKER)
         self.parts = tuple(parts)
         if error_on_huge_major_num and self.parts[0][0] > 1980:
             raise HugeMajorVersionNumError("huge major version number, %r, "
                 "which might cause future problems: %r" % (self.parts[0][0], s))
 
     def _parse_numdots(self, s, full_ver_str, drop_trailing_zeros=True,
-            pad_zeros_length=0):
+                       pad_zeros_length=0):
         """Parse 'N.N.N' sequences, return a list of ints.
 
         @param s {str} 'N.N.N..." sequence to be parsed
         representation."""
         main, prerel, devpost = parts
         s = '.'.join(str(v) for v in main)
-        if prerel is not cls._final_marker:
+        if prerel is not FINAL_MARKER:
             s += prerel[0]
             s += '.'.join(str(v) for v in prerel[1:])
-        if devpost is not cls._final_marker:
+        if devpost is not FINAL_MARKER:
             if devpost[0] == 'f':
                 devpost = devpost[1:]
             s += '.' + ''.join(str(v) for v in devpost)
     def __repr__(self):
         return "%s('%s')" % (self.__class__.__name__, self)
 
-    # Comparison
+    def _cannot_compare(self, other):
+        raise TypeError("cannot compare %s and %s"
+                % (type(self).__name__, type(other).__name__))
+
     def __eq__(self, other):
         if not isinstance(other, RationalVersion):
-            raise TypeError("cannot compare %s and %s"
-                % (type(self).__name__, type(other).__name__))
+            self._cannot_compare(other)
         return self.parts == other.parts
+
     def __lt__(self, other):
         if not isinstance(other, RationalVersion):
-            raise TypeError("cannot compare %s and %s"
-                % (type(self).__name__, type(other).__name__))
+            self._cannot_compare(other)
         return self.parts < other.parts
+
     def __ne__(self, other):
         return not self.__eq__(other)
+
     def __gt__(self, other):
         return not (self.__lt__(other) or self.__eq__(other))
+
     def __le__(self, other):
         return self.__eq__(other) or self.__lt__(other)
+
     def __ge__(self, other):
         return self.__eq__(other) or self.__gt__(other)
 
-
 def suggest_rational_version(s):
     """Suggest a rational version close to the given version string.
 
 
     @param s {str} An irrational version string.
     @returns A rational version string, or None, if couldn't determine one.
-
-    >>> suggest_rational_version('1.0')
-    '1.0'
-    >>> suggest_rational_version('1.0-alpha1')
-    '1.0a1'
-    >>> suggest_rational_version('1.0rc2')
-    '1.0c2'
-    >>> suggest_rational_version('walla walla washington')  # no suggestion
     """
     try:
         RationalVersion(s)
     # PyPI stats: ~21 (0.62%) better
     rs = re.sub(r"\.?(pre|preview)(\d+)$", r"c\g<2>", rs)
 
-
     try:
         RationalVersion(rs)
         return rs   # already rational
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.