Skip to content

Commit

Permalink
Prefer exact matches
Browse files Browse the repository at this point in the history
  • Loading branch information
jacebrowning committed Apr 15, 2023
1 parent 0671d18 commit 3c21732
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
# 3.0 (2023-04-15)

- Dropped support for Python 3.7.
- Updated computer matching to prefer exact matches over partials.

# 2.0 (2022-08-16)

- Dropped support for Python 3.6.
Expand Down
7 changes: 5 additions & 2 deletions mine/models/computer.py
Expand Up @@ -57,12 +57,15 @@ def find(self, name):
return computer
return None

def match(self, partial):
def match(self, partial: str):
"""Find a computer with a similar name."""
log.debug("Finding computer similar to '%s'...", partial)
matches = []
for computer in self:
if partial.lower() in computer.name.lower():
return computer
matches.append(computer)
if matches:
return min(matches, key=lambda computer: len(computer.name))
return None

def get_current(self):
Expand Down
12 changes: 11 additions & 1 deletion mine/tests/test_models_computer.py
Expand Up @@ -63,7 +63,12 @@ class TestComputers:
"""Unit tests for lists of computers."""

computers = Computers(
[Computer("abc", "abc.local", 1), Computer("def", "def.local", 2)]
[
Computer("abc", "abc.local", 1),
Computer("def", "def.local", 2),
Computer("My iMac", "imac.local", 3),
Computer("My Mac", "mac.local", 4),
]
)

def test_get(self):
Expand All @@ -81,6 +86,11 @@ def test_match(self):
computer = self.computers.match("AB")
assert "abc" == computer.name

def test_match_partial(self):
"""Verify an exact match is preferred over partial."""
computer = self.computers.match("mac")
assert "My Mac" == computer.name

def test_generate_name(self):
"""Verify a computer name is generated correctly."""
computers = Computers()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
@@ -1,7 +1,7 @@
[tool.poetry]

name = "mine"
version = "2.1b1"
version = "3.0"
description = "Share application state across computers using Dropbox."

license = "MIT"
Expand Down

0 comments on commit 3c21732

Please sign in to comment.