Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 2932bda

Browse files
authored
Merge pull request #8 from DerwenAI/update
refactor AST loader to fit into Cython-based Cypher parser
2 parents 0570333 + 655a9f2 commit 2932bda

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# editing
22
*~
3-
TAG
43

54
# Byte-compiled / optimized / DLL files
65
__pycache__/

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ default_language_version:
66
exclude: "cython"
77
repos:
88
- repo: https://github.com/pre-commit/pre-commit-hooks
9-
rev: v3.4.0
9+
rev: v4.0.1
1010
hooks:
1111
- id: check-builtin-literals
1212
- id: check-executables-have-shebangs
@@ -20,7 +20,7 @@ repos:
2020
- id: bandit # security vulnerabilities
2121
args: ["--exclude", "sample.py,setup.py,tests,dat"]
2222
- repo: https://github.com/pre-commit/mirrors-mypy
23-
rev: v0.812
23+
rev: v0.910
2424
hooks:
2525
- id: mypy # type annotations
2626
exclude: ^tests/,^dat/
@@ -29,10 +29,10 @@ repos:
2929
hooks:
3030
- id: pylint
3131
- repo: https://github.com/codespell-project/codespell
32-
rev: v1.16.0
32+
rev: v2.1.0
3333
hooks:
3434
- id: codespell
3535
name: codespell
3636
entry: codespell README.md goedwig/*.py
3737
language: python
38-
types: [text]
38+
types: [text]

dat/cyp/q2.tsv

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
0 -1 0 0 173 statement
2+
1 0 1 0 173 query
3+
2 1 2 0 67 MATCH
4+
3 2 3 6 66 pattern
5+
4 3 4 6 66 pattern path
6+
5 4 5 6 37 node pattern
7+
6 5 6 7 10 identifier tom
8+
7 5 6 10 17 label
9+
8 5 6 18 36 map
10+
9 8 7 19 23 prop name name
11+
10 8 7 24 35 string 'Tom Hanks'
12+
11 4 5 37 53 rel pattern
13+
12 11 6 39 42 identifier rel
14+
13 11 6 42 51 rel type
15+
14 4 5 53 66 node pattern
16+
15 14 6 54 59 identifier movie
17+
16 14 6 59 65 label
18+
17 1 2 67 173 RETURN
19+
18 17 3 74 90 projection
20+
19 18 4 74 83 property
21+
20 19 5 74 77 identifier tom
22+
21 19 5 78 82 prop name name
23+
22 18 4 86 90 identifier name
24+
23 17 3 92 115 projection
25+
24 23 4 92 101 property
26+
25 24 5 92 95 identifier tom
27+
26 24 5 96 100 prop name born
28+
27 23 4 104 115 identifier `Year Born`
29+
28 17 3 117 137 projection
30+
29 28 4 117 129 property
31+
30 29 5 117 122 identifier movie
32+
31 29 5 123 128 prop name title
33+
32 28 4 132 137 identifier title
34+
33 17 3 139 173 projection
35+
34 33 4 139 154 property
36+
35 34 5 139 144 identifier movie
37+
36 34 5 145 153 prop name released
38+
37 33 4 157 172 identifier `Year Released`

goedwig/cypher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__ (
2929
self.s0: int = int(s0) # pylint: disable=C0103
3030
self.s1: int = int(s1) # pylint: disable=C0103
3131
self.ast_typestr: str = ast_typestr
32-
self.literal: str = literal
32+
self.literal: str = literal.strip()
3333
self.children: typing.List[int] = []
3434

3535

goedwig/query.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
Query planning.
66
"""
77

8-
import csv
9-
import pathlib
8+
from collections.abc import Iterator
109
import typing
1110

1211
from icecream import ic # type: ignore # pylint: disable=E0401,W0611
@@ -30,12 +29,13 @@ def __init__ (
3029

3130
def load_ast (
3231
self,
33-
ast_path: pathlib.Path,
32+
tsv_iter: Iterator,
3433
) -> None:
3534
"""
3635
Load a TSV file of AST items.
3736
"""
38-
for row in csv.reader(ast_path.open(), delimiter="\t"):
37+
for tsv_row in tsv_iter:
38+
row = tsv_row.split("\t")
3939
item = CypherItem(row)
4040

4141
# back-link the parent item

sample.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
ast_path = pathlib.Path("dat/cyp") / "q1.tsv"
1818

1919
q = goedwig.Query()
20-
q.load_ast(ast_path)
20+
tsv_iter = iter(ast_path.open(encoding="utf-8").readlines())
21+
q.load_ast(tsv_iter)
2122

2223
for item in q.items:
2324
print(item)
2425

2526
ic(q.query_plan())
27+
28+
assert len(q.items) == 14
29+
assert q.items[-1].literal == "p"

0 commit comments

Comments
 (0)