{ "cells": [ { "cell_type": "markdown", "id": "300ab6f1-dc08-41eb-9e77-5f6a8e02ac9a", "metadata": {}, "source": [ "# Activate a Convention\n", "\n", "This section will take you through the steps of activating a convention and find out about registered ones." ] }, { "cell_type": "code", "execution_count": 33, "id": "15348f22-6f7b-4944-976c-3ad94b4f4ef0", "metadata": {}, "outputs": [], "source": [ "from h5rdmtoolbox import convention\n", "import h5rdmtoolbox as h5tbx" ] }, { "cell_type": "markdown", "id": "229d2bd4-d64f-4cfc-9219-6cff1c6ade9f", "metadata": {}, "source": [ "Conventions are registered. If we design a new one, it will only be usable if it is registered.
\n", "To list all available convention, call `get_registered_conventions()`." ] }, { "cell_type": "code", "execution_count": 34, "id": "d221ec94-04ee-4b11-a712-8b992fdbafbd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'h5tbx': Convention(\"h5tbx\"),\n", " 'h5py': Convention(\"h5py\"),\n", " 'MyFirstConvention': Convention(\"MyFirstConvention\"),\n", " 'h5rdmtoolbox-tuturial-convention': Convention(\"h5rdmtoolbox-tuturial-convention\"),\n", " 'h5rdmtoolbox-tutorial-convention': Convention(\"h5rdmtoolbox-tutorial-convention\")}" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "convention.get_registered_conventions()" ] }, { "cell_type": "markdown", "id": "ab671446-fb5c-49a2-984b-264d8a824339", "metadata": {}, "source": [ "## Standard conventions\n", "Two conventions are shipped with the toolbox: \"h5py\" and \"h5tbx\". Without activating any convention, \"h5py\" will be in place. In fact, it does nothing and mimics the behavior of the `h5py` package. This means, that methods like `create_dataset` do not expect other parameters, as we know from the `h5py`-package." ] }, { "cell_type": "markdown", "id": "9c95f394-b8eb-4171-a396-6a68f1cd43f3", "metadata": {}, "source": [ "**Which convention is currently activated?**" ] }, { "cell_type": "code", "execution_count": 35, "id": "b106676c-0ad3-4a26-99bd-3037ebf808c2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Convention(\"h5py\")" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv = convention.get_current_convention()\n", "cv" ] }, { "cell_type": "markdown", "id": "20991d0e-3b4f-467c-80ea-a5d3f8d0b234", "metadata": {}, "source": [ "## Activating a convention\n", "\n", "To activate a convention, call `use()` and pass its name:" ] }, { "cell_type": "code", "execution_count": 37, "id": "9827aab2-6ea2-43c6-a96a-95478151c62f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "using(\"h5tbx\")" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h5tbx.use('h5tbx')" ] }, { "cell_type": "markdown", "id": "38762a7a-e10f-47d3-826b-c7ab7b31898b", "metadata": {}, "source": [ "To deactivate a convention, pass `None` or \"h5py\"" ] }, { "cell_type": "code", "execution_count": 38, "id": "3330f0df-35a8-4ab5-9b39-1b560e4c5fd7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "using(\"h5py\")" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h5tbx.use(None)" ] }, { "cell_type": "markdown", "id": "95503b88-fe67-4cc2-87a0-1059f979c944", "metadata": {}, "source": [ "We can also use a context manager to activate a convention only for part of the code:" ] }, { "cell_type": "code", "execution_count": 40, "id": "7818c13b-de7e-4132-999b-0c8d18ebf4e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "h5py\n", "h5tbx\n", "h5py\n" ] } ], "source": [ "print(convention.get_current_convention().name)\n", "with h5tbx.use('h5tbx'):\n", " print(convention.get_current_convention().name)\n", "print(convention.get_current_convention().name)" ] }, { "cell_type": "markdown", "id": "31444504-86c9-44ae-bd69-a22cb8fa0219", "metadata": {}, "source": [ "To find out how to work with a convention, please go to the [next section](./working_with_conventions.ipynb)." ] } ], "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }