IAM

OPENSOURCEFAN STUDYING
STUDYINGCOMPUTERSCIENCEANDMATH COMPUTERSCIENCE

Check out the latest superpixel benchmark — Superpixel Benchmark (2016) — and let me know your opinion! @david_stutz
14thDECEMBER2016

SNIPPET

A simple method to compare software/package versions in Python.

version_compare.py
# -*- coding: utf-8 -*-
"""
Version compare in Python.
"""

import numpy
import re

def version_compare(version_a, version_b):
    """
    Compare two versions given as strings, taken from `here`_.
    
    .. _here: http://stackoverflow.com/questions/1714027/version-number-comparison
    
    :param version_a: version a
    :type version_a: string
    :param version_b: version b
    :type version_b: string
    :return: 0 if versions are equivalent, < 0 if version_a is lower than version_b
        , > 0 if version_b is lower than version_b
    """
    def normalize(v):
        return [int(x) for x in re.sub(r'(\.0+)*$','', v).split(".")]
        
    return cmp(normalize(version_a), normalize(version_b))

if __name__ == '__main__':
   print version_compare(numpy.version.version, '1.9') < 0

What is your opinion on the code snippet? Is it working? Let me know your thoughts in the comments below or using the following platforms: