Skip to content

Commit 2a340b3

Browse files
committed
jsonparser added along tests
1 parent 5da3608 commit 2a340b3

8 files changed

Lines changed: 73 additions & 63 deletions

File tree

src/rowgen/db.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/rowgen/extract_from_db.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Extracts information (schema) from a SQL database.
3+
"""

src/rowgen/hf_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from huggingface_hub import InferenceClient
22

3-
from .utils import API_KEY
3+
from rowgen.utils import API_KEY
44

55

66
class HFapi:

src/rowgen/insert_todb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
""" """

src/rowgen/json_parser.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import json
2+
3+
4+
# class JsonDB:
5+
# def __init__(self):
6+
# with open("db.json", "r") as f:
7+
# self._json_raw = json.load(f)
8+
#
9+
# def get_column_names(self):
10+
# return self._json_raw["columns"]
11+
#
12+
# def __getitem__(self, item):
13+
# return self._json_raw[item]
14+
15+
16+
class JsonParse:
17+
def __init__(self, query: str):
18+
self._raw_query = query
19+
self.processed_query = JsonParse.trun_query(self._raw_query)
20+
21+
@staticmethod
22+
def trun_query(query: str) -> str:
23+
if query.startswith("```json"):
24+
query = query.removeprefix("```json").strip()
25+
elif query.startswith("```"):
26+
query = query.removeprefix("```").strip()
27+
28+
if query.endswith("```"):
29+
query = query.removesuffix("```").strip()
30+
31+
return query
32+
33+
@property
34+
def get_processed_query(self):
35+
return self.processed_query
36+
37+
def save_to_json(self, file_path: str = "output.json"):
38+
try:
39+
data = json.loads(self.processed_query)
40+
except json.JSONDecodeError as e:
41+
print("failed to parse json", e)
42+
print(self.processed_query)
43+
return
44+
45+
with open(file_path, "w") as f:
46+
json.dump(data, f)

src/rowgen/main.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +0,0 @@
1-
import json
2-
import os
3-
4-
from huggingface_hub import InferenceClient
5-
import sqlalchemy
6-
from dotenv import load_dotenv
7-
from utils import API_KEY
8-
9-
10-
class JsonDB:
11-
def __init__(self):
12-
with open("db.json", "r") as f:
13-
self._json_raw = json.load(f)
14-
15-
def get_column_names(self):
16-
return self._json_raw["columns"]
17-
18-
def __getitem__(self, item):
19-
return self._json_raw[item]
20-
21-
22-
print(JsonDB()["columns"])
23-
24-
25-
class JsonParse:
26-
def __init__(self, query: str):
27-
self._raw_query = query
28-
self.processed_query = JsonParse.trun_query(self._raw_query)
29-
30-
@staticmethod
31-
def trun_query(query: str) -> str:
32-
if query.startswith("```json"):
33-
query = query.removeprefix("```json").strip()
34-
elif query.startswith("```"):
35-
query = query.removeprefix("```").strip()
36-
37-
if query.endswith("```"):
38-
query = query.removesuffix("```").strip()
39-
40-
return query
41-
42-
def save_to_json(self, file_path: str = "output.json"):
43-
try:
44-
data = json.loads(self.processed_query)
45-
except json.JSONDecodeError as e:
46-
print("failed to parse json", e)
47-
print(self.processed_query)
48-
return
49-
50-
with open(file_path, "w") as f:
51-
json.dump(data, f)

src/rowgen/output.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/json_parser_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
import json
3+
from rowgen.json_parser import JsonParse
4+
from rowgen.hf_api import HFapi
5+
from pytest import fixture
6+
7+
8+
@fixture
9+
def hfapi():
10+
return HFapi()
11+
12+
13+
def test_single_row_json(hfapi):
14+
raw_query = hfapi.send_message_to_api(
15+
"This is my database: id, name, username, email, access_level. generate 5 fake data rows for it. don't say anything just generate in json. i want your whole message to be just a json"
16+
)
17+
jsp = JsonParse(raw_query)
18+
try:
19+
json.loads(jsp.processed_query)
20+
return True
21+
except json.JSONDecodeError:
22+
return False

0 commit comments

Comments
 (0)