Skip to content

Instantly share code, notes, and snippets.

@quatrix
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quatrix/82755d3e4df618122ff5 to your computer and use it in GitHub Desktop.
Save quatrix/82755d3e4df618122ff5 to your computer and use it in GitHub Desktop.
__author__ = 'quatrix'
import re
import unittest
def _get_lowest_version(s):
"""
Takes a string of the following format:
something: [foo-5, foo-2, foo-10, foo-4, foo-5]
and returns the 'foo' with the lowest number, in this case foo-2
"""
regex = r"([^\[\]\s,]+)-(\d+)"
choices = re.findall(regex, s)
choices_sorted = sorted(choices, key=lambda k: int(k[1]))
if not choices_sorted:
return ""
return "{}-{}".format(*choices_sorted[0])
def __get_lowest_version(s):
"""
Takes a string of the following format:
something: [foo-5, foo-2, foo-10, foo-4, foo-5]
and returns the 'foo' with the lowest number, in this case foo-2
"""
choices = [i.strip() for i in s.split("[", 2)[1][:-1].split(",") if i]
if not choices:
return ""
return sorted(choices, key=lambda k: int(k.split("-")[-1]))[0]
def get_lowest_version(s):
"""
Takes a string of the following format:
something: [foo-5, foo-2, foo-10, foo-4, foo-5]
and returns the 'foo' with the lowest number, in this case foo-2
"""
choices = s.translate(None, "[] \n").split(":")[1]
if not choices:
return ""
return sorted(choices.split(","), key=lambda k: int(k.split("-")[-1]))[0]
class LowestVersionTestCase(unittest.TestCase):
def assertOrder(self, input_str, expected):
self.assertEqual(get_lowest_version(input_str), expected)
def test_empty(self):
self.assertOrder("default: []", "")
def test_single_line(self):
self.assertOrder("default: [hey-2, hey-3, hey-1, hey-4]", "hey-1")
def test_single_line_multidigit_versions(self):
self.assertOrder("heh: [va-2234, hey-312, pa-1121, ey-440]", "hey-312")
def test_multiline(self):
self.assertOrder("""default: [hey-2,
hey-0,
hey-1, hey-4
]""", "hey-0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment