Skip to content

Commit

Permalink
Proof of Concept: custom fieldname support
Browse files Browse the repository at this point in the history
This is a proof of concept to support validating CSVs that don't include
a header row. The implementation allows a user to define a sequence of
field names and passes these through to the underlying DictReader
instances. The default behavior remains unchanged. I did a quick manual
test with a slight modification of the example Vlad object and sample
CSV contents from the readme.

If this is a useful construct, I'll be happy to fill in the
documentation and tests for this option.

Resolves #88.
  • Loading branch information
jonafato committed Mar 9, 2024
1 parent d831d62 commit d27ea52
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions vladiate/vlad.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
file_validation_failure_threshold=None,
quiet=False,
row_validators=[],
fieldnames=None,
):
self.logger = logs.logger
self.failures = defaultdict(lambda: defaultdict(list))
Expand All @@ -26,6 +27,7 @@ def __init__(
self.source = source
self.validators = validators or getattr(self, "validators", {})
self.row_validators = row_validators or getattr(self, "row_validators", [])
self.fieldnames = fieldnames or getattr(self, "fieldnames", None)
self.delimiter = delimiter or getattr(self, "delimiter", ",")
self.line_count = 0
self.ignore_missing_validators = ignore_missing_validators
Expand Down Expand Up @@ -124,15 +126,15 @@ def _log_missing(self, missing_items):
)

def _get_total_lines(self):
reader = csv.DictReader(self.source.open(), delimiter=self.delimiter)
reader = csv.DictReader(self.source.open(), delimiter=self.delimiter, fieldnames=self.fieldnames)
self.total_lines = sum(1 for _ in reader)
return self.total_lines

def validate(self):
self.logger.info(
"\nValidating {}(source={})".format(self.__class__.__name__, self.source)
)
reader = csv.DictReader(self.source.open(), delimiter=self.delimiter)
reader = csv.DictReader(self.source.open(), delimiter=self.delimiter, fieldnames=self.fieldnames)

if not reader.fieldnames:
self.logger.info(
Expand Down

0 comments on commit d27ea52

Please sign in to comment.