Skip to content

Commit 424f203

Browse files
committed
Simplify compare functions for sorting
1 parent 3b00538 commit 424f203

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

lib/pyld/jsonld.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,12 @@
2626
from .context_resolver import ContextResolver
2727
from c14n.Canonicalize import canonicalize
2828
from cachetools import LRUCache
29-
from collections import namedtuple
30-
from functools import cmp_to_key
3129
import lxml.html
3230
from numbers import Integral, Real
3331
from frozendict import frozendict
3432
from pyld.__about__ import (__copyright__, __license__, __version__)
3533
from .iri_resolver import resolve, unresolve
3634

37-
def cmp(a, b):
38-
return (a > b) - (a < b)
39-
4035
__all__ = [
4136
'__copyright__', '__license__', '__version__',
4237
'compact', 'expand', 'flatten', 'frame', 'link', 'from_rdf', 'to_rdf',
@@ -4663,7 +4658,7 @@ def _compact_iri(
46634658
# lexicographically less than the current choice
46644659
if (is_usable_curie and (
46654660
candidate is None or
4666-
_compare_shortest_least(curie, candidate) < 0)):
4661+
(len(curie), curie) < (len(candidate), candidate))):
46674662
candidate = curie
46684663

46694664
# return curie candidate
@@ -5353,8 +5348,8 @@ def _get_inverse_context(self, active_ctx):
53535348
# create term selections for each mapping in the context, ordered by
53545349
# shortest and then lexicographically least
53555350
for term, mapping in sorted(
5356-
active_ctx['mappings'].items(),
5357-
key=cmp_to_key(_compare_shortest_least)):
5351+
active_ctx['mappings'].items(),
5352+
key=lambda kv: (len(kv[0]), kv[0])):
53585353
if mapping is None or not mapping.get('@id'):
53595354
continue
53605355

@@ -5649,8 +5644,7 @@ def main(self, dataset, options):
56495644

56505645
# 6.3) For each result in the hash path list,
56515646
# lexicographically-sorted by the hash in result:
5652-
cmp_hashes = cmp_to_key(lambda x, y: cmp(x['hash'], y['hash']))
5653-
for result in sorted(hash_path_list, key=cmp_hashes):
5647+
for result in sorted(hash_path_list, key=lambda r: r['hash']):
56545648
# 6.3.1) For each blank node identifier, existing identifier,
56555649
# that was issued a temporary identifier by identifier issuer
56565650
# in result, issue a canonical identifier, in the same order,
@@ -6060,19 +6054,7 @@ def permutations(elements):
60606054
left[elements[i]] = not left[elements[i]]
60616055

60626056

6063-
def _compare_shortest_least(a, b):
6064-
"""
6065-
Compares two strings first based on length and then lexicographically.
6066-
6067-
:param a: the first string.
6068-
:param b: the second string.
60696057

6070-
:return: -1 if a < b, 1 if a > b, 0 if a == b.
6071-
"""
6072-
rval = cmp(len(a), len(b))
6073-
if rval == 0:
6074-
rval = cmp(a, b)
6075-
return rval
60766058

60776059

60786060
def _is_keyword(v):

0 commit comments

Comments
 (0)