msl.io.group module
A Group can contain sub-Groups and/or Datasets.
- class msl.io.group.Group(name, parent, read_only, **metadata)[source]
Bases:
VertexA
Groupcan contain sub-Groups and/orDatasets.Do not instantiate directly. Create a new
Groupusingcreate_group().- Parameters:
name (
str) – The name of thisGroup. Uses a naming convention analogous to UNIX file systems where eachGroupcan be thought of as a directory and where every subdirectory is separated from its parent directory by the'/'character.read_only (
bool) – Whether theGroupis to be accessed in read-only mode.**metadata – Key-value pairs that are used to create the
Metadatafor thisGroup.
- add_dataset(name, dataset)[source]
Add a
Dataset.Automatically creates the ancestor
Groups if they do not exist.
- add_dataset_logging(name, dataset_logging)[source]
Add a
DatasetLogging.Automatically creates the ancestor
Groups if they do not exist.- Parameters:
name (
str) – The name of the newDatasetLoggingto add.dataset_logging (
DatasetLogging) – TheDatasetLoggingto add. TheDatasetLoggingand theMetadataare copied.
- add_group(name, group)[source]
Add a
Group.Automatically creates the ancestor
Groups if they do not exist.
- create_dataset(name, read_only=None, **kwargs)[source]
Create a new
Dataset.Automatically creates the ancestor
Groups if they do not exist.
- create_dataset_logging(name, level='INFO', attributes=None, logger=None, date_fmt=None, **kwargs)[source]
Create a
Datasetthat handlesloggingrecords.Automatically creates the ancestor
Groups if they do not exist.- Parameters:
level (
intorstr, optional) – The logging level to use.attributes (
listortupleofstr, optional) – The attribute names to include in theDatasetfor each logging record. IfNonethen usesasctime,levelname,name, andmessage.logger (
Logger, optional) – TheLoggerthat theDatasetLoggingobject will be added to. IfNonethen it is added to therootLogger.date_fmt (
str, optional) – Thedatetimeformat code to use to represent theasctimeattribute in. IfNonethen uses the ISO 8601 format'%Y-%m-%dT%H:%M:%S.%f'.**kwargs – Additional keyword arguments are passed to
Dataset. The default behaviour is to append every logging record to theDataset. This guarantees that the size of theDatasetis equal to the number of logging records that were added to it. However, this behaviour can decrease the performance if many logging records are added often because a copy of the data in theDatasetis created for each logging record that is added. You can improve the performance by specifying an initial size of theDatasetby including a shape or a size keyword argument. This will also automatically create additional empty rows in theDataset, that is proportional to the size of theDataset, if the size of theDatasetneeds to be increased. If you do this then you will want to callremove_empty_rows()before writingDatasetLoggingto a file or interacting with the data inDatasetLoggingto remove the extra rows that were created.
- Returns:
DatasetLogging– TheDatasetLoggingthat was created.
Examples
>>> import logging >>> from msl.io import JSONWriter >>> logger = logging.getLogger('my_logger') >>> root = JSONWriter() >>> log_dset = root.create_dataset_logging('log') >>> logger.info('hi') >>> logger.error('cannot do that!') >>> log_dset.data array([(..., 'INFO', 'my_logger', 'hi'), (..., 'ERROR', 'my_logger', 'cannot do that!')], dtype=[('asctime', 'O'), ('levelname', 'O'), ('name', 'O'), ('message', 'O')])
Get all
ERRORlogging records>>> errors = log_dset[log_dset['levelname'] == 'ERROR'] >>> print(errors) [(..., 'ERROR', 'my_logger', 'cannot do that!')]
Stop the
DatasetLoggingobject from receiving logging records>>> log_dset.remove_handler()
- create_group(name, read_only=None, **metadata)[source]
Create a new
Group.Automatically creates the ancestor
Groups if they do not exist.- Parameters:
- Returns:
- datasets(exclude=None, include=None, flags=0)[source]
Get the
Datasets in thisGroup.- Parameters:
exclude (
str, optional) – A regex pattern to use to excludeDatasets. There.search()function is used to compare the exclude regex pattern with the name of eachDataset. If there is a match, theDatasetis not yielded.include (
str, optional) – A regex pattern to use to includeDatasets. There.search()function is used to compare the include regex pattern with the name of eachDataset. If there is a match, theDatasetis yielded.flags (
int, optional) – Regex flags that are passed tore.compile().
- Yields:
Dataset– The filteredDatasets based on the exclude and include regex patterns. The exclude pattern has more precedence than the include pattern if there is a conflict.
- groups(exclude=None, include=None, flags=0)[source]
Get the sub-
Groups of thisGroup.- Parameters:
exclude (
str, optional) – A regex pattern to use to excludeGroups. There.search()function is used to compare the exclude regex pattern with the name of eachGroup. If there is a match, theGroupis not yielded.include (
str, optional) – A regex pattern to use to includeGroups. There.search()function is used to compare the include regex pattern with the name of eachGroup. If there is a match, theGroupis yielded.flags (
int, optional) – Regex flags that are passed tore.compile().
- Yields:
Group– The filteredGroups based on the exclude and include regex patterns. The exclude pattern has more precedence than the include pattern if there is a conflict.
- static is_dataset_logging(obj)[source]
Test whether an object is a
DatasetLogging.- Parameters:
obj (
object) – The object to test.- Returns:
bool– Whether obj is an instance ofDatasetLogging.
- require_dataset(name, read_only=None, **kwargs)[source]
Require that a
Datasetexists.If the
Datasetexists then it will be returned if it does not exist then it is created.Automatically creates the ancestor
Groups if they do not exist.
- require_dataset_logging(name, level='INFO', attributes=None, logger=None, date_fmt=None, **kwargs)[source]
Require that a
Datasetexists for handlingloggingrecords.If the
DatasetLoggingexists then it will be returned if it does not exist then it is created.Automatically creates the ancestor
Groups if they do not exist.- Parameters:
level (
intorstr, optional) – The logging level to use.attributes (
listortupleofstr, optional) – The attribute names to include in theDatasetfor each logging record. If theDatasetexists and if attributes are specified, and they do not match those of the existingDataset, then aValueErroris raised. IfNoneand theDatasetdoes not exist then usesasctime,levelname,name, andmessage.logger (
Logger, optional) – TheLoggerthat theDatasetLoggingobject will be added to. IfNonethen it is added to therootLogger.date_fmt (
str, optional) – Thedatetimeformat code to use to represent theasctimeattribute in. IfNonethen uses the ISO 8601 format'%Y-%m-%dT%H:%M:%S.%f'.**kwargs – Additional keyword arguments are passed to
Dataset. The default behaviour is to append every logging record to theDataset. This guarantees that the size of theDatasetis equal to the number of logging records that were added to it. However, this behaviour can decrease the performance if many logging records are added often because a copy of the data in theDatasetis created for each logging record that is added. You can improve the performance by specifying an initial size of theDatasetby including a shape or a size keyword argument. This will also automatically create additional empty rows in theDataset, that is proportional to the size of theDataset, if the size of theDatasetneeds to be increased. If you do this then you will want to callremove_empty_rows()before writingDatasetLoggingto a file or interacting with the data inDatasetLoggingto remove the extra rows that were created.
- Returns:
DatasetLogging– TheDatasetLoggingthat was created or that already existed.