Skip to content

Commit e7fcf45

Browse files
committed
docstring updates
1 parent 8b5fee3 commit e7fcf45

File tree

2 files changed

+115
-130
lines changed

2 files changed

+115
-130
lines changed

docs/source/magic_ext.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Options
4141

4242
When using `StackqlMagic` as cell magic, you can pass in the following options:
4343

44-
- **--no-display** : Suppresses the display of the results. Even when this option is enabled, the results are still saved in the `stackql_df` Pandas DataFrame.
44+
- ``--no-display`` : Suppresses the display of the results. Even when this option is enabled, the results are still saved in the `stackql_df` Pandas DataFrame.
4545

4646
Example:
4747

pystackql/stackql.py

Lines changed: 114 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -15,105 +15,84 @@
1515
from io import StringIO
1616

1717
class StackQL:
18-
"""
19-
A class representing an instance of the StackQL query engine.
20-
21-
server_mode: Connect to a StackQL server.
22-
:type server_mode: bool
23-
:default: False
24-
25-
server_address: The address of the StackQL server (server_mode only).
26-
:type server_address: str
27-
:default: '127.0.0.1'
28-
29-
server_port: The port of the StackQL server (server_mode only).
30-
:type server_port: int
31-
:default: 5466
32-
33-
output: Determines the format of the output, options are 'dict', 'pandas', and 'csv'.
34-
:type output: str
35-
:default: 'dict'
36-
:options: ['dict', 'pandas', 'csv']
37-
:note: 'csv' is not supported in server_mode
38-
39-
sep: Seperator for values in CSV output (output='csv' only).
40-
:type sep: str
41-
:default: ','
42-
43-
header: Show column headers in CSV output (output='csv' only).
44-
:type header: bool
45-
:default: False
46-
47-
download_dir: The download directory for the StackQL executable (not supported in server_mode).
48-
:type download_dir: str
49-
:default: site.getuserbase()
50-
51-
api_timeout: API timeout (not supported in server_mode).
52-
:type api_timeout: int
53-
:default: 45
54-
55-
proxy_host: HTTP proxy host (not supported in server_mode).
56-
:type proxy_host: str
57-
:default: None
18+
"""A class representing an instance of the StackQL query engine.
5819
59-
proxy_password: HTTP proxy password (only applicable when proxy_host is set).
60-
:type proxy_password: str
61-
:default: None
62-
63-
proxy_port: HTTP proxy port (only applicable when proxy_host is set).
64-
:type proxy_port: int
65-
:default: -1
66-
67-
proxy_scheme: HTTP proxy scheme (only applicable when proxy_host is set).
68-
:type proxy_scheme: str
69-
:default: 'http'
70-
71-
proxy_user: HTTP proxy user (only applicable when proxy_host is set).
72-
:type proxy_user: str
73-
:default: None
74-
75-
max_results: Max results per HTTP request (not supported in server_mode).
76-
:type max_results: int
77-
:default: -1
78-
79-
page_limit: Max pages of results that will be returned per resource (not supported in server_mode).
80-
:type page_limit: int
81-
:default: 20
82-
83-
max_depth: Max depth for indirect queries: views and subqueries (not supported in server_mode).
84-
:type max_depth: int
85-
:default: 5
86-
87-
debug: Enable debug logging.
88-
:type debug: bool
89-
:default: False
90-
91-
debug_log_file: Path to debug log file (if debug is True).
92-
:type debug_log_file: str
93-
:default: ~/.pystackql/debug.log
20+
:param server_mode: Connect to a StackQL server
21+
(defaults to `False`)
22+
:type server_mode: bool, optional
23+
:param server_address: The address of the StackQL server
24+
(`server_mode` only, defaults to `'127.0.0.1'`)
25+
:type server_address: str, optional
26+
:param server_port: The port of the StackQL server
27+
(`server_mode` only, defaults to `5466`)
28+
:type server_port: int, optional
29+
:param output: Determines the format of the output, options are 'dict', 'pandas', and 'csv'
30+
(defaults to `'dict'`, `'csv'` is not supported in `server_mode`)
31+
:type output: str, optional
32+
:param sep: Seperator for values in CSV output
33+
(defaults to `','`, `output='csv'` only)
34+
:type sep: str, optional
35+
:param header: Show column headers in CSV output
36+
(defaults to `False`, `output='csv'` only)
37+
:type header: bool, optional
38+
:param download_dir: The download directory for the StackQL executable
39+
(defaults to `site.getuserbase()`, not supported in `server_mode`)
40+
:type download_dir: str, optional
41+
:param api_timeout: API timeout
42+
(defaults to `45`, not supported in `server_mode`)
43+
:type api_timeout: int, optional
44+
:param proxy_host: HTTP proxy host
45+
(not supported in `server_mode`)
46+
:type proxy_host: str, optional
47+
:param proxy_password: HTTP proxy password
48+
(only applicable when `proxy_host` is set)
49+
:type proxy_password: str, optional
50+
:param proxy_port: HTTP proxy port
51+
(defaults to `-1`, only applicable when `proxy_host` is set)
52+
:type proxy_port: int, optional
53+
:param proxy_scheme: HTTP proxy scheme
54+
(defaults to `'http'`, only applicable when `proxy_host` is set)
55+
:type proxy_scheme: str, optional
56+
:param proxy_user: HTTP proxy user
57+
(only applicable when `proxy_host` is set)
58+
:type proxy_user: str, optional
59+
:param max_results: Max results per HTTP request
60+
(defaults to `-1` for no limit, not supported in `server_mode`)
61+
:type max_results: int, optional
62+
:param page_limit: Max pages of results that will be returned per resource
63+
(defaults to `20`, not supported in `server_mode`)
64+
:type page_limit: int, optional
65+
:param max_depth: Max depth for indirect queries: views and subqueries
66+
(defaults to `5`, not supported in `server_mode`)
67+
:type max_depth: int, optional
68+
:param custom_auth: Custom StackQL provider authentication object supplied using the class constructor
69+
(not supported in `server_mode`)
70+
:type auth: dict, optional
71+
:param debug: Enable debug logging
72+
(defaults to `False`)
73+
:type debug: bool, optional
74+
:param debug_log_file: Path to debug log file
75+
(defaults to `~/.pystackql/debug.log`, only available if debug is `True`)
76+
:type debug_log_file: str, optional
9477
9578
--- Read-Only Attributes ---
9679
97-
platform: The operating system platform.
98-
:type platform: str
99-
100-
package_version: The version number of the pystackql Python package.
101-
:type package_version: str
102-
103-
version: The version number of the StackQL executable (not supported in server_mode).
104-
:type version: str
105-
106-
params: A list of command-line parameters passed to the StackQL executable (not supported in server_mode).
107-
:type params: list
108-
109-
bin_path: The full path of the StackQL executable (not supported in server_mode).
110-
:type bin_path: str
111-
112-
sha: The commit (short) sha for the installed `stackql` binary build (not supported in server_mode).
113-
:type sha: str
114-
115-
auth: Custom StackQL provider authentication object supplied using the class constructor (not supported in server_mode).
116-
:type auth: dict
80+
:param platform: The operating system platform
81+
:type platform: str, readonly
82+
:param package_version: The version number of the `pystackql` Python package
83+
:type package_version: str, readonly
84+
:param version: The version number of the `stackql` executable
85+
(not supported in `server_mode`)
86+
:type version: str, readonly
87+
:param params: A list of command-line parameters passed to the `stackql` executable
88+
(not supported in `server_mode`)
89+
:type params: list, readonly
90+
:param bin_path: The full path of the `stackql` executable
91+
(not supported in `server_mode`).
92+
:type bin_path: str, readonly
93+
:param sha: The commit (short) sha for the installed `stackql` binary build
94+
(not supported in `server_mode`).
95+
:type sha: str, readonly
11796
"""
11897

11998
def _debug_log(self, message):
@@ -122,9 +101,11 @@ def _debug_log(self, message):
122101

123102
def _connect_to_server(self):
124103
"""Establishes a connection to the server using psycopg.
125-
126-
Returns:
127-
Connection object if successful, or None if an error occurred.
104+
105+
:return: Connection object if successful, or `None` if an error occurred.
106+
:rtype: Connection or None
107+
...
108+
:raises `psycopg2.OperationalError`: Failed to connect to the server.
128109
"""
129110
try:
130111
conn = psycopg2.connect(
@@ -143,8 +124,7 @@ def _connect_to_server(self):
143124
return None
144125

145126
def _run_server_query(self, query):
146-
"""
147-
Runs a query against the server using psycopg2.
127+
"""Runs a query against the server using psycopg2.
148128
149129
:param query: SQL query to be executed on the server.
150130
:type query: str
@@ -166,8 +146,7 @@ def _run_server_query(self, query):
166146
raise
167147

168148
def _run_query(self, query, is_statement=False):
169-
"""
170-
Internal method to execute a StackQL query using a subprocess.
149+
"""Internal method to execute a StackQL query using a subprocess.
171150
172151
The method spawns a subprocess to run the StackQL binary with the specified query and parameters.
173152
It waits for the subprocess to complete and captures its stdout as the output. This approach ensures
@@ -185,7 +164,7 @@ def _run_query(self, query, is_statement=False):
185164
186165
:raises FileNotFoundError: If the StackQL binary isn't found.
187166
:raises Exception: For any other exceptions during the execution, providing a generic error message.
188-
"""
167+
"""
189168
local_params = self.params.copy()
190169
local_params.insert(1, query)
191170
try:
@@ -358,14 +337,13 @@ def __init__(self,
358337
self.params.append(proxy_scheme)
359338

360339
def properties(self):
361-
"""
362-
Retrieves the properties of the StackQL instance.
340+
"""Retrieves the properties of the StackQL instance.
363341
364342
This method collects all the attributes of the StackQL instance and
365343
returns them in a dictionary format.
366344
367-
Returns:
368-
dict: A dictionary containing the properties of the StackQL instance.
345+
:return: A dictionary containing the properties of the StackQL instance.
346+
:rtype: dict
369347
370348
Example:
371349
::
@@ -382,22 +360,17 @@ def properties(self):
382360
return props
383361

384362
def upgrade(self, showprogress=True):
385-
"""
386-
Upgrades the StackQL binary to the latest version available.
363+
"""Upgrades the StackQL binary to the latest version available.
387364
388365
This method initiates an upgrade of the StackQL binary. Post-upgrade,
389366
it updates the `version` and `sha` attributes of the StackQL instance
390367
to reflect the newly installed version.
391368
392-
Parameters:
393-
showprogress (bool, optional): Indicates if progress should be displayed
394-
during the upgrade. Defaults to True.
395-
396-
Prints:
397-
str: A message indicating the new version of StackQL post-upgrade.
369+
:param showprogress: Indicates if progress should be displayed during the upgrade. Defaults to True.
370+
:type showprogress: bool, optional
398371
399-
Example:
400-
stackql upgraded to version v0.5.396
372+
:return: A message indicating the new version of StackQL post-upgrade.
373+
:rtype: str
401374
"""
402375
_setup(self.download_dir, self.platform, showprogress)
403376
self.version, self.sha = _get_version(self.bin_path)
@@ -419,8 +392,12 @@ def executeStmt(self, query):
419392
returns a JSON string representation of the result.
420393
:rtype: str
421394
422-
Note: In `server_mode`, the method internally converts the result from the server to a
423-
JSON string before returning.
395+
Example:
396+
>>> from pystackql import StackQL
397+
>>> stackql = StackQL()
398+
>>> stackql_query = "REGISTRY PULL okta"
399+
>>> result = stackql.executeStmt(stackql_query)
400+
>>> print(result)
424401
"""
425402
if self.server_mode:
426403
# Use server mode
@@ -430,8 +407,7 @@ def executeStmt(self, query):
430407
return self._run_query(query, is_statement=True)
431408

432409
def execute(self, query):
433-
"""
434-
Executes a query using the StackQL instance and returns the output
410+
"""Executes a query using the StackQL instance and returns the output
435411
in the format specified by the `output` attribute.
436412
437413
Depending on the `server_mode` and `output` attribute of the instance,
@@ -444,11 +420,18 @@ def execute(self, query):
444420
445421
:return: The output result of the query. Depending on the `output` attribute,
446422
the result can be a dictionary, a Pandas DataFrame, or a raw CSV string.
423+
CSV output is currently not supported in `server_mode`.
447424
:rtype: dict, pd.DataFrame, or str
448425
449-
Note: In server_mode, if `output` is set to 'pandas', the result is converted into a
450-
Pandas DataFrame; otherwise, it is returned as a dictionary by default. CSV output
451-
is currently not supported in server_mode.
426+
Example:
427+
>>> from pystackql import StackQL
428+
>>> stackql = StackQL()
429+
>>> stackql_query = \"\"\"SELECT SPLIT_PART(machineType, '/', -1) as machine_type, status, COUNT(*) as num_instances
430+
... FROM google.compute.instances
431+
... WHERE project = 'stackql-demo' AND zone = 'australia-southeast1-a'
432+
... GROUP BY machine_type, status
433+
... HAVING COUNT(*) > 2\"\"\"
434+
>>> result = stackql.execute(stackql_query)
452435
"""
453436
if self.server_mode:
454437
# Use server mode
@@ -483,6 +466,8 @@ def execute(self, query):
483466
#
484467

