Klasse für Versionsinformationen

Code-Stücke können hier veröffentlicht werden.
Benutzeravatar
Dookie
Python-Forum Veteran
Beiträge: 2010
Registriert: Freitag 11. Oktober 2002, 18:00
Wohnort: Salzburg
Kontaktdaten:

Klasse für Versionsinformationen

Beitragvon Dookie » Dienstag 8. Februar 2005, 16:32

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
    Modul:          Version
    Description:    Description
    Version:        1.0.1a
    Copyright:      2004 by Fritz Cizmarov fritz@sol.at
    Created:        08. Feb. 2005
    Last modified:  08. Feb. 2005
    License:        Python license
    Requirements:   Python2.3
    Exports:        Classes and Functions to export
"""

import re

class Version(tuple):
    """
        Class for versioninformation, representet by a tuple holding
        major-, minor- and subversion.
        To create an versioninstance use Version(0,1,0) or Version("0.1.0")
        The elements can also be called by the properties major, minor and sub
    """

    def major(self):
        return self[0]
    major = property(major)

    def minor(self):
        return self[1]
    minor = property(minor)

    def sub(self):
        return self[2]
    sub = property(sub)

    def state(self):
        return self[3]
    state = property(state)

    def __new__(cls, major=0, minor=0, sub=0, state=""):
        if isinstance(major, basestring):
            vs = re.findall(r"((\d+\.\d+\.\d+)|(\d+\.\d))(\w*)", major)
            if vs:
                v = [int(x) for x in vs[0][0].split(".")]
                while len(v) < 3:
                    v.append(0)
                major, minor, sub = v
                state = vs[0][3]
        return super(Version, cls).__new__(cls, (major, minor, sub, state))

    def __repr__(self):
        return "Version(%i, %i, %i, '%s')" % self

    def __str__(self):
        return "%i.%i.%i%s" % self

    def __add__(self, other):
        major, minor, sub = [a+b for a, b in zip(self[:3], other[:3])]
        return type(self)(major, minor, sub, self.state)


if __name__ == "__main__":
    v1 = Version("1.0.1a0")
    print v1
    v2 = Version(1, 2, 3)
    print v2
    print v1+v2
    print v1 < v2
    print "Major:", v1.major, "Minor:", v1.minor
    print "Sub:", v1.sub, "State:" , v1.state




Gruß

Dookie

Code: Alles auswählen

#!/usr/bin/env python
import this

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder