unitpackage.local

Utilities to work with local data packages such as collecting packages and creating unitpackages.

unitpackage.local.collect_datapackage(filename)

Return a data package from a :param filename.

EXAMPLES:

>>> package = collect_datapackage("./examples/no_bibliography/no_bibliography.json")
>>> package 
{'resources': [{'name':
...
unitpackage.local.collect_datapackages(data)

Return a list of data packages defined in the directory data and its subdirectories.

EXAMPLES:

>>> packages = collect_datapackages("./examples")
>>> packages[0] 
{'resources': [{'name':
...
unitpackage.local.create_df_resource(package, resource_name='echemdb')

Return a pandas dataframe resource from a data packages, where the first resource refers to a CSV.

EXAMPLES:

>>> from frictionless import Package
>>> package = Package("./examples/no_bibliography/no_bibliography.json")
>>> df_resource = create_df_resource(package) 
>>> df_resource
{'name': 'echemdb',
...

>>> df_resource.data
              t         E         j
...
unitpackage.local.create_unitpackage(csvname, metadata=None, fields=None)

Return a data package built from a :param metadata: dict and tabular data in :param csvname: str.

The :param fields: list must must be structured such as [{‘name’:’E’, ‘unit’: ‘mV’}, {‘name’:’T’, ‘unit’: ‘K’}].

EXAMPLES:

>>> fields = [{'name':'E', 'unit': 'mV'}, {'name':'I', 'unit': 'A'}]
>>> package = create_unitpackage("./examples/from_csv/from_csv.csv", fields=fields)
>>> package 
{'resources': [{'name':
...

TESTS:

Invalid fields:

>>> fields = 'not a list'
>>> package = create_unitpackage("./examples/from_csv/from_csv.csv", fields=fields) 
Traceback (most recent call last):
...
ValueError: 'fields' must be a list such as
[{'name': '<fieldname>', 'unit':'<field unit>'}]`,
e.g., `[{'name':'E', 'unit': 'mV}, {'name':'T', 'unit': 'K}]`

More fields than required:

>>> fields = [{'name':'E', 'unit': 'mV'}, {'name':'I', 'unit': 'A'}, {'name':'x', 'unit': 'm'}]
>>> package = create_unitpackage("./examples/from_csv/from_csv.csv", fields=fields) 

Part of the fields specified:

>>> fields = [{'name':'E', 'unit': 'mV'}]
>>> package = create_unitpackage("./examples/from_csv/from_csv.csv", fields=fields) 
unitpackage.local.write_metadata(out, metadata)

Write metadata to the out stream in JSON format.