### Install looseversion from source Source: https://github.com/effigies/looseversion/blob/main/README.md Clone the repository and install the package locally. ```bash git clone https://github.com/effigies/looseversion.git pip install looseversion/ ``` -------------------------------- ### Install looseversion via PyPI Source: https://github.com/effigies/looseversion/blob/main/README.md Use pip to install the package from the Python Package Index. ```bash pip install looseversion ``` -------------------------------- ### Handling Various Version Formats and Sorting Source: https://context7.com/effigies/looseversion/llms.txt Illustrates parsing and sorting a list of versions with diverse formats using LooseVersion. ```python # Various version format examples versions = [ LooseVersion("1.5.1"), LooseVersion("1.5.2b2"), LooseVersion("161"), LooseVersion("3.10a"), LooseVersion("8.02"), LooseVersion("1996.07.12"), LooseVersion("3.2.pl0"), LooseVersion("2g6"), LooseVersion("0.960923"), LooseVersion("2.2beta29"), LooseVersion("1.13++"), LooseVersion("5.5.kw"), ] # Sort versions sorted_versions = sorted(versions) for v in sorted_versions: print(f"{v}") ``` -------------------------------- ### Basic Version Comparison with LooseVersion Source: https://context7.com/effigies/looseversion/llms.txt Demonstrates basic version comparison using LooseVersion. Strings are automatically coerced for comparison. ```python from looseversion import LooseVersion # Basic version comparison v1 = LooseVersion("1.5.1") v2 = LooseVersion("1.5.2b2") print(v1 < v2) # True print(v1 > v2) # False print(v1 == v2) # False print(v1 <= v2) # True print(v1 >= v2) # False # Direct string comparison (strings are automatically coerced) print(LooseVersion("1.0.0") < "2.0.0") # True print(LooseVersion("3.10a") > "161") # False ``` -------------------------------- ### Comparing Looseversion with distutils.version.LooseVersion Source: https://context7.com/effigies/looseversion/llms.txt Illustrates seamless comparison between Looseversion instances and the original distutils.version.LooseVersion, facilitating gradual codebase migration. ```python from looseversion import LooseVersion import warnings # Suppress deprecation warning from distutils with warnings.catch_warnings(): warnings.simplefilter("ignore") from distutils.version import LooseVersion as DistutilsLooseVersion # Create versions from both libraries new_version = LooseVersion("2.0.0") old_version = DistutilsLooseVersion("1.5.0") # Cross-library comparisons work seamlessly print(new_version > old_version) # True print(old_version < new_version) # True print(new_version == DistutilsLooseVersion("2.0.0")) # True # Works in both directions print(DistutilsLooseVersion("3.0") > LooseVersion("2.0")) # True ``` -------------------------------- ### Demonstrate LooseVersion Parsing Source: https://context7.com/effigies/looseversion/llms.txt This snippet shows how LooseVersion parses different version string formats into a list of version components. It's useful for understanding how the library handles non-standard versioning schemes. ```python from looseversion import LooseVersion examples = [ ("1.5.1", [1, 5, 1]), ("1.5.2b2", [1, 5, 2, "b", 2]), ("161", [161]), ("3.10a", [3, 10, "a"]), ("1.13++", [1, 13, "++"]), ("3.2.pl0", [3, 2, "pl", 0]), ] for vstring, expected in examples: v = LooseVersion(vstring) print(f"{vstring} -> {v.version}") assert v.version == expected ``` -------------------------------- ### Using the parse() Method Source: https://context7.com/effigies/looseversion/llms.txt Demonstrates the `parse()` method of LooseVersion, which can be used to parse or re-parse a version string into its component parts. ```python from looseversion import LooseVersion # Create empty version and parse later v = LooseVersion() v.parse("1.2.3alpha4") print(v.vstring) # "1.2.3alpha4" print(v.version) # [1, 2, 3, 'alpha', 4] # Re-parse with different version v.parse("2.0.0") print(v.vstring) # "2.0.0" print(v.version) # [2, 0, 0] ``` -------------------------------- ### Legacy Version Schemes with LooseVersion2 Source: https://context7.com/effigies/looseversion/llms.txt Shows how LooseVersion2 can be used to sort and compare legacy version schemes, including those with pre-release identifiers like 'alpha' and 'beta'. ```python # Useful for legacy version schemes legacy_versions = [ LooseVersion2("1.0-alpha"), LooseVersion2("1.0-beta"), LooseVersion2("1.0"), LooseVersion2("1.0-patch1"), ] for v in legacy_versions: print(f"{v}: {v.version}") ``` -------------------------------- ### Accessing Parsed Version Components Source: https://context7.com/effigies/looseversion/llms.txt Shows how to access the original version string and its parsed components using the `vstring` and `version` attributes of a LooseVersion object. ```python # Access parsed components v = LooseVersion("1.5.2b2") print(v.vstring) # "1.5.2b2" - original string print(v.version) # [1, 5, 2, 'b', 2] - parsed components print(str(v)) # "1.5.2b2" print(repr(v)) # "LooseVersion ('1.5.2b2')" ``` -------------------------------- ### LooseVersion2: Python 2 Semantics for Version Comparison Source: https://context7.com/effigies/looseversion/llms.txt Demonstrates LooseVersion2, which restores Python 2 semantics where strings are considered greater than integers in comparisons. Useful for legacy version schemes. ```python from looseversion import LooseVersion, LooseVersion2 # Python 2 semantics with LooseVersion2 # Strings are always "greater" than integers v1 = LooseVersion2("0.3@v0.3") v2 = LooseVersion2("0.3.1@v0.3.1") # The "@" character makes string components that compare > integers print(v1 > v2) # True (string "@v0.3" > integer component) print(v1 < v2) # False # Beta versions with dash separator beta = LooseVersion2("13.0-beta3") release = LooseVersion2("13.0.1") print(beta > release) # True (string "-beta3" > integer 1) print(beta < release) # False ``` -------------------------------- ### Compare versions with LooseVersion Source: https://github.com/effigies/looseversion/blob/main/README.md Compare version strings using the LooseVersion class, which supports comparison between LooseVersion objects and strings. ```python >>> from looseversion import LooseVersion >>> LooseVersion("1.0.0") < LooseVersion("2.0.0") True >>> LooseVersion("1.0.0") < "2" True ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.