Skip to content

Commit

Permalink
Added validate_read() and validate_write() methods. This will allow
Browse files Browse the repository at this point in the history
bi-directional rules in the near future.
  • Loading branch information
austinhartzheim committed Aug 11, 2015
1 parent 61b55a7 commit 5a1b519
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion rigidity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,70 @@ def writerows(self, rows):

# New methods, not part of the `csv` interface
def validate(self, row):
'''
.. warning::
This method is deprecated and will be removed in a future
release; it is included only to support old code. It will
not produce consistent results with bi-directional rules.
You should use :meth:`validate_read` or
:meth:`validate_write` instead.
Validate that the row conforms with the specified rules,
correcting invalid rows where the rule is able to do so.
If the row is valid or can be made valid through corrections,
this method will return a row that can be written to the CSV
file. If the row is invalid and cannot be corrected, then this
method will raise an exception.
:param row: a row object that can be passed to a CSVWriter's
writerow() method.
'''
# Ensure mutability - I'm looking at you, tuples!
if not isinstance(row, (list, dict)):
row = list(row)

# Iterate through all keys, updating the data
for key in self.keys:
value = row[key]
for rule in self.rules[key]:
if hasattr(rule, 'apply'):
value = rule.apply(value)
else:
return rule.read(value)
row[key] = value

# Return the updated data
return row

def validate_write(self, row):
'''
Validate that the row conforms with the specified rules,
correcting invalid rows where the rule is able to do so.
If the row is valid or can be made valid through corrections,
this method will return a row that can be written to the CSV
file. If the row is invalid and cannot be corrected, then this
method will raise an exception.
:param row: a row object that can be passed to a CSVWriter's
writerow() method.
'''
# Ensure mutability - I'm looking at you, tuples!
if not isinstance(row, (list, dict)):
row = list(row)

# Iterate through all keys, updating the data
for key in self.keys:
value = row[key]
for rule in self.rules[key]:
value = rule.write(value)
row[key] = value

# Return the updated data
return row

def validate_read(self, row):
'''
Validate that the row conforms with the specified rules,
correcting invalid rows where the rule is able to do so.
Expand All @@ -97,7 +161,7 @@ def validate(self, row):
for key in self.keys:
value = row[key]
for rule in self.rules[key]:
value = rule.apply(value)
value = rule.read(value)
row[key] = value

# Return the updated data
Expand Down

0 comments on commit 5a1b519

Please sign in to comment.