@@ -375,7 +375,8 @@ def compare(self, other):
375
375
"""
376
376
Compare self with other.
377
377
378
- :param other: second version (can be string or a VersionInfo instance)
378
+ :param other: the second version (can be string, a dict, tuple/list, or
379
+ a VersionInfo instance)
379
380
:return: The return value is negative if ver1 < ver2,
380
381
zero if ver1 == ver2 and strictly positive if ver1 > ver2
381
382
:rtype: int
@@ -386,10 +387,16 @@ def compare(self, other):
386
387
1
387
388
>>> semver.VersionInfo.parse("2.0.0").compare("2.0.0")
388
389
0
390
+ >>> semver.VersionInfo.parse("2.0.0").compare(dict(major=2, minor=0, patch=0))
391
+ 0
389
392
"""
390
393
cls = type (self )
391
394
if isinstance (other , str ):
392
395
other = cls .parse (other )
396
+ elif isinstance (other , dict ):
397
+ other = cls (** other )
398
+ elif isinstance (other , (tuple , list )):
399
+ other = cls (* other )
393
400
elif not isinstance (other , cls ):
394
401
raise TypeError (
395
402
"Expected str or {} instance, but got {}" .format (
@@ -417,27 +424,27 @@ def compare(self, other):
417
424
418
425
@comparator
419
426
def __eq__ (self , other ):
420
- return _compare_by_keys ( self .to_dict (), _to_dict ( other ) ) == 0
427
+ return self .compare ( other ) == 0
421
428
422
429
@comparator
423
430
def __ne__ (self , other ):
424
- return _compare_by_keys ( self .to_dict (), _to_dict ( other ) ) != 0
431
+ return self .compare ( other ) != 0
425
432
426
433
@comparator
427
434
def __lt__ (self , other ):
428
- return _compare_by_keys ( self .to_dict (), _to_dict ( other ) ) < 0
435
+ return self .compare ( other ) < 0
429
436
430
437
@comparator
431
438
def __le__ (self , other ):
432
- return _compare_by_keys ( self .to_dict (), _to_dict ( other ) ) <= 0
439
+ return self .compare ( other ) <= 0
433
440
434
441
@comparator
435
442
def __gt__ (self , other ):
436
- return _compare_by_keys ( self .to_dict (), _to_dict ( other ) ) > 0
443
+ return self .compare ( other ) > 0
437
444
438
445
@comparator
439
446
def __ge__ (self , other ):
440
- return _compare_by_keys ( self .to_dict (), _to_dict ( other ) ) >= 0
447
+ return self .compare ( other ) >= 0
441
448
442
449
def __repr__ (self ):
443
450
s = ", " .join ("%s=%r" % (key , val ) for key , val in self .to_dict ().items ())
@@ -651,25 +658,6 @@ def cmp_prerelease_tag(a, b):
651
658
return cmp (len (a ), len (b ))
652
659
653
660
654
- def _compare_by_keys (d1 , d2 ):
655
- for key in ["major" , "minor" , "patch" ]:
656
- v = cmp (d1 .get (key ), d2 .get (key ))
657
- if v :
658
- return v
659
-
660
- rc1 , rc2 = d1 .get ("prerelease" ), d2 .get ("prerelease" )
661
- rccmp = _nat_cmp (rc1 , rc2 )
662
-
663
- if not rccmp :
664
- return 0
665
- if not rc1 :
666
- return 1
667
- elif not rc2 :
668
- return - 1
669
-
670
- return rccmp
671
-
672
-
673
661
@deprecated (version = "2.10.0" )
674
662
def compare (ver1 , ver2 ):
675
663
"""
@@ -689,9 +677,7 @@ def compare(ver1, ver2):
689
677
0
690
678
"""
691
679
v1 = VersionInfo .parse (ver1 )
692
- # v2 = VersionInfo.parse(ver2)
693
680
return v1 .compare (ver2 )
694
- # return _compare_by_keys(v1, v2)
695
681
696
682
697
683
@deprecated (version = "2.10.0" )
0 commit comments