Skip to content

version

util.version

POSTFIXES module-attribute

POSTFIXES = {None: -9999, 'rc': 1, 'beta': 2, 'alpha': 3}

Postfix

Postfix(postfix: Optional[str] = None)

Defines pre-release version type. (None > "rc" > "beta" > "alpha")

Usage
version = Version(0, 0, 0, "rc1")
print(version.postfix.name)
# rc
print(version.postfix.version)
# 1

Raises:

  • VersionFormatError

    postfix must be one of "rc", "beta", "alpha" or None

Source code in SaigeToolkit/util/version.py
def __init__(self, postfix: Optional[str] = None):
    if postfix is None:
        self.name, self.version = None, None
    else:
        match = re.match(r"^([a-zA-Z]+)(\d+)$", postfix)
        if match:
            self.name = match.groups()[0]
            self.version = int(match.groups()[-1])
        else:
            raise VersionFormatError
    self.priority = POSTFIXES[self.name]

name class-attribute instance-attribute

name: Optional[str] = None

version class-attribute instance-attribute

version: Optional[int] = None

priority class-attribute instance-attribute

priority: int = POSTFIXES[name]

__bool__

__bool__()
Source code in SaigeToolkit/util/version.py
def __bool__(self):
    return bool(self.name)

__str__

__str__()
Source code in SaigeToolkit/util/version.py
def __str__(self):
    if self:
        postfix_string = f"{self.name}{self.version}"
    else:
        postfix_string = ""
    return postfix_string

__repr__

__repr__() -> str
Source code in SaigeToolkit/util/version.py
def __repr__(self) -> str:
    return str(self)

__eq__

__eq__(other)
Source code in SaigeToolkit/util/version.py
def __eq__(self, other):
    return self.priority == other.priority and self.version == other.version

__gt__

__gt__(other)
Source code in SaigeToolkit/util/version.py
def __gt__(self, other):
    if self.version or other.version:
        return -self.priority > -other.priority or (
            self.priority == other.priority and self.version > other.version
        )
    # self.version == other.version == None인 경우 False 반환
    return False

__ge__

__ge__(other)
Source code in SaigeToolkit/util/version.py
def __ge__(self, other):
    return self == other or self > other

__lt__

__lt__(other)
Source code in SaigeToolkit/util/version.py
def __lt__(self, other):
    return not self >= other

__le__

__le__(other)
Source code in SaigeToolkit/util/version.py
def __le__(self, other):
    return not self > other

Version

Version(major: Union[int, str], minor: Union[int, str], patch: Union[int, str], postfix: Optional[str] = None)

Defines Version type: https://semver.org/

Usage
version = Version(0, 0, 0)
print(version)
# 0.0.0
version_rc = Version(0, 0, 0, "rc1")
print(version_rc)
# 0.0.0-rc1
version2 = Version.from_string("0.0.1")
print(version2 > version)
# True
print(Version.from_string("0.0.1") > Version(0, 0, 1, "rc1"))
# True
print(Version.from_string("0.0.1-rc1") > Version(0, 0, 1, "beta2"))
# True

Raises:

  • VersionFormatError

    version string format should be f"{self.major}.{self.minor}.{self.patch}-{self.postfix}"

Source code in SaigeToolkit/util/version.py
def __init__(
    self,
    major: Union[int, str],
    minor: Union[int, str],
    patch: Union[int, str],
    postfix: Optional[str] = None,
):

    self.major = int(major)
    self.minor = int(minor)
    self.patch = int(patch)
    self.postfix = Postfix(postfix)

major instance-attribute

major: int = int(major)

minor instance-attribute

minor: int = int(minor)

patch instance-attribute

patch: int = int(patch)

postfix instance-attribute

postfix: Postfix = Postfix(postfix)

__str__

__str__() -> str
Source code in SaigeToolkit/util/version.py
def __str__(self) -> str:
    version_string = f"{self.major}.{self.minor}.{self.patch}"
    if self.postfix:
        version_string = f"{version_string}-{self.postfix}"
    return version_string

__repr__

__repr__() -> str
Source code in SaigeToolkit/util/version.py
def __repr__(self) -> str:
    return str(self)

from_string classmethod

from_string(version_string: str)
Source code in SaigeToolkit/util/version.py
@classmethod
def from_string(cls, version_string: str):
    try:
        major, minor, patch_postfix = version_string.split(".")

        if "-" in patch_postfix:
            patch = patch_postfix.split("-")[0]
            postfix = patch_postfix.split("-")[-1]
        else:
            patch = patch_postfix
            postfix = None

        return cls(major, minor, patch, postfix)
    except Exception as e:
        raise VersionFormatError from e

__eq__

__eq__(other)
Source code in SaigeToolkit/util/version.py
def __eq__(self, other):
    return (
        self.major == other.major
        and self.minor == other.minor
        and self.patch == other.patch
        and self.postfix == other.postfix
    )

__gt__

__gt__(other)
Source code in SaigeToolkit/util/version.py
def __gt__(self, other):
    return (
        self.major > other.major
        or (self.major == other.major and self.minor > other.minor)
        or (self.major == other.major and self.minor == other.minor and self.patch > other.patch)
        or (
            self.major == other.major
            and self.minor == other.minor
            and self.patch == other.patch
            and self.postfix > other.postfix
        )
    )

__ge__

__ge__(other)
Source code in SaigeToolkit/util/version.py
def __ge__(self, other):
    return self == other or self > other

__lt__

__lt__(other)
Source code in SaigeToolkit/util/version.py
def __lt__(self, other):
    return not self >= other

__le__

__le__(other)
Source code in SaigeToolkit/util/version.py
def __le__(self, other):
    return not self > other