API Docs: Fields

class ogitm.fields.BaseField(**kwargs)[source]

Abstract Base Class for field types.

Cannot be instantiated, but should be inherited to provide all the useful information that a field might need.

Parameters:
  • default (any) – A default value to provide if the input is ever None. If not provided, and nullable is False, a field will not accept None as an argument.
  • nullable (bool) – True if this field can be None/null, False otherwise. Defaults to True.
  • coerce

    A function that can coerce any input into input of a valid type. If it cannot coerce, it should either return “False” or raise a ValueError. Defaults to a no-op.

    Example: coerce=int would convert values to int where possible.

check(val)[source]

Base case method to check if a value is allowed by this field.

Must be overriden. Currently only returns True, but may do its own checking in future, and so should probably be checked before any overriden method.

Parameters:val (any) – Value to check
Returns:Whether that value is allowed by the parameters given to this field.
coerce(val)[source]

Attempt to coerce a value using the pre-defined function.

If no function was passed in, the default operation is to return the value straight through. If the function fails to coerce (i.e. raises ValueError), the value is returned unchanged. (type_check should therefore always be used to check the type of a coerced value.)

Parameters:val (any) – Value to coerce
Returns:Coerced value
type_check(val, typ=None)[source]

Check if value is of a certain type (using nullability).

If this field instance can be nulled, checks if the val is either of type typ or of the None type. Otherwise, it just checks if the val is of type typ. Note that typ is passed straight through to isinstance, so it can be any value allowed by the second parameter of isinstance.

Parameters:
  • val (any) – Value to check
  • typ – Type(s) to check against
Returns:

Whether val is of type typ.

class ogitm.fields.String(**kwargs)[source]

A field representing string types.

Parameters:regex (string or regex) – Regular expression that this string must match. If not present, any string will match. Can be either a regular expression object, or a string.
class ogitm.fields.Number(**kwargs)[source]

A field representing real numeric types.

Parameters:
  • min (numeric) – The minimum (inclusive) value that this field can contain. If not specified, there is no minimum.
  • max (numeric) – The maximum (inclusive) value that this field can contain. If not specified, there is no maximum.
class ogitm.fields.Float(**kwargs)[source]

A field representing floating point numbers.

Parameters:
class ogitm.fields.Integer(**kwargs)[source]

A field representing integers.

Parameters:
class ogitm.fields.Boolean(**kwargs)[source]

A field representing boolean values

See coerce_boolean() for a useful coercion function for this field.

ogitm.fields.coerce_boolean(val)[source]

A useful function for coercing various types to boolean.

Unlike the usual Python bool() function which simply tests if a value is empty, this matches boolean True, strings in the set {'yes', 'y', 'true', 't', 'on'} and the integer 1 for True, or boolean False, strings in the set {'no', 'n', 'false', 'f', 'off'} and the integer 0 for False.

This is done in a case-insensitive manner. If the value is a string not in the described sets, a number that doesn’t equal 1 or 0, or any other type (excepting boolean of course), this function will raise ValueError.

class ogitm.fields.Choice(choices=None, **kwargs)[source]

A field representing a single item from a set of items.

Parameters:choices (collection) – A required collection of items. The check method will then ensure that the value must be in this collection.