Skip to content

Record

toksearch.record.Record

Bases: object

A class to hold a record of data for a single shot

The Record class is a dictionary-like object that holds data for a single shot. It has a 'shot' field that is required to be an integer, and it has an 'errors' field that is a dictionary of errors that have occurred while processing the data.

Most of the time, you will not need to instantiate a Record object directly. Instead, the Pipeline class will create Record objects for you.

__contains__(key)

Check if a field is in the record

__delattr__(name)

Delete a field by attribute. Refuses the undeletable ones.

Fields live in dict as both dict entries and attributes -- the class sets self.shot directly and the codebase reads record.shot throughout -- so del record.version is the natural attribute-style dual of del record["version"], and it bypassed every guard until this existed. Protection that only covers one of two equivalent idioms is not protection.

__delitem__(key)

Delete a field from the record.

Refuses the undeletable fields. This used to bypass the guard that pop() applies, so del record["shot"] succeeded while record.pop("shot") did not -- a field protected from keep() but not from del is inconsistently protected, which is to say not protected.

Raises InvalidRecordField, not KeyError, so that "you may not remove this" is distinguishable from "there is no such field". A caller writing try: del rec[k] except KeyError: pass would otherwise swallow a refusal as an absence.

__getitem__(key)

Get a field from the record

__init__(shot)

Create a Record object with a shot number

Parameters:

Name Type Description Default
shot int

The shot number for this record

required

Raises:

Type Description
InvalidShotNumber

If the shot number is not castable to an integer

__len__()

Get the number of fields in the record

__repr__()

Get a string representation of the record

__setitem__(key, value)

Set a field in the record

discard(keys)

Remove all fields in keys from the record

Parameters:

Name Type Description Default
keys List[Any]

A list of keys to remove from the record

required

from_dict(input_dict) classmethod

Instantiate a Record object from a dictionary

Parameters:

Name Type Description Default
input_dict dict

Must contain the key 'shot', and must NOT contain the keys 'key' or 'errors'

required

Returns:

Name Type Description
Record Record

A Record object with the fields from input_dict

Raises:

Type Description
MissingShotNumber

If the input_dict does not contain the key 'shot'

InvalidRecordField

If the input_dict contains the key 'key' or 'errors'

get(key, default)

Get a field from the record with a default value if the field does not exist

keep(keys)

Remove all fields from the record that are not in keys

Parameters:

Name Type Description Default
keys List[Any]

A list of keys to keep in the record

required

keys()

Get the keys of the record

pop(key)

Remove a field and return its value.

Undeletable fields are left in place and return None.

set_error(field, exception)

Set an error for a field in the record

Updates the 'errors' field in the record with key 'field' to store the exception (including the traceback in a serializable format)

Parameters:

Name Type Description Default
field str

The field name to set the error for

required
exception Exception

The exception to store

required