MCPcopy
hub / github.com/corpnewt/GenSMBIOS / compare_versions

Method compare_versions

Scripts/utils.py:45–82  ·  view source on GitHub ↗
(self, vers1, vers2, **kwargs)

Source from the content-addressed store, hash-verified

43 exit(1)
44
45 def compare_versions(self, vers1, vers2, **kwargs):
46 # Helper method to compare ##.## strings
47 #
48 # vers1 < vers2 = True
49 # vers1 = vers2 = None
50 # vers1 > vers2 = False
51
52 # Sanitize the pads
53 pad = str(kwargs.get("pad", ""))
54 sep = str(kwargs.get("separator", "."))
55
56 ignore_case = kwargs.get("ignore_case", True)
57
58 # Cast as strings
59 vers1 = str(vers1)
60 vers2 = str(vers2)
61
62 if ignore_case:
63 vers1 = vers1.lower()
64 vers2 = vers2.lower()
65
66 # Split and pad lists
67 v1_parts, v2_parts = self.pad_length(vers1.split(sep), vers2.split(sep))
68
69 # Iterate and compare
70 for i in range(len(v1_parts)):
71 # Remove non-numeric
72 v1 = ''.join(c.lower() for c in v1_parts[i] if c.isalnum())
73 v2 = ''.join(c.lower() for c in v2_parts[i] if c.isalnum())
74 # Equalize the lengths
75 v1, v2 = self.pad_length(v1, v2)
76 # Compare
77 if str(v1) < str(v2):
78 return True
79 elif str(v1) > str(v2):
80 return False
81 # Never differed - return None, must be equal
82 return None
83
84 def pad_length(self, var1, var2, pad = "0"):
85 # Pads the vars on the left side to make them equal length

Callers 1

mainMethod · 0.80

Calls 1

pad_lengthMethod · 0.95

Tested by

no test coverage detected