Skip to content

Commit

Permalink
Use constant for valid delimiters
Browse files Browse the repository at this point in the history
Avoids re-defining this list at each use case and prevents them from
getting out of sync.
  • Loading branch information
victorlin committed Mar 31, 2023
1 parent 70d8150 commit 10bf2bc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions augur/io/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from .file import open_file


# List of valid delimiters when reading a metadata file.
VALID_DELIMITERS = (',', '\t')


def read_metadata(metadata_file, id_columns=("strain", "name"), chunk_size=None):
"""Read metadata from a given filename and into a pandas `DataFrame` or
`TextFileReader` object.
Expand Down Expand Up @@ -135,7 +139,6 @@ def read_table_to_dict(table, duplicate_reporting=DataErrorMethod.ERROR_FIRST, i
2. The provided *id_column* does not exist in the *metadata*
3. The *duplicate_reporting* method is set to ERROR_FIRST or ERROR_ALL and duplicate(s) are found
"""
valid_delimiters = [',', '\t']
seen_ids = set()
duplicate_ids = set()
with open_file(table) as handle:
Expand All @@ -151,7 +154,7 @@ def read_table_to_dict(table, duplicate_reporting=DataErrorMethod.ERROR_FIRST, i
try:
# Note: this sort of duplicates _get_delimiter(), but it's easier if
# this is separate since it handles non-seekable buffers.
dialect = csv.Sniffer().sniff(table_sample, valid_delimiters)
dialect = csv.Sniffer().sniff(table_sample, VALID_DELIMITERS)
except csv.Error as err:
raise AugurError(
f"Could not determine the delimiter of {table!r}. "
Expand Down Expand Up @@ -435,7 +438,7 @@ def _get_delimiter(path: str):
with open_file(path) as file:
try:
# Infer the delimiter from the first line.
return csv.Sniffer().sniff(file.readline(), delimiters=[',', '\t']).delimiter
return csv.Sniffer().sniff(file.readline(), VALID_DELIMITERS).delimiter
except csv.Error as err:
raise AugurError(
f"Could not determine the delimiter of {path!r}. "
Expand Down

0 comments on commit 10bf2bc

Please sign in to comment.