Schema

class lsst.alert.packet.Schema(schema_definition)

Bases: object

An Avro schema.

Parameters:
schema_definitiondict

An Avro schema definition as returned by e.g. fastavro.schema.load_schema.

Notes

The interaction with fastavro here needs some explanation.

When fastavro loads a schema, it parses each of the types contained within that schema and remembers them for future use. So that if, for example, your schema defines a type lsst.alert.diaSource, fastavro will remember that type and use it when referring to your schema.

However, it uses a single lookup table by type for these. Thus, if you load another schema which defines an lsst.alert.diaSource type which is not the same as the first, then it will clobber the earlier definition, and confusion will reign.

We avoid this here by fully resolving everything (ie, all schemas are defined in terms of primitive types) and then clearing the fastavro cache after loading.

Methods Summary

deserialize(record)

Deserialize an Avro packet folowing this schema.

from_file([filename])

Instantiate a Schema by reading its definition from the filesystem.

from_uri([base_uri])

Instantiate a Schema by reading its definition from a URI.

retrieve_alerts(fp)

Read alert packets from the given I/O stream.

serialize(record)

Create an Avro representation of data following this schema.

store_alerts(fp, records)

Store alert packets to the given I/O stream.

validate(record)

Validate packet contents against this schema.

Methods Documentation

deserialize(record)

Deserialize an Avro packet folowing this schema.

Parameters:
recordbytes

The data to be deserialized.

Returns:
alert_datadict

Deserialized packet contents.

classmethod from_file(filename=None)

Instantiate a Schema by reading its definition from the filesystem.

Parameters:
filenamestr, optional

Path to the schema root (/path/to/lsst.vM_m.alert.avsc). Will recursively load referenced schemas, assuming they can be found; otherwise, will raise. If None (the default), will load the latest schema defined in this package.

classmethod from_uri(base_uri: None | str | ResourcePath = None) Schema

Instantiate a Schema by reading its definition from a URI.

Parameters:
base_uristr or lsst.resources.ResourcePath or None

URI to the base schema as either a ResourcePath or a string that can be converted to one. If None the most recent default schema will be used.

retrieve_alerts(fp)

Read alert packets from the given I/O stream.

Parameters:
fpderivative of IOBase

I/O stream from which data will be read.

schemalist, optional

A schema describing the contents of the Avro packets. If not provided, the schema used when writing the alert stream will be used.

Returns:
schemalsst.alert.Schema

The schema with which alerts were written (which may be different from this schema being used for deserialization).

recordsiterable of dict

Alert records.

serialize(record)

Create an Avro representation of data following this schema.

Parameters:
recorddict

The data to be serialized to Avro.

Returns:
avro_databytes

An Avro serialization of the input data.

store_alerts(fp, records)

Store alert packets to the given I/O stream.

Parameters:
fpderivative of IOBase

I/O stream to which data will be written.

recordsiterable of dict

Alert records to be stored.

validate(record)

Validate packet contents against this schema.

Parameters:
recorddict

The data to be checked for schema compliance.

Returns:
validbool

Whether or not the data complies with the schema.