Hydra utility library

Utility functions

Units and Dimensions

hydra_base.lib.units.add_dimension(dimension, **kwargs)

Add the dimension defined into the object “dimension” to the DB If dimension[“project_id”] is None it means that the dimension is global, otherwise is property of a project If the dimension exists emits an exception

hydra_base.lib.units.add_unit(unit, **kwargs)

Add the unit defined into the object “unit” to the DB If unit[“project_id”] is None it means that the unit is global, otherwise is property of a project If the unit exists emits an exception

A minimal example:

new_unit = dict(

    name      = 'Teaspoons per second',
    abbreviation = 'tsp s^-1',
    cf        = 0,               # Constant conversion factor
    lf        = 1.47867648e-05,  # Linear conversion factor
    dimension_id = 2,
    description  = 'A flow of one teaspoon per second.',
)
add_unit(new_unit)
hydra_base.lib.units.bulk_add_dimensions(dimension_list, **kwargs)

Save all the dimensions contained in the passed list.

hydra_base.lib.units.bulk_add_units(unit_list, **kwargs)

Save all the units contained in the passed list, with the name of their dimension.

hydra_base.lib.units.check_consistency(measure_or_unit_abbreviation, dimension, **kwargs)

Check whether a specified unit is consistent with the physical dimension asked for by the attribute or the dataset.

hydra_base.lib.units.check_unit_matches_dimension(unit_id, dimension_id, **kwargs)

Check whether a specified unit is part of the specified dimension.

Args:

unit_id (int): The ID of the unit to compare with the dimension dimension_id (int): The ID of the dimension to check

Throws:

hydra_base.ValidationError when the unit_id does not match the supplied dimension

Returns:

None

hydra_base.lib.units.convert(values, source_measure_or_unit_abbreviation, target_measure_or_unit_abbreviation)

Convert a value or a list of values from an unit to another one. The two units must represent the same physical dimension.

hydra_base.lib.units.convert_dataset(dataset_id, target_unit_abbreviation, **kwargs)

Convert a whole dataset (specified by ‘dataset_id’) to new unit (‘target_unit_abbreviation’). Conversion ALWAYS creates a NEW dataset, so function returns the dataset ID of new dataset.

hydra_base.lib.units.convert_units(values, source_measure_or_unit_abbreviation, target_measure_or_unit_abbreviation, **kwargs)

Convert a value from one unit to another one.

Args:

values: single measure or an array of measures source_measure_or_unit_abbreviation: A measure in the source unit, or just the abbreviation of the source unit, from which convert the provided measure value/values target_measure_or_unit_abbreviation: A measure in the target unit, or just the abbreviation of the target unit, into which convert the provided measure value/values

Example:

>>> cli = PluginLib.connect()
>>> cli.service.convert_units(20.0, 'm', 'km')
0.02
Returns:

Always a list

hydra_base.lib.units.delete_dimension(dimension_id, **kwargs)

Delete a dimension from the DB. Raises and exception if the dimension does not exist

hydra_base.lib.units.delete_unit(unit_id, **kwargs)

Delete a unit from the DB. Raises and exception if the unit does not exist

hydra_base.lib.units.exists_dimension(dimension_name, **kwargs)

Given a dimension returns True if it exists, False otherwise

hydra_base.lib.units.get_dimension(dimension_id, do_accept_dimension_id_none=False, **kwargs)

Given a dimension id returns all its data

hydra_base.lib.units.get_dimension_by_name(dimension_name, **kwargs)

Given a dimension name returns all its data. Used in convert functions

hydra_base.lib.units.get_dimension_by_unit_id(unit_id, do_accept_unit_id_none=False, **kwargs)

Return the physical dimension a given unit id refers to. if do_accept_unit_id_none is False, it raises an exception if unit_id is not valid or None if do_accept_unit_id_none is True, and unit_id is None, the function returns a Dimension with id None (unit_id can be none in some cases)

hydra_base.lib.units.get_dimension_by_unit_measure_or_abbreviation(measure_or_unit_abbreviation, **kwargs)

Return the physical dimension a given unit abbreviation of a measure, or the measure itself, refers to. The search key is the abbreviation or the full measure

hydra_base.lib.units.get_dimensions(**kwargs)

Returns a list of objects describing all the dimensions with all the units.

hydra_base.lib.units.get_empty_dimension(**kwargs)

Returns a dimension object initialized with empty values

hydra_base.lib.units.get_unit(unit_id, **kwargs)

Returns a single unit

hydra_base.lib.units.get_unit_by_abbreviation(unit_abbreviation, **kwargs)

Returns a single unit by abbreviation. Used as utility function to resolve string to id

hydra_base.lib.units.get_units(**kwargs)

Returns all the units

hydra_base.lib.units.is_global_dimension(dimension_id, **kwargs)

Returns True if the dimension is Global, False is it is assigned to a project

hydra_base.lib.units.is_global_unit(unit_id, **kwargs)

Returns True if the Unit is Global, False is it is assigned to a project ‘unit’ is a Unit object

hydra_base.lib.units.log = <Logger hydra_base.lib.units (INFO)>

DIMENSION FUNCTIONS - VARIOUS

hydra_base.lib.units.update_dimension(dimension, **kwargs)

Update a dimension in the DB. Raises and exception if the dimension does not exist. The key is ALWAYS the name and the name itself is not modificable

hydra_base.lib.units.update_unit(unit, **kwargs)

Update a unit in the DB. Raises and exception if the unit does not exist

hydra_base.lib.units.validate_resource_attributes(resource, attributes, template, check_unit=True, exact_match=False, **kwargs)

Validate that the resource provided matches the template. Only passes if the resource contains ONLY the attributes specified in the template.

The template should take the form of a dictionary, as should the resources.

check_unit: Makes sure that if a unit is specified in the template, it

is the same in the data

exact_match: Ensures that all the attributes in the template are in

the data also. By default this is false, meaning a subset of the template attributes may be specified in the data. An attribute specified in the data must be defined in the template.

@returns a list of error messages. An empty list indicates no errors were found.

Logging

class hydra_base.hydra_logging.ColorizingStreamHandler(stream=None)
emit(record)

Emit a record.

If a formatter is specified, it is used to format the record. The record is then written to the stream with a trailing newline. If exception information is present, it is formatted using traceback.print_exception and appended to the stream. If the stream has an ‘encoding’ attribute, it is used to determine how to do the output to the stream.

format(record)

Format the specified record.

If a formatter is set, use it. Otherwise, use the default formatter for the module.

Error classes

exception hydra_base.exceptions.DataError(message='A hydra error has occurred')
exception hydra_base.exceptions.HydraAttributeError(message='A hydra error has occurred')
exception hydra_base.exceptions.HydraDBError(message='A hydra error has occurred')
exception hydra_base.exceptions.HydraError(message='A hydra error has occurred')
exception hydra_base.exceptions.HydraPluginError(message='A hydra error has occurred')
exception hydra_base.exceptions.OwnershipError(message='A hydra error has occurred')
exception hydra_base.exceptions.PermissionError(message='A hydra error has occurred')
exception hydra_base.exceptions.ResourceNotFoundError(message='A hydra error has occurred')
exception hydra_base.exceptions.ValidationError(message='A hydra error has occurred')