Skip to content

Commit

Permalink
remove lookup_index method from StrainCollection (#90)
Browse files Browse the repository at this point in the history
- remove method `lookup_index`
- remove attribute `_strain_dict_index`
  • Loading branch information
CunliangGeng committed May 3, 2023
1 parent 7094d0d commit 0b13746
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 29 deletions.
18 changes: 1 addition & 17 deletions src/nplinker/strain_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import csv
import os
from os import PathLike
from pathlib import Path
from typing import Iterator
Expand All @@ -9,7 +8,6 @@
from .utils import list_dirs
from .utils import list_files


logger = LogConfig.getLogger(__name__)


Expand All @@ -20,7 +18,6 @@ def __init__(self):
self._strains: list[Strain] = []
# dict of strain name (id and alias) to strain object
self._strain_dict_name: dict[str, Strain] = {}
self._strain_dict_index: dict[int, Strain] = {}

def __repr__(self) -> str:
return str(self)
Expand All @@ -38,7 +35,7 @@ def __len__(self) -> int:
def __eq__(self, other) -> bool:
if isinstance(other, StrainCollection):
return (self._strains == other._strains
and self._strain_dict_index == other._strain_dict_index)
and self._strain_dict_name == other._strain_dict_name)
return NotImplemented

def __contains__(self, item: str | Strain) -> bool:
Expand Down Expand Up @@ -74,7 +71,6 @@ def add(self, strain: Strain) -> None:
existing.add_alias(alias)
self._strain_dict_name[alias] = existing
else:
self._strain_dict_index[len(self)] = strain
self._strains.append(strain)
self._strain_dict_name[strain.id] = strain
for alias in strain.aliases:
Expand All @@ -88,7 +84,6 @@ def remove(self, strain: Strain):
"""
if strain.id in self._strain_dict_name:
self._strains.remove(strain)
# remove from dict id
del self._strain_dict_name[strain.id]
for alias in strain.aliases:
del self._strain_dict_name[alias]
Expand All @@ -102,17 +97,6 @@ def filter(self, strain_set: set[Strain]):
if strain not in strain_set:
self.remove(strain)

def lookup_index(self, index: int) -> Strain:
"""Return the strain from lookup by index.
Args:
index(int): Position index from which to retrieve the strain
Returns:
Strain: Strain identified by the given index.
"""
return self._strain_dict_index[index]

def lookup(self, name: str) -> Strain:
"""Lookup a strain by name (id or alias).
Expand Down
13 changes: 1 addition & 12 deletions tests/test_strain_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ def test_iter(collection: StrainCollection, strain: Strain):
for actual in collection:
assert actual == strain


def test_add(strain: Strain):
sut = StrainCollection()
sut.add(strain)
assert strain in sut
for alias in strain.aliases:
assert alias in sut
assert sut._strain_dict_index[0] == strain


def test_remove(collection: StrainCollection, strain: Strain):
Expand All @@ -56,9 +56,6 @@ def test_remove(collection: StrainCollection, strain: Strain):
with pytest.raises(KeyError):
_ = collection._strain_dict_name[strain.id]
assert strain not in collection
# TODO: issue #90
# with pytest.raises(KeyError):
# collection.lookup_index(0)


def test_filter(collection: StrainCollection, strain: Strain):
Expand All @@ -70,13 +67,6 @@ def test_filter(collection: StrainCollection, strain: Strain):
assert len(collection) == 1


def test_lookup_index(collection: StrainCollection, strain: Strain):
actual = collection.lookup_index(0)
assert actual == strain
with pytest.raises(KeyError):
collection.lookup_index(1)


def test_lookup(collection: StrainCollection, strain: Strain):
assert collection.lookup(strain.id) == strain
for alias in strain.aliases:
Expand All @@ -89,7 +79,6 @@ def test_add_from_file():
sut = StrainCollection()
sut.add_from_file(DATA_DIR / "strain_mappings.csv")
assert len(sut) == 27
assert len(sut.lookup_index(1).aliases) == 29


def test_save_to_file(collection: StrainCollection, tmp_path):
Expand Down

0 comments on commit 0b13746

Please sign in to comment.