Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] support loading of empty picklists #2106

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/sourmash/picklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def init(self, values=[]):
self.pickset = set(values)
return self.pickset

def load(self, pickfile, column_name):
def load(self, pickfile, column_name, *, allow_empty=False):
"load pickset, return num empty vals, and set of duplicate vals."
pickset = self.init()

Expand All @@ -150,6 +150,7 @@ def load(self, pickfile, column_name):
n_empty_val = 0
dup_vals = set()
with open(pickfile, newline='') as csvfile:
self.pickfile = pickfile
x = csvfile.readline()

# skip leading comment line in case there's a manifest header
Expand All @@ -160,7 +161,10 @@ def load(self, pickfile, column_name):

r = csv.DictReader(csvfile)
if not r.fieldnames:
raise ValueError(f"empty or improperly formatted pickfile '{pickfile}'")
if not allow_empty:
raise ValueError(f"empty or improperly formatted pickfile '{pickfile}'")
else:
return 0, 0

if column_name not in r.fieldnames:
raise ValueError(f"column '{column_name}' not in pickfile '{pickfile}'")
Expand All @@ -180,7 +184,6 @@ def load(self, pickfile, column_name):
else:
self.add(col)

self.pickfile = pickfile
return n_empty_val, dup_vals

def add(self, value):
Expand Down
1 change: 1 addition & 0 deletions tests/test-data/picklist/empty.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

23 changes: 23 additions & 0 deletions tests/test_picklist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Tests for the picklist API.
"""
import pytest
import sourmash

import sourmash_tst_utils as utils
from sourmash.picklist import SignaturePicklist


def test_load_empty_picklist_fail():
empty = utils.get_test_data('picklist/empty.csv')

pl = SignaturePicklist('manifest')
with pytest.raises(ValueError):
pl.load(empty, 'foo', allow_empty=False)


def test_load_empty_picklist_allow():
empty = utils.get_test_data('picklist/empty.csv')

pl = SignaturePicklist('manifest')
pl.load(empty, 'foo', allow_empty=True)