unitpackage.metadata
Metadata management for unitpackage entries.
This module provides the MetadataDescriptor class that manages metadata for Entry objects, supporting both dict and attribute access, and providing methods to load metadata from various sources (YAML, JSON, dict).
EXAMPLES:
Access metadata with dict-style or attribute-style syntax:
>>> from unitpackage.entry import Entry
>>> entry = Entry.create_example()
>>> entry.metadata['echemdb']['source']['citationKey']
'alves_2011_electrochemistry_6010'
>>> entry.metadata.echemdb.source.citationKey
'alves_2011_electrochemistry_6010'
Load metadata from external sources:
>>> entry.metadata.from_dict({'custom': {'key': 'value'}})
>>> entry.metadata['custom']['key']
'value'
- class unitpackage.metadata.MetadataDescriptor(entry)
Manages metadata for an Entry, supporting both dict and attribute access, and providing methods to load metadata from various sources.
EXAMPLES:
>>> from unitpackage.entry import Entry >>> entry = Entry.create_example() >>> entry.metadata {'echemdb': {'experimental': ... >>> entry.metadata['echemdb']['source']['citationKey'] 'alves_2011_electrochemistry_6010' >>> entry.metadata.echemdb.source.citationKey 'alves_2011_electrochemistry_6010'
- from_dict(data)
Load metadata from a dictionary.
EXAMPLES:
>>> from unitpackage.entry import Entry >>> entry = Entry.create_example() >>> entry.metadata.from_dict({'echemdb': {'source': {'citationKey': 'test'}}}) >>> entry.metadata['echemdb']['source']['citationKey'] 'test'
- from_json(filename, key=None)
Load metadata from a JSON file.
If a key is provided, the loaded data is stored under that key. Otherwise, it replaces the entire metadata dict.
EXAMPLES:
>>> import os >>> import json >>> import tempfile >>> from unitpackage.entry import Entry >>> entry = Entry.create_example() >>> with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: ... json.dump({'source': {'citationKey': 'json_test'}}, f) ... temp_path = f.name >>> entry.metadata.from_json(temp_path, key='echemdb') >>> entry.metadata['echemdb']['source']['citationKey'] 'json_test' >>> os.unlink(temp_path)
- from_yaml(filename, key=None)
Load metadata from a YAML file.
If a key is provided, the loaded data is stored under that key. Otherwise, it replaces the entire metadata dict.
EXAMPLES:
>>> import os >>> import tempfile >>> import yaml >>> from unitpackage.entry import Entry >>> entry = Entry.create_example() >>> with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f: ... yaml.dump({'source': {'citationKey': 'yaml_test'}}, f) ... temp_path = f.name >>> entry.metadata.from_yaml(temp_path, key='echemdb') >>> entry.metadata['echemdb']['source']['citationKey'] 'yaml_test' >>> os.unlink(temp_path)