{ "cells": [ { "cell_type": "markdown", "id": "93d9a7e4-6832-4015-9c2c-cfead3bbc5c6", "metadata": {}, "source": [ "# Creating a convention using [metadata4ing](https://nfdi4ing.pages.rwth-aachen.de/metadata4ing/metadata4ing/#http://w3id.org/nfdi4ing/metadata4ing#ContactPerson)" ] }, { "cell_type": "code", "execution_count": 1, "id": "5d881134-bc2a-41ef-92cd-4dccc9445ede", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Failed to import module h5tbx\n" ] } ], "source": [ "import h5rdmtoolbox as h5tbx\n", "from h5rdmtoolbox import convention" ] }, { "cell_type": "code", "execution_count": 2, "id": "95090720-1057-497a-89a1-0a6a95590e3d", "metadata": {}, "outputs": [], "source": [ "cv = convention.Convention(name='PersonConvention', contact='John Doe')\n", "cv.register()" ] }, { "cell_type": "code", "execution_count": 3, "id": "94f15c2a-d891-4cce-8ad5-bb293ddc367b", "metadata": {}, "outputs": [], "source": [ "from pydantic import BaseModel\n", "from typing_extensions import Literal\n", "\n", "Role = Literal[\n", " \"Creator\",\n", " \"ContactPerson\",\n", "]\n", "\n", "class Person(BaseModel):\n", " first_name: str\n", " last_name: str\n", " role: Role\n", "\n", "class SpecialStandardAttribute(h5tbx.convention.standard_attributes.StandardAttribute):\n", "\n", " def set(self, parent, value, attrs=None):\n", " \"\"\"Is called when attribute of a standard attribute is set\"\"\"\n", " print(value)\n", " grp_name = f'{value[\"role\"]}Person'\n", " i = 0\n", " while grp_name in parent:\n", " i += 1\n", " grp_name = f'{value[\"role\"]}Person{i}'\n", " person_grp = parent.create_group(grp_name)\n", " \n", " person_grp.create_string_dataset('first_name',\n", " data=value['first_name'],\n", " attrs=dict(iri='http://xmlns.com/foaf/0.1/firstName',\n", " description='The first name of a person'))\n", " person_grp.create_string_dataset('last_name',\n", " data=value['last_name'],\n", " attrs=dict(iri='http://xmlns.com/foaf/0.1/lastName',\n", " description='The last name of a person'))\n", "\n", "personStdAttr = SpecialStandardAttribute(\n", " name='person',\n", " validator=Person,\n", " target_method='__init__',\n", " description='Person',\n", " default_value='$empty'\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "id": "1f42e04e-14a8-4f93-a980-bcb1853ebabb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'person': }" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv.add_standard_attribute(personStdAttr)\n", "cv.registered_standard_attributes" ] }, { "cell_type": "code", "execution_count": 5, "id": "afd03386-65b0-42e6-9646-0516083b2c41", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "using(\"PersonConvention\")" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h5tbx.use(None)\n", "h5tbx.use(cv.name)" ] }, { "cell_type": "code", "execution_count": 6, "id": "e6a265c7-ba59-45ac-af9c-ef6bcd83bd5c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Person(first_name='Matthias', last_name='Probst', role='Creator')" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Person(first_name='Matthias', last_name='Probst', role='Creator')" ] }, { "cell_type": "code", "execution_count": 7, "id": "218b9643-c9a9-4b3f-8a64-a7cfdc9dea1b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'first_name': 'Matthias', 'last_name': 'Probst', 'role': 'Creator'}\n" ] }, { "data": { "text/html": [ "\n", "
\n", "\n", " \n", "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "with h5tbx.File(person=dict(first_name='Matthias', last_name='Probst', role='Creator')) as h5:\n", " h5.dump()" ] }, { "cell_type": "code", "execution_count": null, "id": "48657e9d-fcab-49ce-8a0d-e5a6e1ce47e0", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.19" } }, "nbformat": 4, "nbformat_minor": 5 }