Skip to content

Commit da38b5b

Browse files
committed
.gitignore, implement dict return, README update
1 parent 132f421 commit da38b5b

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# Environments
7+
.env
8+
.venv
9+
env/
10+
venv/
11+
ENV/
12+
env.bak/
13+
venv.bak/
14+

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ executing queries as shown in the following examples.
1616
client = featurebase.client()
1717

1818
# query the endpoint with SQL
19-
result = client.query("SELECT * from demo;")
19+
result = client.query("SELECT * FROM demo;")
2020
if result.ok:
2121
print(result.data)
2222

23+
# get result dicts from the endpoint query with SQL
24+
result = client.query("SELECT * FROM demo;")
25+
if result.dict:
26+
print(result.dict)
27+
2328
# query the endpoint with a batch of SQLs, running the SQLs synchronously
2429
# Synchronous run best suited for executing DDL and DMLs that need to follow specific run order
2530
# passing the optional parameter "stoponerror=True" will stop execution at the failed SQL and the remaining SQLs in the list will not be executed.

src/featurebase/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33
import urllib.request
44
import urllib.error
55

6+
# apply schema to list of dicts
7+
def apply_schema(list_of_lists, schema):
8+
# build field names
9+
field_names = []
10+
11+
# add them from the schema
12+
for field in schema.get('fields'):
13+
field_names.append(field.get('name'))
14+
15+
# build the dicts
16+
result = []
17+
for row in list_of_lists:
18+
dict_row = {}
19+
for i, val in enumerate(row):
20+
dict_row[field_names[i]] = val
21+
result.append(dict_row)
22+
return result
23+
24+
625
# client represents a http connection to the FeatureBase sql endpoint.
726
class client:
827
"""Client represents a http connection to the FeatureBase sql endpoint.
@@ -144,11 +163,13 @@ def __init__(self, sql, response, code, reason):
144163
self.ok=False
145164
self.schema=None
146165
self.data=None
166+
self.dict=None
147167
self.error=None
148168
self.warnings=None
149169
self.execution_time=0
150170
self.sql=sql
151171
self.ok=code==200
172+
152173
if self.ok:
153174
try:
154175
result=json.loads(response)
@@ -160,6 +181,11 @@ def __init__(self, sql, response, code, reason):
160181
self.data=result.get('data')
161182
self.warnings=result.get('warnings')
162183
self.execution_time=result.get('execution-time')
184+
185+
# Apply schema to the data
186+
if self.schema and self.data:
187+
self.dict = apply_schema(self.data, self.schema)
188+
163189
except json.JSONDecodeError as exc:
164190
self.ok=False
165191
self.error=error(500, 'JSON error. ' + str(response))

0 commit comments

Comments
 (0)