Base class

class fuzzyfields.FuzzyField(*, required: bool = True, default: Any = None, description: str = None, unique: bool = False)

Abstract base class.

Parameters
  • required (bool) – If False, return default if value is “” or “N/A”. If True, ensure that this field has a value,

  • default – Default value to return in case required is False and value is None, NaN, NaT, empty string, “N/A”, or similar (basically anything for which pandas.isnull() returns True, or that pandas.read_csv() interprets as a NaN)

  • description (str) – Optional description for the specific field or property being validated. It should not contain the field name or settings.

  • unique (bool) – Set to True to raise an error in case of duplicate values. When FuzzyField instances are used as class attributes, the uniqueness check is performed across all instances of the owner class and its subclasses.

__delete__(instance) → None

Delete the field value on an instance of the owner class.

__get__(instance, owner) → Any

Retrieve stored value of the property.

Returns

Stored value, or self.default is the stored value is None. When invoked as a class property, return the FuzzyField object itself.

One may wish to postprocess the return value before it is returned. This can be particularly useful when one wants to alter the output of a field depending on the output of other attributes of the instance that may not be available when FuzzyField.validate() is executed. This can be achieved by overriding this method as follows:

>>> from fuzzyfields import String

>>> class Dog(String):
...    def __get__(self, instance, owner):
...        value = super().__get__(instance, owner)
...        if value is self:
...            return self
...
...        return f"{value}, {instance.name}'s dog!'"

>>> class Owner:
...    name = String()
...    dog = Dog()

>>> human = Owner()
>>> human.dog = 'Lassie'
>>> human.name = 'Bob'
>>> human.dog
"Lassie, Bob's dog!"
__init__(*, required: bool = True, default: Any = None, description: str = None, unique: bool = False)

Initialize self. See help(type(self)) for accurate signature.

__repr__() → str

Fancy print the description of the fuzzyfield and all the relevant settings. Used when building the docstring of the owner class.

Internally invokes FuzzyField.sphinxdoc().

__set__(instance, value) → None

Store value of the property for parent object. Can be used in two ways:

  • with a regular value to be validated

  • with a new instance of another FuzzyField. This way one can override settings with instance-specific ones.

__set_name__(owner, name: str) → None

Called at the time the owner class is created. The descriptor has been assigned to name.

__weakref__

list of weak references to the object (if defined)

copy()

Shallow copy of self. The seen_values set is recreated as an empty set.

name = None

Name of the field being validated. This is set automatically:

owner = None

The class to which the FuzzyField is attached to as a descriptor. None when used within the DictReader framework.

parse(value: Any) → Any

On-the fly parsing and validation for a local variable.

This is a wrapper around preprocess() -> validate() -> postprocess().

Parameters

value – Raw value to be preprocessed and validated

Returns

Fully preprocessed value, or self.default if the value is null-like and required=False

postprocess(value: Any) → Any

Post-process the value after validating it and before storing it. This method is invoked after FuzzyField.validate() and tests the required and unique flags.

Raises
static preprocess(value: Any) → Any

Perform initial cleanup of a raw input value. This method is automatically invoked before FuzzyField.validate().

Parameters

value – raw input value

Returns

the argument, stripped of leading and trailing whitespace and carriage returns if it is a string. If the argument is null, return None. Otherwise return the argument unaltered.

seen_values = None

Record of already encountered values. This attribute only exists if unique=True.

property sphinxdoc

Virtual property - to be overridden. Automated documentation that will appear in Sphinx. It should not include the name, owner, required, default, unique, or description attributes.

validate(value: Any) → Any

Virtual method - to be overridden. Validate and reformat value. This method is invoked when processing a new value, after FuzzyField.preprocess() and before FuzzyField.postprocess(), but only if the value is not None after preprocess.

Parameters

value – Input data, already preprocessed by FuzzyField.preprocess(). Object type could be anything and should be either tested or carefully handled through duck-typing.

Returns

Reformatted value, or None if default is to be used.

Note

Do not return self.default. This is left to postprocess(). Instead, for any value that equates to null/blank, always return None.

Raises

MalformedFieldError, FieldTypeError – if the value is not valid