{ "cells": [ { "cell_type": "markdown", "id": "db1f241d-601e-4422-883a-7905830a1a9c", "metadata": {}, "source": [ "# Group creation\n", "The default group creation is not much different, except that you can optionally pass attributes as a dictionary during group initialization.\n", "\n", "Furthermore, it is also possible to overwrite an existing group by passing `overwrite=True`.\n", "\n", "If the group should not be overwritten but only the attributes, pass `update_attrs=True`. Note, that this will update the existing matching attributes." ] }, { "cell_type": "code", "execution_count": 1, "id": "859f96b2-c712-418a-9104-b94bc3a7799d", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Failed to import module h5tbx\n" ] }, { "data": { "text/plain": [ "using(\"h5py\")" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import h5rdmtoolbox as h5tbx\n", "\n", "h5tbx.use(None)" ] }, { "cell_type": "code", "execution_count": 2, "id": "e05731c2-b266-4139-9bf7-4fe5d3354035", "metadata": { "tags": [] }, "outputs": [], "source": [ "with h5tbx.File() as h5:\n", " h5.create_group('mygrp')\n", " h5.create_group('mygrp', overwrite=True)" ] }, { "cell_type": "code", "execution_count": 3, "id": "f13b927e-361f-46e8-aeea-e9a0c1505371", "metadata": {}, "outputs": [], "source": [ "with h5tbx.File() as h5:\n", " h5.create_group('mygrp', attrs=dict(one=2, two='a second attr'))\n", " \n", " # create the same group again but indicate that only the attributes should be overwritten:\n", " h5.create_group('mygrp', attrs=dict(one=2, two='a second attr which is overwritten'), update_attrs=True)" ] }, { "cell_type": "markdown", "id": "656b228d-aacc-4e4c-b78a-a793e37d0fae", "metadata": {}, "source": [ "# Group exploration\n", "\n", "Sometimes it is helpful to get all groups of a current group." ] }, { "cell_type": "code", "execution_count": 4, "id": "ac44590c-f3e8-4913-bcf3-73f9cee6ba5e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "with h5tbx.File(h5.hdf_filename) as h5:\n", " print(h5.get_groups())" ] }, { "cell_type": "markdown", "id": "ed12c8db-c9c2-4c08-80ea-8068037f562d", "metadata": {}, "source": [ "With `recursive=True` all subgroups are returned, too, however the default is `False`." ] }, { "cell_type": "code", "execution_count": 5, "id": "4cf89676-2d5d-46fe-9b03-f1525b52f3e3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[, ]\n" ] } ], "source": [ "with h5tbx.File(h5.hdf_filename) as h5:\n", " print(h5.get_groups(recursive=True))" ] } ], "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 }