Skip to main content

pyMetadataEditor

A tool connected to Metadata Editor for creating, editing and managing metadata for microdata, indicators, geospatial data, documents, scripts, images and videos.

How to use pyMetadataEditor

from pymetadataeditor import MetadataEditor
import os
your_api_key = os.getenv("API_KEY")
api_url = os.getenv("API_URL")
me = MetadataEditor(api_url=api_url, api_key=your_api_key, verify_ssl=False)

Listing your projects

me.list_projects(limit=8)
type idno study_idno title abbreviation nation year_start year_end
id
1003 document 12345 DOC_001 Sample Document 1 SD1 Example Nation 2020 2025
1002 survey 67890 SURVEY_002 Sample Survey 2 SS2 Example Nation 2019 2024
1001 timeseries 54321 TS_003 Time Series 3 TS3 Another Nation 2021 2026

Creating a new indicator project

demo_name = "GB20241030_demo"
series_description = {
                        "idno": demo_name,
                        "doi": "V1",
                        "name": "Version 1",
                        "display_name": "Version 1"
                     }

indicator_id = me.create_project_log({"idno": demo_name, "series_description": series_description}, "indicator")

Starting with outlines

The metadata can be both large and hierarchical. Starting with a skeleton outline makes things easier.

Outlines are available in three modes - dictionary, pydantic model and as an Excel file.

Dictionaries

Dictionaries are created like so:

indicator_dict = me.make_metadata_outline('indicator', output_mode='dict')
indicator_dict
{'metadata_information': {'title': None,
  'idno': None,
  'producers': [{'name': '', 'abbr': None, 'affiliation': None, 'role': None}],
  'prod_date': None,
  ...
    'email': None,
    'telephone': None,
    'uri': None}]},
 'tags': [{'tag': None, 'tag_group': None}]}

Pydantic

Pydantic is a nice python library for defining and validating data schemas. An outline for the indicator schema can be created like so:

indicator_pydantic = me.make_metadata_outline('indicator', 'pydantic')
indicator_pydantic

giving

IHSN_INDICATOR_1-0_Template_v01_EN(metadata_information=metadata_information(title=None, idno=None, producers=[Producer(name='', abbr=None, affiliation=None, role=None)], prod_date=None, ...

It can be updated using dot notation, for example:

indicator_pydantic.metadata_information.producers[0].name = "example_producer"
indicator_pydantic

giving

IHSN_INDICATOR_1-0_Template_v01_EN(metadata_information=metadata_information(title=None, idno=None, producers=[Producer(name='example_producer', abbr=None, affiliation=None, role=None)], prod_date=None, ...

Excel

Finally, a nicely formatted Excel file can be created into which the metadata can be written, with the name of the metadata type or of the default template used as the filename if no filename is explicitly given.

outline_filename = me.make_metadata_outline('indicator', 'excel')

And then read back in from Excel like so:

indicator_excel = me.read_metadata_from_excel(outline_filename)

Retreiving existing metadata

Likewise, existing projects can be downloaded as either dictionaries, pydantic models or as excel spreadsheets.

Asking for the metadata as a pydantic object

demo_pydantic = me.get_project_metadata_by_id(indicator_id, 'pydantic')
demo_pydantic

which gives:

IHSN_INDICATOR_1-0_Template_v01_EN(metadata_information=metadata_information(title=None, idno=None, producers=[Producer(name='', abbr=None, affiliation=None, role=None)], prod_date=None, ...

Automatic Metadata Creation and Augmentation from Sources

We can use a Large Language Model to make a first draft of metadata from a source document or documents.

We can create metadata from source files such as:

  • pdfs
  • word
  • excel
  • powerpoint
  • text files
  • csv
  • XML
  • ZIP files
  • Images
docs = ["survey_records/cambodia/cambodia_lsms_basic_information_document.pdf", "survey_records/cambodia/cambodia_living_standards_measurement_study_plus_manual_english.pdf"]

example = me.draft_metadata_from_files(openai_api_key=openai_key, 
                                       files=docs, 
                                       metadata_type_or_template_uid='microdata',
                                       output_mode='pydantic',
                                       metadata_producer_organization="The World Bank Group, DEC - Development Data Group"
                                       )

The files are read in and sent to the LLM for processing.

Read in survey_records/cambodia/cambodia_lsms_basic_information_document.pdf, running token count is 6373
Read in survey_records/cambodia/cambodia_living_standards_measurement_study_plus_manual_english.pdf, running token count is 24901
Sending to OpenAI, this may take a few minutes...

We can then view the new metadata

example.pretty_print()

which gives

IHSN_DDI_2-5_Template_v01_EN(
    doc_desc=doc_desc(
        producers=[
            Producer(
                name='The World Bank Group, DEC - Development Data Group',
                abbr='WBG',
                affiliation='World Bank',
                role='Metadata producer'
            )
        ],
        prod_date='2025-01-28',
        idno='CAMBODIA_LSMS_PLUS_2019_2020_v01_EN',
        version_statement=version_statement(
            version='1.0',
            version_date='2025-01-28',
            version_resp='',
            version_notes='First draft of the metadata for the Cambodia Living Standards Measurement Study - Plus 
(LSMS+) 2019-20.'
        )
    ),
    ...

)

Contributing

Setting up the python environment

This library uses Poetry for dependency management (https://python-poetry.org/docs/basic-usage/).

In your python environment run pip install poetry then navigate to the pymetadataeditor folder and run poetry install or, if that doesn't work, try python -m poetry install.

Development python environment

If you want to make changes to this repo then you also need to install the tools used for development but which aren't used otherwise, for example pytest.

Run:

poetry install --with dev poetry run pre-commit install

Poetry troubleshooting

If you are running on Windows and see errors about numpy installation errors then it could be an issue with Windows file paths. With default settings, file paths that exceed a few hundred characters can cause installation problems. To overcome this you can either

  1. enable long path support in Windows (https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=powershell#enable-long-paths-in-windows-10-version-1607-and-later)
  2. install python libraries in a folder in the current directory by running poetry config virtualenvs.in-project true and then running poetry install

Markdown API Documentation

Create documentation for the pyMetadataEditor class by running the following command:

python make_docs.py

Notes

In keeping with World Bank Group practice, it should be noted that parts of this code base were written with the assistance of ChatGPT.

Metadata

Release files for pymetadataeditor 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymetadataeditor 0.3.2
File Size Uploaded Provenance
pymetadataeditor-0.3.2.tar.gz 39.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pymetadataeditor 0.3.2
File Interpreter ABI Platform
pymetadataeditor-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 78.9 kB

Release files / pymetadataeditor-0.3.2.tar.gz

Download URL pymetadataeditor-0.3.2.tar.gz
Size 39.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f93609df63bd7cdc4a31f72fde60db7b2ae17bb7a5ba7ad701827551cb2375ba
BLAKE2b-256 checksum
How to use checksums
55e8da7a68bafea2af93400bc846f0893689de6dca85164395a1e7c4d12a627c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Darwin/24.6.0

Release files / pymetadataeditor-0.3.2-py3-none-any.whl

Download URL pymetadataeditor-0.3.2-py3-none-any.whl
Size 39.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ba0e9204fde1d8bb84167cbe21008fd6f675e4e854992392b779c7a3062e665
BLAKE2b-256 checksum
How to use checksums
56b9da3e53fb31e5db637765e734ed3ac0eb360a350810ae2d5b024e242ee8cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.11.9 Darwin/24.6.0

Security for pymetadataeditor 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page