Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dictionary retrieval #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.ok and 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.
Expand Down
26 changes: 26 additions & 0 deletions src/featurebase/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down