Skip to main content

A script and python module to compare version numbers. Use this to compare the version strings of packages, modules, really anything.

Project description

A script and python module to compare version numbers. Use this to compare the version strings of packages, modules, really anything.

Commandline Tool

You can use the provided cmp-version tool to compare two version strings.

It will print to stdout “-1” if version1 is less/older than version2, “0” if they are equal, and “1” if version1 is greater/newer than version2.

Examples:

$ ./cmp-version 1.0.1b 1.0.1a

1

$ ./cmp-version 1.0.1b 1.0.2

-1

$ ./cmp-version 1.0 1.0.0

0

Comparing Filenames

Without actually scanning the contents of a package, you can from a script compare the versions based on the filenames.

Example:

$ cmp-version glibc-6.2.1-3.x86_64.rpm glibc-6.2.3-1.x86_64.rpm

-1

Method Signature

The cmp_version module provides a single method, cmp_version which compares two versions “cmp” style (think strcmp or the “cmp” operator in python<3).

def cmp_version(version1, version2):

‘’’

cmp_version - Compare two version strings, checking which one represents a “newer” (greater) release.

Note that even if two version strings are not equal string-wise, they may still be equal version-wise (e.x. 1.0.0 is the same version as 1.0)

@param version1 <str> - A version string

@param version2 <str> - A version string

@return <int>

-1 if version1 is older/less than version2

0 if version1 is equal to version2

1 if version1 is newer/greater than version2

So for example,

cmp_version(‘1.0.5b’, ‘1.0.5a’) would return 1 because 1.0.5b comes after 1.0.5a

Sorting Lists

You can sort a list of versions, or filenames containing versions etc like this:

>>> from cmp_version import cmp_version
>>>
>>> x = ['1.12', '1.21', '1.01b']
>>> sorted(x, cmp=cmp_version)

[‘1.01b’, ‘1.12’, ‘1.21’]

Return value

  • 0 is returned when the two versions are equal, even if the strings are not equal (for example “1.0” is the same version as “1.0.0”)

  • -1 is returned when version1 is older/less than version2

  • 1 is returned when version1 is newer/greater than version2.

License

This module is released under Public Domain.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cmp_version-1.0.2.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file cmp_version-1.0.2.tar.gz.

File metadata

  • Download URL: cmp_version-1.0.2.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cmp_version-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e426d027a1b603355af7da5a04a3445f79cc91310b609620391daf47c12ccb08
MD5 7a5daa28ad46c2c6d2d8930606de5140
BLAKE2b-256 8a00961079f7a453e908b055d37d9a448497698d2892f23d8266262508a44af0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page