MCPcopy Create free account
hub / github.com/GraphLite-AI/GraphLite / main

Function main

examples/python/bindings/basic_usage.py:13–104  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11
12
13def main():
14 print("=== GraphLite Python Bindings Example ===\n")
15
16 # Use temporary directory for demo
17 temp_dir = tempfile.mkdtemp(prefix="graphlite_python_")
18 print(f"Using temporary database: {temp_dir}\n")
19
20 try:
21 # 1. Open database
22 print("1. Opening database...")
23 db = GraphLite(temp_dir)
24 print(f" ✓ GraphLite version: {GraphLite.version()}\n")
25
26 # 2. Create session
27 print("2. Creating session...")
28 session = db.create_session("admin")
29 print(f" ✓ Session created: {session[:20]}...\n")
30
31 # 3. Create schema and graph
32 print("3. Setting up schema and graph...")
33 db.execute(session, "CREATE SCHEMA IF NOT EXISTS example")
34 db.execute(session, "SESSION SET SCHEMA example")
35 db.execute(session, "CREATE GRAPH IF NOT EXISTS social")
36 db.execute(session, "SESSION SET GRAPH social")
37 print(" ✓ Schema and graph created\n")
38
39 # 4. Insert data
40 print("4. Inserting data...")
41 db.execute(session, "CREATE (p:Person {name: 'Alice', age: 30})")
42 db.execute(session, "CREATE (p:Person {name: 'Bob', age: 25})")
43 db.execute(session, "CREATE (p:Person {name: 'Charlie', age: 35})")
44 print(" ✓ Inserted 3 persons\n")
45
46 # 5. Query data
47 print("5. Querying data...")
48 result = db.query(session, "MATCH (p:Person) RETURN p.name as name, p.age as age")
49 print(f" Found {result.row_count} persons:")
50 for row in result.rows:
51 print(f" - {row['name']}: {row['age']} years old")
52 print()
53
54 # 6. Filter with WHERE
55 print("6. Filtering with WHERE clause...")
56 result = db.query(
57 session,
58 "MATCH (p:Person) WHERE p.age > 25 RETURN p.name as name, p.age as age ORDER BY p.age DESC"
59 )
60 print(f" Found {result.row_count} persons over 25:")
61 for row in result.rows:
62 print(f" - {row['name']}: {row['age']} years old")
63 print()
64
65 # 7. Aggregation
66 print("7. Aggregation query...")
67 result = db.query(session, "MATCH (p:Person) RETURN count(p) as total, avg(p.age) as avg_age")
68 if result.rows:
69 row = result.first()
70 print(f" Total persons: {row['total']}")

Callers 1

basic_usage.pyFile · 0.70

Calls 9

create_sessionMethod · 0.95
executeMethod · 0.95
queryMethod · 0.95
close_sessionMethod · 0.95
closeMethod · 0.95
GraphLiteClass · 0.90
versionMethod · 0.45
firstMethod · 0.45
columnMethod · 0.45

Tested by

no test coverage detected