Commits

Anonymous committed 097818d

added RationalVersion.from_parts classmethod

Comments (0)

Files changed (1)

 >>> str(v)
 '1.0.dev456post623'
 
+>>> parts = v.parts
+>>> v2 = V.from_parts(*v.parts)
+>>> v2 == v
+True
+>>> str(v2) == str(v)
+True
+
 # Irrational version strings
 
 >>> v = V('1')
         """
         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'
                              (dev(?P<dev>\d+))|
                              (post(?P<post>\d+))))?
        $
-    # A marker used in the second and third parts of the `info` tuple, for
+    # 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):
                               'post', groups.get('devpost2')))
         else:
             parts.append(self._final_marker)
-        self.info = tuple(parts)
-        if error_on_huge_major_num and self.info[0][0] > 1980:
+        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.info[0][0], s))
+                "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):
         return nums
 
     def __str__(self):
-        main, prerel, devpost = self.info
-        s = '.'.join(str(v) for v in main)# if v)
-        if prerel is not self._final_marker:
+        return self.parts_to_str(self.parts)
+
+    @classmethod
+    def parts_to_str(cls, parts):
+        """Transforms a version expressed in tuple into its string
+        representation."""
+        main, prerel, devpost = parts
+        s = '.'.join(str(v) for v in main)
+        if prerel is not cls._final_marker:
             s += prerel[0]
-            s += '.'.join(str(v) for v in prerel[1:])# if v)
-        if devpost is not self._final_marker:
+            s += '.'.join(str(v) for v in prerel[1:])
+        if devpost is not cls._final_marker:
             if devpost[0] == 'f':
                 devpost = devpost[1:]
-            s += '.' + ''.join(str(v) for v in devpost)# if v)
+            s += '.' + ''.join(str(v) for v in devpost)
         return s
 
     def __repr__(self):
         if not isinstance(other, RationalVersion):
             raise TypeError("cannot compare %s and %s"
                 % (type(self).__name__, type(other).__name__))
-        return self.info == other.info
+        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__))
-        return self.info < other.info
+        return self.parts < other.parts
     def __ne__(self, other):
         return not self.__eq__(other)
     def __gt__(self, other):