Skip to content

Commit 2cd13f4

Browse files
committed
v3.6.0
1 parent b462767 commit 2cd13f4

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Changelog
22

3-
## v3.5.7 (2024-04-17)
3+
## v3.6.0 (2024-04-18)
44

55
### Updates
66

77
* modified dict response for `executeStmt`
8+
* modified error response for `execute`
89

910
## v3.5.4 (2024-04-11)
1011

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,4 @@ To publish the package to PyPI, run the following command:
194194

195195
::
196196

197-
twine upload --config-file .pypirc dist/pystackql-3.5.7.tar.gz
197+
twine upload --config-file .pypirc dist/pystackql-3.6.0.tar.gz

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# The short X.Y version
2727
version = ''
2828
# The full version, including alpha/beta/rc tags
29-
release = '3.5.7'
29+
release = '3.6.0'
3030

3131

3232
# -- General configuration ---------------------------------------------------

pystackql/stackql.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,17 @@ def _run_query(self, query):
176176
:type query: str
177177
178178
:return: The output result of the query, which can either be the actual query result or an error message.
179-
:rtype: str
179+
:rtype: dict
180+
181+
Example:
182+
::
180183
184+
{
185+
"data": "[{\"machine_type\": \"n1-standard-1\", \"status\": \"RUNNING\", \"num_instances\": 3}, ...]",
186+
"error": "stderr message if present",
187+
"exception": "ERROR: {\"exception\": \"<exception message>\", \"doc\": \"<exception doc>\", \"params\": \"<params>\", \"stdout\": \"<stdout>\", \"stderr\": \"<stderr>\"}
188+
}
189+
181190
Possible error messages include:
182191
- Indications that the StackQL binary wasn't found.
183192
- Generic error messages for other exceptions encountered during the query execution.
@@ -219,11 +228,6 @@ def _run_query(self, query):
219228
}
220229
output["exception"] = f"ERROR: {json.dumps(error_details)}"
221230

222-
# output = {'data': <stdout str>, 'error': <stderr str>, 'exception': <exception as a json string> }
223-
# for a statement you would expect 'error' to exist
224-
# for a query you would expect 'data' to exist
225-
# 'exception' in output indicates an error occurred during the execution (statement or query)
226-
227231
return output
228232

229233
def __init__(self,
@@ -487,12 +491,20 @@ def executeStmt(self, query):
487491
else:
488492
return result
489493
else:
494+
490495
# returns either...
491496
# {'error': '<error message>'} if something went wrong; or
492497
# {'message': '<message>'} if the statement was executed successfully
498+
493499
result = self._run_query(query)
494500
if "exception" in result:
495-
return {"error": result["exception"]}
501+
exception_msg = result["exception"]
502+
if self.output == 'pandas':
503+
return pd.DataFrame({'error': [exception_msg]}) if exception_msg else pd.DataFrame({'error': []})
504+
elif self.output == 'csv':
505+
return exception_msg
506+
else:
507+
return {"error": exception_msg}
496508

497509
# message on stderr
498510
message = result["error"]
@@ -543,35 +555,57 @@ def execute(self, query, suppress_errors=True):
543555
return result
544556
else:
545557
# returns either...
546-
# {'error': <error json str>} if something went wrong; or
547-
# [{<data>}] if the statement was executed successfully, messages to stderr are ignored
558+
# [{'error': <error json str>}] if something went wrong; or
559+
# [{<row1>},...] if the statement was executed successfully, messages to stderr are ignored
548560

549561
output = self._run_query(query)
550562
if "exception" in output:
551-
return {"error": output["exception"]}
563+
exception_msg = output["exception"]
564+
if self.output == 'pandas':
565+
return pd.DataFrame({'error': [exception_msg]}) if exception_msg else pd.DataFrame({'error': []})
566+
elif self.output == 'csv':
567+
return exception_msg
568+
else:
569+
return [{"error": exception_msg}]
552570

553571
if "data" in output:
572+
data = output["data"]
554573
# theres data, return it
555574
if self.output == 'csv':
556-
return output["data"]
575+
return data
557576
elif self.output == 'pandas':
558577
try:
559-
return pd.read_json(StringIO(output["data"]))
578+
return pd.read_json(StringIO(data))
560579
except ValueError:
561580
return pd.DataFrame([{"error": "Invalid JSON output"}])
562581
else: # Assume 'dict' output
563582
try:
564-
return json.loads(output["data"])
583+
return json.loads(data)
565584
except ValueError:
566-
return {"error": f"Invalid JSON output : {output['data']}"}
567-
else:
568-
if "error" in output:
569-
if suppress_errors:
570-
return []
571-
else:
572-
return {"error": output["error"]}
585+
return [{"error": f"Invalid JSON output : {data}"}]
586+
587+
if "error" in output:
588+
# theres no data but there is stderr from the request, could be an expected error like a 404
589+
if suppress_errors:
590+
# we dont care about errors, return an empty list
591+
csv_ret = ""
592+
pd_ret = pd.DataFrame()
593+
dict_ret = []
594+
else:
595+
# we care about errors, return the error
596+
err_msg = output["error"]
597+
csv_ret = err_msg
598+
pd_ret = pd.DataFrame([{"error": err_msg}])
599+
dict_ret = [{"error": err_msg}]
600+
601+
if self.output == 'csv':
602+
return csv_ret
603+
elif self.output == 'pandas':
604+
return pd_ret
605+
else: # Assume 'dict' output
606+
return dict_ret
607+
573608

574-
#
575609
# asnyc query support
576610
#
577611

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name='pystackql',
13-
version='3.5.7',
13+
version='3.6.0',
1414
description='A Python interface for StackQL',
1515
long_description=readme,
1616
author='Jeffrey Aven',

0 commit comments

Comments
 (0)