MCPcopy Create free account
hub / github.com/numpy/numpy / _cmpkey

Function _cmpkey

numpy/_utils/_pep440.py:437–487  ·  view source on GitHub ↗
(epoch, release, pre, post, dev, local)

Source from the content-addressed store, hash-verified

435
436
437def _cmpkey(epoch, release, pre, post, dev, local):
438 # When we compare a release version, we want to compare it with all of the
439 # trailing zeros removed. So we'll use a reverse the list, drop all the now
440 # leading zeros until we come to something non-zero, then take the rest,
441 # re-reverse it back into the correct order, and make it a tuple and use
442 # that for our sorting key.
443 release = tuple(
444 reversed(list(
445 itertools.dropwhile(
446 lambda x: x == 0,
447 reversed(release),
448 )
449 ))
450 )
451
452 # We need to "trick" the sorting algorithm to put 1.0.dev0 before 1.0a0.
453 # We'll do this by abusing the pre-segment, but we _only_ want to do this
454 # if there is no pre- or a post-segment. If we have one of those, then
455 # the normal sorting rules will handle this case correctly.
456 if pre is None and post is None and dev is not None:
457 pre = -Infinity
458 # Versions without a pre-release (except as noted above) should sort after
459 # those with one.
460 elif pre is None:
461 pre = Infinity
462
463 # Versions without a post-segment should sort before those with one.
464 if post is None:
465 post = -Infinity
466
467 # Versions without a development segment should sort after those with one.
468 if dev is None:
469 dev = Infinity
470
471 if local is None:
472 # Versions without a local segment should sort before those with one.
473 local = -Infinity
474 else:
475 # Versions with a local segment need that segment parsed to implement
476 # the sorting rules in PEP440.
477 # - Alphanumeric segments sort before numeric segments
478 # - Alphanumeric segments sort lexicographically
479 # - Numeric segments sort numerically
480 # - Shorter versions sort before longer versions when the prefixes
481 # match exactly
482 local = tuple(
483 (i, "") if isinstance(i, int) else (-Infinity, i)
484 for i in local
485 )
486
487 return epoch, release, pre, post, dev, local

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected