Rigidity

This module contains the wrapper class that can be used to adapt your CSV parsing code to use rigidity.

Rigidity is a simple wrapper to the built-in csv module that allows for validation and correction of data being read/written from/to CSV files.

This module allows you to easily construct validation and correction rulesets to be applied automatically while preserving the csv interface. This allows you to easily upgrade old software to use new, strict rules.

class rigidity.Rigidity(csvobj, rules=[], display=0)[source]

Bases: object

A wrapper for CSV readers and writers that allows

DISPLAY_NONE = 0

Do not display output at all.

DISPLAY_SIMPLE = 1

Display simple warnings when ValueError is raised by a rule.

skip()[source]

Return a row, skipping validation. This is useful when you want to skip validation of header information.

validate(row)[source]

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 validate_read() or 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.

Parameters:row – a row object that can be passed to a CSVWriter’s writerow() method.
validate_read(row)[source]

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.

Parameters:row – a row object that can be returned from CSVReader’s readrow() method.
validate_write(row)[source]

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.

Parameters:row – a row object that can be passed to a CSVWriter’s __next__() method.
writeheader()[source]

Plain pass-through to the given CSV object. It is assumed that header information is already valid when the CSV object is constructed.

writerow(row)[source]

Validate and correct the data provided in row and raise an exception if the validation or correction fails. Then, write the row to the CSV file.

writerows(rows)[source]

Validate and correct the data provided in every row and raise an exception if the validation or correction fails.

Note

Behavior in the case that the data is invalid and cannot be repaired is undefined. For example, the implementation may choose to write all valid rows up until the error, or it may choose to only conduct the write operation after all rows have been verified. Do not depend on the presence or absence of any of the rows in rows in the event that an exception occurs.