Rules

This submodule contains the built-in rules that are used for filtering and modifying data.

class rigidity.rules.Boolean(allow_null=False, action=1, default=None)[source]

Bases: rigidity.rules.Rule

Cast a string as a boolean value.

ACTION_DEFAULT = 2

When invalid data is encountered, return a set defaut value.

ACTION_DROPROW = 3

When invalid data is encountered, drop the row.

ACTION_ERROR = 1

When invalid data is encountered, raise an exception.

__init__(allow_null=False, action=1, default=None)[source]
Parameters:action – take the behavior indicated by ACTION_ERROR, ACTION_DEFAULT, or ACTION_DROPROW.
class rigidity.rules.Bytes(encoding='utf8')[source]

Bases: rigidity.rules.Rule

When reading data, encode it as a bytes object using the given encoding. When writing data, decode it using the given encoding.

class rigidity.rules.CapitalizeWords(seperators=' tnr', cap_first=True)[source]

Bases: rigidity.rules.Rule

Capitalize words in a string. By default, words are detected by searching for space, tab, new line, and carriage return characters. You may override this setting.

Also, by default, the first character is capitalized automatically.

__init__(seperators=' \t\n\r', cap_first=True)[source]
Parameters:
  • seperators (str) – capitalize any character following a character in this string.
  • cap_first (bool) – automatically capitalize the first character in the string.
class rigidity.rules.Contains(string)[source]

Bases: rigidity.rules.Rule

Check that a string field value contains the string (or all strings in a list of strings) passed as a parameter to this rule.

class rigidity.rules.Drop[source]

Bases: rigidity.rules.Rule

Drop the data in this column, replacing all data with an empty string value.

class rigidity.rules.Float(action=1)[source]

Bases: rigidity.rules.Rule

Cast all data to floats or die trying.

ACTION_DROPROW = 3

When invalid data is encountered, drop the row.

ACTION_ERROR = 1

When invalid data is encountered, raise an exception.

ACTION_ZERO = 2

When invalid data is encountered, return zero.

__init__(action=1)[source]
Parameters:action – take the behavior indicated by ACTION_ERROR, ACTION_ZERO, or ACTION_DROPROW.
class rigidity.rules.Integer(action=1)[source]

Bases: rigidity.rules.Rule

Cast all data to ints or die trying.

ACTION_DROPROW = 3

When invalid data is encountered, drop the row.

ACTION_ERROR = 1

When invalid data is encountered, raise an exception.

ACTION_ZERO = 2

When invalid data is encountered, return zero.

__init__(action=1)[source]
Parameters:action – take the behavior indicated by ACTION_ERROR, ACTION_ZERO, or ACTION_DROPROW.
class rigidity.rules.Lower[source]

Bases: rigidity.rules.Rule

Convert a string value to lower-case.

class rigidity.rules.NoneToEmptyString[source]

Bases: rigidity.rules.Rule

Replace None values with an empty string. This is useful in cases where legacy software uses None to create an empty cell, but your other checks require a string.

class rigidity.rules.RemoveLinebreaks[source]

Bases: rigidity.rules.Rule

Remove linebreaks from the start and end of field values. These can sometimes be introduced into files and create problems for humans because they are invisible.to human users.

class rigidity.rules.ReplaceValue(replacements={}, missing_action=4, default_value='')[source]

Bases: rigidity.rules.Rule

Check if the value has a specified replacement. If it does, replace it with that value. If it does not, take one of the following configurable actions: pass it through unmodified, drop the row, or use a default value.

ACTION_BLANK = 5

When no replacement is found, return an empty string.

ACTION_DEFAULT_VALUE = 2

When no replacement is found, return a set default value.

ACTION_DROP = 5

Warning

ACTION_DROP is deprecated due to the name being similar to ACTION_DROPROW. Use ACTION_BLANK instead.

ACTION_DROPROW = 1

When no replacement is found, drop the row.

ACTION_ERROR = 4

When no replacement is found, raise an exception.

ACTION_PASSTHROUGH = 3

When no replacement is found, allow the original to pass through.

__init__(replacements={}, missing_action=4, default_value='')[source]
Parameters:
  • replacements (dict) – a mapping between original values and replacement values.
  • missing_action – when a replacement is not found for a value, take the behavior specified by the specified value, such as ACTION_DROP, ACTION_DEFAULT_VALUE, ACTION_PASSTHROUGH, or ACTION_ERROR.
  • default_value – if ACTION_DEFAULT_VALUE is the missing replacement behavior, use this variable as the default replacement value.
class rigidity.rules.Rule[source]

Base rule class implementing a simple apply() method that returns the given data unchanged.

apply(value)[source]

This is the default method for applying a rule to data. By default, the read() and write() methods will use this method to validate and modify data.

Parameters:value – the data to be validated.
Returns:the validated and possibly modified value as documented by the rule.
Raises rigidity.errors.DropRow:
 when the rule wants to cancel processing of an entire row, it may do so with the DropRow error. This signifies to the rigidity.Rigidity class that it should discontinue processing the row.
read(value)[source]

When reading data, it is validated with this method. By default, this method calls the apply() method of this class. However, you may override this method to achieve different behavior when reading and writing.

Parameters:value – the data to be validated.
Returns:the validated and possibly modified value as documented by the rule.
Raises rigidity.errors.DropRow:
 when the rule wants to cancel processing of an entire row, it may do so with the DropRow error. This signifies to the rigidity.Rigidity class that it should discontinue processing the row.
write(value)[source]

When writing data, it is validated with this method. By default, this method calls the apply() method of this class. However, you may override this method to achieve different behavior when reading and writing.

Parameters:value – the data to be validated.
Returns:the validated and possibly modified value as documented by the rule.
Raises rigidity.errors.DropRow:
 when the rule wants to cancel processing of an entire row, it may do so with the DropRow error. This signifies to the rigidity.Rigidity class that it should discontinue processing the row.
class rigidity.rules.Static(value)[source]

Bases: rigidity.rules.Rule

Replace a field’s value with a static value declared during initialization.

class rigidity.rules.Strip(chars=None)[source]

Bases: rigidity.rules.Rule

Strip excess white space from the beginning and end of a value.

class rigidity.rules.Unique(action=1)[source]

Bases: rigidity.rules.Rule

Only allow unique values to pass. When a repeated value is found, the row may be dropped or an error may be raised.

ACTION_DROPROW = 2

When repeat data is encountered, drop the row.

ACTION_ERROR = 1

When repeat data is encountered, raise an exception.

__init__(action=1)[source]
Parameters:action – Accepts either ACTION_ERROR or ACTION_DROPROW as the behavior to be performed when a value is not unique.
class rigidity.rules.UpcA(strict=False)[source]

Bases: rigidity.rules.Rule

Validate UPC-A barscode numbers to ensure that they are 12 digits. Strict validation of the check digit may also be enabled.

__init__(strict=False)[source]
Parameters:strict (bool) – If true, raise a ValueError if the given UPC code fails the check digit validation.
apply(value)[source]

Cast the value to a string, then check that it is numeric. Afterwards, zero-pad the left side to reach the standard length of 12 digits.

Raises ValueError:
 when strict mode is enabled and the given UPC code fails the check digit validation.
class rigidity.rules.Upper[source]

Bases: rigidity.rules.Rule

Convert a string value to upper-case.