{ "cells": [ { "cell_type": "markdown", "id": "bc8aa0cd-d64b-4ec2-bc5e-d723c20a156e", "metadata": { "tags": [] }, "source": [ "# Natural Naming\n", "\n", "`h5RDMtoolbox` supports attribute style access (natural naming) on datasets, groups and attributes instead of the known dictionary style. This means that HDF objects must not be named as existing attributes of used instances, e.g. `File`." ] }, { "cell_type": "code", "execution_count": 1, "id": "97f1f3a5-5f93-4e1b-b7a4-e3d9b296dd00", "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "using(\"h5py\")" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import h5rdmtoolbox as h5tbx\n", "h5tbx.use(None)" ] }, { "cell_type": "markdown", "id": "c18d75cd-6d5b-4a32-add1-a068a03f563b", "metadata": {}, "source": [ "You can enable natural naming by the following line. However, `True` is the default." ] }, { "cell_type": "code", "execution_count": 2, "id": "44fc86eb-608d-4254-873d-6d4dabd74997", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h5tbx.set_config(natural_naming=True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "c5b94c36-c5c2-4c09-b5a8-a4a45ccd401e", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "with h5tbx.File() as h5:\n", " ds = h5.create_dataset('test', shape=(3,))\n", " print(h5.test)" ] }, { "cell_type": "markdown", "id": "d2b6c0a6-db9d-4b01-80de-3ac8f07aba77", "metadata": {}, "source": [ "Disabeling the natural naming configuration will raise an `AttributeError` when running the above code:" ] }, { "cell_type": "code", "execution_count": 4, "id": "b2d56e3a-78d8-4149-94dc-1377a0bd0cd6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h5tbx.set_config(natural_naming=False)" ] }, { "cell_type": "code", "execution_count": 5, "id": "8e030d3d-b0f7-4cb6-b473-d337724953ef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'File' object has no attribute 'test'\n" ] } ], "source": [ "with h5tbx.File() as h5:\n", " ds = h5.create_dataset('test', shape=(3,))\n", " try:\n", " print(h5.test)\n", " except Exception as e:\n", " print(e)" ] }, { "cell_type": "code", "execution_count": null, "id": "c5bc6305-0f3d-42e4-84f9-243c4f50ba4d", "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 }