Skip to content

Commit 5a0933b

Browse files
committed
Update query
1 parent 39c17c3 commit 5a0933b

File tree

4 files changed

+51
-64
lines changed

4 files changed

+51
-64
lines changed

pathtraits/access.py

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,6 @@ def get_dict(db, path):
7070
return res
7171

7272

73-
def get_paths(db, query_str):
74-
"""
75-
Docstring for get_paths
76-
77-
:param db: Description
78-
:param query_str: Description
79-
"""
80-
query_str = f"SELECT DISTINCT path FROM data where {query_str};"
81-
res = db.execute(query_str, ignore_error=False).fetchall()
82-
res = [x["path"] for x in res]
83-
return res
84-
85-
86-
def get_paths_values(db, query_str):
87-
"""
88-
Docstring for get_paths_values
89-
90-
:param db: Description
91-
:param query_str: Description
92-
"""
93-
query_str = f"SELECT * FROM data where {query_str};"
94-
response = db.execute(query_str, ignore_error=False).fetchall()
95-
res = {}
96-
for r in response:
97-
path = r["path"]
98-
# ensure distinct paths
99-
# pylint: disable=C0201
100-
if path not in res.keys():
101-
r = nest_dict(r)
102-
r.pop("path")
103-
res[path] = r
104-
return res
105-
106-
10773
def get(path, db_path, verbose):
10874
"""
10975
Docstring for get
@@ -124,33 +90,16 @@ def get(path, db_path, verbose):
12490
sys.exit(1)
12591

12692

127-
def query(query_str, db_path, show_values):
93+
def query(query_str, db_path):
12894
"""
12995
Docstring for query
13096
13197
:param query_str: Description
13298
:param db_path: Description
13399
"""
134100
db = TraitsDB(db_path)
135-
if show_values:
136-
res = get_paths_values(db, query_str)
137-
if len(res) > 0:
138-
print(yaml.safe_dump(res))
139-
else:
140-
logger.error(
141-
"No paths found for traits matching %s in database %s",
142-
query_str,
143-
db_path,
144-
)
145-
sys.exit(1)
146-
else:
147-
res = get_paths(db, query_str)
148-
if len(res) > 0:
149-
for r in res:
150-
print(r)
151-
else:
152-
logger.error(
153-
"No paths found for traits matching %s in database %s",
154-
query_str,
155-
db_path,
156-
)
101+
paths = db.get_paths(query_str)
102+
if paths == []:
103+
sys.exit(f"No paths matching query '{query_str}'")
104+
for path in paths:
105+
print(path)

pathtraits/cli.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,14 @@ def get(path, db_path, verbose):
7575
default=DB_PATH,
7676
type=click.Path(file_okay=True, dir_okay=False),
7777
)
78-
@click.option(
79-
"--show-values", flag_value=True, default=False, help="Also show their trait values"
80-
)
81-
def query(query_str, db_path, show_values):
78+
def query(query_str, db_path):
8279
"""
8380
Get paths of given traits
8481
8582
Enter QUERY_STR in SQLite3 where statement format,
86-
e.g. "[score/REAL]>1" to get all paths having a score >1.
83+
e.g. "score_REAL>1" to get all paths having a numerical score >1.
8784
"""
88-
access.query(query_str, db_path, show_values)
85+
access.query(query_str, db_path)
8986

9087

9188
if __name__ == "__main__":

pathtraits/db.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ class TraitsDB:
2121
cursor = None
2222
traits = []
2323

24+
@staticmethod
25+
def remove_type_suffixes(s: str):
26+
"""
27+
Docstring for remove_type_suffixes
28+
29+
:param s: Description
30+
:type s: str
31+
"""
32+
s = s.removesuffix("_TEXT").removesuffix("_REAL").removesuffix("_BOOL")
33+
return s
34+
2435
@staticmethod
2536
def row_factory(cursor, row):
2637
"""
@@ -40,7 +51,7 @@ def row_factory(cursor, row):
4051
if isinstance(v, float):
4152
v_int = int(v)
4253
v = v_int if v_int == v else v
43-
k = k.removesuffix("_TEXT").removesuffix("_REAL").removesuffix("_BOOL")
54+
k = TraitsDB.remove_type_suffixes(k)
4455
res[k] = v
4556
return res
4657

@@ -224,6 +235,27 @@ def get_pathtraits(self, path: str):
224235
res[k] = v
225236
return res
226237

238+
def get_paths(self, query_str):
239+
"""
240+
Get paths matching pathtraits
241+
242+
:param self: Description
243+
:param kwargs: pathtraits to match
244+
"""
245+
traits = filter(lambda x: x in query_str, self.traits)
246+
query = "SELECT DISTINCT path FROM _path"
247+
for trait in traits:
248+
query += f" NATURAL JOIN {trait}"
249+
query += f" WHERE {query_str};"
250+
251+
response = self.execute(query)
252+
if response is None:
253+
return None
254+
else:
255+
res = response.fetchall()
256+
res = [x["path"] for x in res]
257+
return res
258+
227259
def put_path_id(self, path):
228260
"""
229261
Docstring for put_path_id

test/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ def test_missing_north_america(self):
7070
for k, v in target.items():
7171
self.assertEqual(source[k], v)
7272

73+
def test_query(self):
74+
q1 = self.db.get_paths("score_REAL > 3")
75+
self.assertEqual(len(q1), 2)
76+
77+
q2 = self.db.get_paths(
78+
"score_TEXT = 'zero' AND description_TEXT LIKE '%Germany%'"
79+
)
80+
self.assertEqual(len(q2), 1)
81+
7382

7483
if __name__ == "__main__":
7584
unittest.main()

0 commit comments

Comments
 (0)