it's 2017; provide a sorting key function

This commit is contained in:
Nathan J. Mehl 2017-06-13 14:02:49 -07:00
parent 520d7cbf3a
commit bf4295893b
4 changed files with 19 additions and 5 deletions

View File

@ -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

View File

@ -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)

View File

@ -1,6 +1,6 @@
from distutils.core import setup
__VERSION__ = '1.2.0'
__VERSION__ = '1.2.1'
setup(
name='pydpkg',

View File

@ -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):