Package: pystackql
PyStackQL - Python Wrapper for StackQLStackQL is an open source developer tool which allows you to query and interact with cloud and SaaS provider APIs using SQL grammar.
StackQL can be used for cloud inventory analysis, cloud cost optimization, cloud security and compliance, provisioning/IaC, assurance, XOps, and more.PyStackQL is a Python wrapper for StackQL which allows you to use StackQL within Python applications and to use the power of Python to extend StackQL.
PyStackQL can be used withpandas,matplotlib,plotly,jupyterand other Python libraries to create powerful data analysis and visualization applications.For detailed documentation, including the API reference, seeRead the Docs.Installing PyStackQLPyStackQL can be installed with pip as follows:pip install pystackqlYou can install from source by cloning this repository and running a pip install command in the root directory of the repository:git clone https://github.com/stackql/pystackql
cd pystackql
pip install .Using PyStackQLThe following example demonstrates how to run a query and return the results as apandas.DataFrame:from pystackql import StackQL
import pandas as pd
region = "ap-southeast-2"
stackql = StackQL()

query = """
SELECT instanceType, COUNT(*) as num_instances
FROM aws.ec2.instances
WHERE region = '%s'
GROUP BY instanceType
""" % (region)

res = stackql.execute(query)
df = pd.read_json(res)
print(df)Using PyStackQL with Jupyter NotebookTo use the integrated Jupyter magic commands provided by PyStackQL:Load the Extension:%load_extpystackqlExecute a Query Using Line Magic:%stackqlSHOWSERVICESINazureOr Using Cell Magic:%%stackqlSELECTstatus,count(*)asnum_instancesFROMgoogle.compute.instancesWHEREproject='$project'ANDzone='$zone'GROUPBYstatusYou can find more examples in thestackql docsor the examples inreadthedocs.Supported Operating SystemsPyStackQL (and StackQL) are supported on:MacOS (arm and amd)LinuxWindowsSupported Python VersionsPyStackQL has been tested on:Python 3.7Python 3.8Python 3.9Python 3.10Python 3.11Python 3.12 (MacOS and Linux only)LicensingPyStackQL is licensed under the MIT License. The license is availablehereBuilding the docsTo build the docs, you will need to install the following packages:pip install sphinx sphinx_rtd_theme sphinx-autodoc-typehintsThen, from the root directory of the repository, run:cd docs
make htmlThe docs will be built in thedocs/build/htmldirectory.Building the packageTo build the package, you will need to install the following packages:pip install setuptools wheel twineThen, from the root directory of the repository, run:python3 setup.py sdistThe package will be built in thedistdirectory.Testing LocallyBefore testing, ensure you have all the required packages installed:pip install -r requirements.txtOnce the dependencies are installed, you can run the tests using the provided script:sh run_testsThis script sets up the necessary environment variables and then runs the unit tests.Note: Make sure to set up the environment variables in thetests/creds/env_vars/test.envfile or supply them in another way before running the tests. The tests may require specific configurations or access keys to connect to services.For better isolation and reproducibility, consider using a virtual environment:python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtOnce you’re done testing, you can deactivate the virtual environment:deactivatePublishing the packageTo publish the package to PyPI, run the following command:twine upload dist/pystackql-3.2.5.tar.gz