it's 2017; provide a sorting key function
This commit is contained in:
parent
520d7cbf3a
commit
bf4295893b
|
@ -120,11 +120,11 @@ Compare two arbitrary version strings
|
||||||
>>> Dpkg.compare_versions(ver_1, ver_2)
|
>>> Dpkg.compare_versions(ver_1, ver_2)
|
||||||
-1
|
-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
|
>>> 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']
|
['0:1.0-test1', '0:1.0-test2', '1:0.0-test0']
|
||||||
|
|
||||||
Use the `dpkg-inspect.py` script to inspect packages
|
Use the `dpkg-inspect.py` script to inspect packages
|
||||||
|
|
|
@ -13,6 +13,7 @@ import tarfile
|
||||||
from gzip import GzipFile
|
from gzip import GzipFile
|
||||||
from hashlib import md5, sha1, sha256
|
from hashlib import md5, sha1, sha256
|
||||||
from email import message_from_string as Message
|
from email import message_from_string as Message
|
||||||
|
from functools import cmp_to_key
|
||||||
|
|
||||||
# pypi imports
|
# pypi imports
|
||||||
import six
|
import six
|
||||||
|
@ -520,3 +521,17 @@ class Dpkg(object):
|
||||||
# at this point, the versions are equal, but due to an interpolated
|
# at this point, the versions are equal, but due to an interpolated
|
||||||
# zero in either the epoch or the debian version
|
# zero in either the epoch or the debian version
|
||||||
return 0
|
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)
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -1,6 +1,6 @@
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
|
|
||||||
__VERSION__ = '1.2.0'
|
__VERSION__ = '1.2.1'
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='pydpkg',
|
name='pydpkg',
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
from functools import cmp_to_key
|
|
||||||
from email.message import Message
|
from email.message import Message
|
||||||
|
|
||||||
from pydpkg import Dpkg, DpkgVersionError
|
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
|
# http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
sorted(['a', '', '~', '~~a', '~~'],
|
sorted(['a', '', '~', '~~a', '~~'],
|
||||||
key=cmp_to_key(Dpkg.dstringcmp)),
|
key=Dpkg.dstringcmp_key),
|
||||||
['~~', '~~a', '~', '', 'a'])
|
['~~', '~~a', '~', '', 'a'])
|
||||||
|
|
||||||
def test_compare_revision_strings(self):
|
def test_compare_revision_strings(self):
|
||||||
|
|
Loading…
Reference in New Issue