From da38b5b959eef4f04c9c18acc2db8d0e021999c9 Mon Sep 17 00:00:00 2001 From: Kord Campbell Date: Tue, 13 Jun 2023 18:16:48 -0500 Subject: [PATCH 1/2] .gitignore, implement dict return, README update --- .gitignore | 14 ++++++++++++++ README.md | 7 ++++++- src/featurebase/client.py | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cad9007 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + diff --git a/README.md b/README.md index 2a624d0..2d14ba4 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,15 @@ executing queries as shown in the following examples. client = featurebase.client() # query the endpoint with SQL - result = client.query("SELECT * from demo;") + result = client.query("SELECT * FROM demo;") if result.ok: print(result.data) + # get result dicts from the endpoint query with SQL + result = client.query("SELECT * FROM demo;") + if result.dict: + print(result.dict) + # query the endpoint with a batch of SQLs, running the SQLs synchronously # Synchronous run best suited for executing DDL and DMLs that need to follow specific run order # passing the optional parameter "stoponerror=True" will stop execution at the failed SQL and the remaining SQLs in the list will not be executed. diff --git a/src/featurebase/client.py b/src/featurebase/client.py index ba3b3ae..47e2cd0 100644 --- a/src/featurebase/client.py +++ b/src/featurebase/client.py @@ -3,6 +3,25 @@ import urllib.request import urllib.error +# apply schema to list of dicts +def apply_schema(list_of_lists, schema): + # build field names + field_names = [] + + # add them from the schema + for field in schema.get('fields'): + field_names.append(field.get('name')) + + # build the dicts + result = [] + for row in list_of_lists: + dict_row = {} + for i, val in enumerate(row): + dict_row[field_names[i]] = val + result.append(dict_row) + return result + + # client represents a http connection to the FeatureBase sql endpoint. class client: """Client represents a http connection to the FeatureBase sql endpoint. @@ -144,11 +163,13 @@ def __init__(self, sql, response, code, reason): self.ok=False self.schema=None self.data=None + self.dict=None self.error=None self.warnings=None self.execution_time=0 self.sql=sql self.ok=code==200 + if self.ok: try: result=json.loads(response) @@ -160,6 +181,11 @@ def __init__(self, sql, response, code, reason): self.data=result.get('data') self.warnings=result.get('warnings') self.execution_time=result.get('execution-time') + + # Apply schema to the data + if self.schema and self.data: + self.dict = apply_schema(self.data, self.schema) + except json.JSONDecodeError as exc: self.ok=False self.error=error(500, 'JSON error. ' + str(response)) From 03b57c78485f52c8c437bf2b6a20e7d44cd27d47 Mon Sep 17 00:00:00 2001 From: Kord Campbell Date: Tue, 13 Jun 2023 18:18:12 -0500 Subject: [PATCH 2/2] README touchup --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d14ba4..8df9554 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ executing queries as shown in the following examples. # get result dicts from the endpoint query with SQL result = client.query("SELECT * FROM demo;") - if result.dict: + if result.ok and result.dict: print(result.dict) # query the endpoint with a batch of SQLs, running the SQLs synchronously