diff --git a/README.md b/README.md index 6b47411..0c9c863 100644 --- a/README.md +++ b/README.md @@ -120,11 +120,11 @@ Compare two arbitrary version strings >>> Dpkg.compare_versions(ver_1, ver_2) -1 -Use as a cmp function to sort a list of version strings +Use as a key function to sort a list of version strings ------------------------------------------------------- >>> from pydpkg import Dpkg - >>> sorted(['0:1.0-test1', '1:0.0-test0', '0:1.0-test2'] , cmp=Dpkg.compare_versions) + >>> sorted(['0:1.0-test1', '1:0.0-test0', '0:1.0-test2'] , key=Dpkg.compare_versions_key) ['0:1.0-test1', '0:1.0-test2', '1:0.0-test0'] Use the `dpkg-inspect.py` script to inspect packages diff --git a/pydpkg/__init__.py b/pydpkg/__init__.py index 43ae251..37c89b8 100644 --- a/pydpkg/__init__.py +++ b/pydpkg/__init__.py @@ -13,6 +13,7 @@ import tarfile from gzip import GzipFile from hashlib import md5, sha1, sha256 from email import message_from_string as Message +from functools import cmp_to_key # pypi imports import six @@ -520,3 +521,17 @@ class Dpkg(object): # at this point, the versions are equal, but due to an interpolated # zero in either the epoch or the debian version return 0 + + @staticmethod + def compare_versions_key(x): + """Uses functools.cmp_to_key to convert the compare_versions() + function to a function suitable to passing to sorted() and friends + as a key.""" + return cmp_to_key(Dpkg.compare_versions)(x) + + @staticmethod + def dstringcmp_key(x): + """Uses functools.cmp_to_key to convert the dstringcmp() + function to a function suitable to passing to sorted() and friends + as a key.""" + return cmp_to_key(Dpkg.dstringcmp)(x) diff --git a/setup.py b/setup.py index 373dd2e..2ba9434 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from distutils.core import setup -__VERSION__ = '1.2.0' +__VERSION__ = '1.2.1' setup( name='pydpkg', diff --git a/tests/test_dpkg.py b/tests/test_dpkg.py index 7f37e12..ec74a2d 100644 --- a/tests/test_dpkg.py +++ b/tests/test_dpkg.py @@ -2,7 +2,6 @@ import os import unittest -from functools import cmp_to_key from email.message import Message from pydpkg import Dpkg, DpkgVersionError @@ -100,7 +99,7 @@ class DpkgVersionsTest(unittest.TestCase): # http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version self.assertEqual( sorted(['a', '', '~', '~~a', '~~'], - key=cmp_to_key(Dpkg.dstringcmp)), + key=Dpkg.dstringcmp_key), ['~~', '~~a', '~', '', 'a']) def test_compare_revision_strings(self):