{ "cells": [ { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:19.036357Z", "start_time": "2019-09-23T18:50:19.031896Z" } }, "source": [ "# Formatting\n", "\n", "This notebooks demonstrates how to use configured [Formatting](https://nexus-forge.readthedocs.io/en/latest/interaction.html#formatting) string patterns to normalise resource identifiers." ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2019-09-23T18:50:20.068658Z", "start_time": "2019-09-23T18:50:19.054054Z" } }, "outputs": [], "source": [ "from kgforge.core import KnowledgeGraphForge" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A configuration file is needed in order to create a KnowledgeGraphForge session. A configuration can be generated using the notebook [00-Initialization.ipynb](00%20-%20Initialization.ipynb)." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "forge = KnowledgeGraphForge(\"../../configurations/forge.yml\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from kgforge.core import Resource" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "from uuid import uuid4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using a str formatter named 'identifier'" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "uuid = str(uuid4())" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "# here \"identifier\" is the formatter name as configured while \"persons\" can be seen as a type\n", "# by default, the type of formatter is str.format()\n", "uid = forge.format(\"identifier\", \"persons\", uuid) " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "resource = Resource(id=uid, type=\"Person\", name=\"Jane Doe\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " id: https://kg.example.ch/persons/9be10b3c-470c-4a1c-b212-5c118934e055\n", " type: Person\n", " name: Jane Doe\n", "}\n" ] } ], "source": [ "print(resource)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using a URI_REWRITER formatter\n", "This is a store based URI formatter. Using BlueBrainNexus store, this formatter will output a fully expanded resource._store_metadata._self url value" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "from kgforge.core.commons.formatter import Formatter" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['STR', 'URI_REWRITER']" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# List the supported formatter types\n", "[f\"{fm.name}\" for fm in Formatter] " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# When using BlueBrainNexus store, set is_file to True to rewrite a file id or _self \n", "\n", "forge.format(uri=resource.id, formatter=Formatter.URI_REWRITER, is_file=False) " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7 (nexusforgelatest)", "language": "python", "name": "nexusforgelatest" }, "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.0" } }, "nbformat": 4, "nbformat_minor": 4 }