485468
def _run_server_query_with_new_connection(self, query):
469+
"""Run a query against a StackQL postgres wire protocol server with a new connection.
470+
"""
486471
conn = None
487472
try:
488473
# Establish a new connection using credentials and configurations
@@ -513,8 +498,7 @@ def _run_server_query_with_new_connection(self, query):
513498
conn.close()
514499

515500
def _sync_query(self, query, new_connection=False):
516-
"""
517-
Synchronous function to perform the query.
501+
"""Synchronous function to perform the query.
518502
"""
519503
if self.server_mode and new_connection:
520504
# Directly get the list of dicts; no JSON string conversion needed.
@@ -532,8 +516,7 @@ def _sync_query(self, query, new_connection=False):
532516
return result
533517

534518
async def executeQueriesAsync(self, queries):
535-
"""
536-
Executes multiple StackQL queries asynchronously using the current StackQL instance.
519+
"""Executes multiple StackQL queries asynchronously using the current StackQL instance.
537520
538521
This method utilizes an asyncio event loop to concurrently run a list of provided
539522
StackQL queries. Each query is executed independently, and the combined results of
@@ -552,10 +535,12 @@ async def executeQueriesAsync(self, queries):
552535
:raises ValueError: If an unsupported output mode is selected (anything other than 'dict' or 'pandas').
553536
554537
Example:
538+
>>> from pystackql import StackQL
539+
>>> stackql = StackQL()
555540
>>> queries = [
556541
>>> "SELECT '%s' as region, instanceType, COUNT(*) as num_instances FROM aws.ec2.instances WHERE region = '%s' GROUP BY instanceType" % (region, region)
557542
>>> for region in regions ]
558-
>>> res = stackql.executeQueriesAsync(queries)
543+
>>> result = stackql.executeQueriesAsync(queries)
559544
560545
Note:
561546
- When operating in `server_mode`, this method is not supported.

0 commit comments

Comments
 (0)