Record

knackpy.record #

Record Objects #

class Record(MutableMapping)

A dict-like object for storing record data.

__init__ #

def __init__(data, field_defs, identifier, timezone)

A bunch of side effects happen on initialization:

  • timestamps are corrected
  • Field classes are constructed for each key/value in the record
  • the Record is set to immutable, preventing further updates to record values via __setitem__.

Arguments:

  • data dict - A single Knack record, such as what is returned from the Knack API or via JSON download in the Knack builder.
  • field_defs list - A list of knackpy.fields.FieldDef objects.
  • identifier str or None - The Knack field key which should be used in the instance’s repr. This property is available in the knack object metadata, and is referred to the “Display Field” in the Knack builder under object settings. For some reason, the identifier is not always available in the object’s metadata. If the identifier is None, “id” will be used.
  • timezone pytz.timezone - A pytz.timezone object representing the record’s timezone.

__getitem__ #

def __getitem__(client_key)

Return the field whose key or name matches the client-provided value.

Arguments:

  • client_key str - A field key (e.g., “field_99”) or field name.

Returns:

  • object - The field’s value (dict, list, str, int, whatever Knack has in store for you.)

__setitem__ #

def __setitem__(key, value)

Bad things will happen if you re-assign record values to anything other than a field class. This is not immediately obvious, because you can assign values to the record without issue, but some operations will fail after assignment of a non-Field value. e.g., .format() and dict().

All that to say, we set immutable = True after init, and further attempts to setitem will raise a TypeError.

Raises:

  • TypeError - Record object does not support item assignment.

format #

def format(keys: Union[list, bool] = True, values: Union[list, bool] = True)

Returns the record as a dict.

Arguments:

  • keys bool or list, optional - If the keys should be formatted, or a list of field keys specifying the keys to be formatted.
  • values bool or list, optional - If values should be formatted, or a list of field keys specifying the values to be formatted

Returns:

  • dict - A dict of the record values with formatted (aka, humaized) keys and/or values.