{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Get UUID\n", "\n", "The `api.get_uuid` api endpoint is a utility function for finding database entities across database models." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example\n", "\n", "We can easily find a specific record by UUID. If you [populated](api_populate_defaults.ipynb) the default `Keyword`, you will find a record of UUID `885735f3-121e-4ca0-ac8b-f37dbc972f03`. The `get_uuid` will figure out, what kind of database model has to be loaded." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "EARTH SCIENCE > TERRESTRIAL HYDROSPHERE \n" ] } ], "source": [ "from metacatalog import api\n", "\n", "session = api.connect_database()\n", "keyword = api.get_uuid(session, uuid='885735f3-121e-4ca0-ac8b-f37dbc972f03')\n", "\n", "print(keyword)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While `Keyword` is already capable of loading all child keywords recursively, we can easily rebuild this function using the `get_uuid` function. This can be very helpful, when a list of objects needs to be loaded from `metacatalog`." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Parent object\n", "########################################\n", "{\n", " \"id\": 25,\n", " \"uuid\": \"885735f3-121e-4ca0-ac8b-f37dbc972f03\",\n", " \"value\": \"TERRESTRIAL HYDROSPHERE\",\n", " \"path\": \"EARTH SCIENCE > TERRESTRIAL HYDROSPHERE\",\n", " \"children\": [\n", " \"099ab1ae-f4d2-48cc-be2f-86bd58ffc4ca\",\n", " \"734f8f27-6976-4b67-8794-c7fc79d6161e\",\n", " \"50b8fe04-9149-4b7f-a8b2-b33b1e3aa192\",\n", " \"5debb283-51e4-435e-b2a2-e8e2a977220d\",\n", " \"8c02f5d1-ce86-4bf5-84d5-b3496cdba6ad\"\n", " ],\n", " \"thesaurus_id\": 1\n", "}\n", "\n", "Children:\n", "########################################\n", "EARTH SCIENCE > TERRESTRIAL HYDROSPHERE > GLACIERS/ICE SHEETS\n", "EARTH SCIENCE > TERRESTRIAL HYDROSPHERE > GROUND WATER\n", "EARTH SCIENCE > TERRESTRIAL HYDROSPHERE > SNOW/ICE\n", "EARTH SCIENCE > TERRESTRIAL HYDROSPHERE > SURFACE WATER\n", "EARTH SCIENCE > TERRESTRIAL HYDROSPHERE > WATER QUALITY/WATER CHEMISTRY\n" ] } ], "source": [ "import json\n", "kw_dict = keyword.to_dict(deep=False)\n", "\n", "print('Parent object\\n' + '#' * 40)\n", "print(json.dumps(kw_dict, indent=4))\n", "\n", "# load the children\n", "print('\\nChildren:\\n' + '#' * 40)\n", "for uuid in kw_dict.get('children', []):\n", " print(api.get_uuid(session, uuid=uuid).full_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Funtion" ] }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. autofunction:: metacatalog.api.get_uuid" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "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.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }