Skip to content

Commit 4778c46

Browse files
authored
Merge pull request #1 from ccozad/neo4j
Neo4j example
2 parents 0a375e6 + 2048d89 commit 4778c46

File tree

6 files changed

+115
-3
lines changed

6 files changed

+115
-3
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ See this video for additional commentary on [Python Dev Environment Setup](https
4040
- On Windows: `.venv\Scripts\activate`
4141
- *Install dependencies*
4242
- On Mac: `pip install -r requirements.txt`
43-
- On Windoes: `pip install -r requirements.txt`
43+
- On Windows: `pip install -r requirements.txt`
4444
- *Run tests*
4545
- `pytest -rA`
4646

4747
# Topics
4848
Here's what you can find here:
4949

5050
## [Algorithms](/algorithms/)
51-
- [Binary search](/algorithms/binary_search_client.py) an efficent search within sorted lists
51+
- [Binary search](/algorithms/binary_search_client.py) an efficient search within sorted lists
5252
- [Binary search tree](/algorithms/binary_tree.py) a specially organized binary tree that allows divide and conquer searches
5353
- [Hamming distance](/algorithms/hamming_distance.py) Number of positions in which symbols differ for two equal length strings
5454
- [Inorder tree traversal](/algorithms/binary_search_tree_walker.py) Traverse a binary search tree inorder to list nodes in sorted ascending order
@@ -77,6 +77,10 @@ Here's what you can find here:
7777
- [Load CSV into a pandas dataframe](/data_science/load_csv.py)
7878
- [Group data and then find statistics for each group](/data_science/stats_by_group.py)
7979

80+
## Databases
81+
- [Neo4j](/databases/neo4j/)
82+
- [Run a simple query against Neo4j](/databases/neo4j/simple.py)
83+
8084
## [Games](/games/)
8185
- Boards
8286
- [2D grid](/games/boards/grid_2d.py) A simple representation of a 2D grid board that can be used for games like tic-tac-toe or checkers

azure/key_vault/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Change `example.env` to `.env`
1212

1313
# Execution Order
1414

15-
All of the examples have guards if they are run before preconditions are met. The recomended order to run things and have something actually happen is
15+
All of the examples have guards if they are run before preconditions are met. The recommended order to run things and have something actually happen is
1616

1717
1. `set_secret.py`
1818
2. `get_secret.py`

databases/neo4j/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.venv/
2+
.env

databases/neo4j/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Introduction
2+
3+
This set of examples assumes you have the movie database loaded
4+
5+
See https://neo4j.com/graphgists/cypher-introduction-social-movie-database/ for details on the movie database.
6+
7+
# Dependencies
8+
9+
All of the dependencies listed below need to be in place before running the code.
10+
11+
- Python virtual environment
12+
- Environment variables
13+
14+
## Python Virtual Environment
15+
16+
- Move to the neo4j folder
17+
- `cd <neo4j>`
18+
- Create a virtual environment
19+
- On Mac: `python3 -m venv .venv`
20+
- On Windows: `python -m venv .venv`
21+
- Activate the virtual environment
22+
- On Mac: `source .venv/bin/activate`
23+
- On Windows: `.venv\Scripts\activate`
24+
- Install dependencies
25+
- On Mac: `pip3 install -r requirements.txt`
26+
- On Windows: `pip install -r requirements.txt`
27+
- Call a specific script
28+
- On Mac: `python3 <script_name>.py`
29+
- On Windows: `python <script_name>.py`
30+
- Deactivate virtual environment
31+
- `deactivate`
32+
33+
## Environment Variables
34+
35+
Create a file named `.env` with the contents shown below
36+
37+
```
38+
NEO4J_URI=<address>
39+
NEO4J_USER=<user>
40+
NEO4J_PASSWORD=<password>
41+
```
42+
43+
# Example Output
44+
45+
## simple.py
46+
47+
```text
48+
python3 simple.py
49+
50+
Successfully connected to Neo4j
51+
Running a simple query:
52+
Movies that Tom Hanks acted in:
53+
- Apollo 13
54+
- You've Got Mail
55+
- A League of Their Own
56+
- Joe Versus the Volcano
57+
- That Thing You Do
58+
- The Da Vinci Code
59+
- Cloud Atlas
60+
- Cast Away
61+
- The Green Mile
62+
- Sleepless in Seattle
63+
- The Polar Express
64+
- Charlie Wilson's War
65+
66+
The query `MATCH (actor:Person {name: $name})-[:ACTED_IN]->(movies) RETURN actor,movies` returned 12 records in 9 ms.
67+
```

databases/neo4j/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv
2+
neo4j

databases/neo4j/simple.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This example assumes you have the movie database loadeed
2+
from neo4j import GraphDatabase
3+
4+
import os
5+
import dotenv
6+
dotenv.load_dotenv()
7+
neo4j_uri = os.getenv("NEO4J_URI")
8+
neo4j_user = os.getenv("NEO4J_USER")
9+
neo4j_password = os.getenv("NEO4J_PASSWORD")
10+
auth = (neo4j_user, neo4j_password)
11+
12+
with GraphDatabase.driver(neo4j_uri, auth=auth) as driver:
13+
driver.verify_connectivity()
14+
15+
print("Successfully connected to Neo4j")
16+
17+
print("Running a simple query:")
18+
19+
records, summary, keys = driver.execute_query(
20+
"MATCH (actor:Person {name: $name})-[:ACTED_IN]->(movies) RETURN actor,movies",
21+
name="Tom Hanks",
22+
database_="neo4j",
23+
)
24+
25+
print("Movies that Tom Hanks acted in:")
26+
27+
# Loop through results and do something with them
28+
for movie in records:
29+
title = movie.data().get("movies").get("title")
30+
print(" - ", title)
31+
32+
# Summary information
33+
print("\nThe query `{query}` returned {records_count} records in {time} ms.".format(
34+
query=summary.query, records_count=len(records),
35+
time=summary.result_available_after,
36+
))
37+

0 commit comments

Comments
 (0)