{ "cells": [ { "cell_type": "markdown", "id": "22d93371-d72a-498a-b25e-affb11659f0d", "metadata": {}, "source": [ "## Tests of connection to the Neo4j database, and basic NeoAccess library operations\n", "#### (`debug` mode OFF)" ] }, { "cell_type": "code", "execution_count": 1, "id": "910c294a-eb6b-43d7-9369-980f20974e12", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 'D:\\Docs\\- MY CODE\\Brain Annex\\BA-Win7' to sys.path\n" ] } ], "source": [ "import set_path # Importing this module will add the project's home directory to sys.path" ] }, { "cell_type": "code", "execution_count": 2, "id": "e00686a6-c019-414e-92be-a44d32cfe138", "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import getpass\n", "\n", "from BrainAnnex.modules.neo_access.neo_access import NeoAccess" ] }, { "cell_type": "code", "execution_count": 3, "id": "8ebe967e-3446-4ca5-8584-9603ec532a9b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "To create a database connection, enter the host IP, but leave out the port number: (EXAMPLES: bolt://1.2.3.4 OR neo4j://localhost )\n", "\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ "Enter host IP WITHOUT the port number. EXAMPLE: bolt://123.456.789.012 bolt://155.248.202.124\n", "Enter the database password: ········\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "=> Will be using: host='bolt://155.248.202.124:7687', username='neo4j', password=**********\n" ] } ], "source": [ "print(\"To create a database connection, enter the host IP, but leave out the port number: (EXAMPLES: bolt://1.2.3.4 OR neo4j://localhost )\\n\")\n", "\n", "host = input(\"Enter host IP WITHOUT the port number. EXAMPLE: bolt://123.456.789.012\")\n", "host += \":7687\" # EXAMPLE of host value: \"bolt://123.456.789.012:7687\"\n", "\n", "password = getpass.getpass(\"Enter the database password:\")\n", "\n", "print(f\"\\n=> Will be using: host='{host}', username='neo4j', password=**********\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "7247f139-9f06-41d1-98e8-410ff7c9f177", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Attempting to connect to Neo4j host 'bolt://155.248.202.124:7687', with username 'neo4j'\n" ] } ], "source": [ "db = NeoAccess(host=host,\n", " credentials=(\"neo4j\", password), debug=False)" ] }, { "cell_type": "code", "execution_count": 5, "id": "c96ece03-2b07-4a4d-ad6e-e41ccbf67251", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Version of the Neo4j driver: 4.3.9\n" ] } ], "source": [ "print(\"Version of the Neo4j driver: \", db.version())" ] }, { "cell_type": "code", "execution_count": 6, "id": "c3e8506e-b904-48c1-a22a-2818807814cf", "metadata": {}, "outputs": [], "source": [ "db.empty_dbase() # WARNING: USE WITH CAUTION!!!" ] }, { "cell_type": "code", "execution_count": 7, "id": "9b1f3651-f613-4966-b1ac-f2eddfbb2e79", "metadata": {}, "outputs": [], "source": [ "neo_car = db.create_node(\"Car\", {'color': 'white', 'make': 'Toyota'})\n", "neo_person = db.create_node(\"Person\", {'name': 'Julian'})" ] }, { "cell_type": "code", "execution_count": 8, "id": "5063cec5-e693-4002-aa84-c5c71240039d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "number_added = db.add_links(neo_car, neo_person, rel_name=\"OWNED_BY\")\n", "\n", "number_added" ] }, { "cell_type": "code", "execution_count": 9, "id": "151b9ffc-2cdc-4f49-837f-001cf1dab1d2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'color': 'white', 'make': 'Toyota'}]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retrieve the car nodes\n", "db.get_nodes(neo_car)" ] }, { "cell_type": "code", "execution_count": 10, "id": "b33d4866-3f90-402f-94da-5b9cdfb67666", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'white'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retrieve a single property of the car node (for situation when only 1 node is present)\n", "db.get_nodes(neo_car, single_cell=\"color\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "cf9dec5d-2376-4a63-a1ab-24d5fbfc1dc2", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How many owners does the car have?\n", "db.count_links(neo_car, rel_name=\"OWNED_BY\", rel_dir=\"OUT\") " ] }, { "cell_type": "code", "execution_count": 12, "id": "df19cdc1-992b-471b-8fa2-ada0fb4c226d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'name': 'Julian'}]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Look up information about the car owner(s)\n", "db.follow_links(neo_car, rel_name=\"OWNED_BY\", rel_dir=\"OUT\") " ] }, { "cell_type": "code", "execution_count": null, "id": "a07e719a-fa52-4752-b29c-98009d8f3709", "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }