Skip to main content

A Python interface for StackQL

Project description

"stackql logo"

PyStackQL - Python Wrapper for StackQL

Documentation Status PyPI PyPI - Downloads

StackQL 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 with pandas, matplotlib, plotly, jupyter and other Python libraries to create powerful data analysis and visualization applications.

For detailed documentation, including the API reference, see Read the Docs.

Installing PyStackQL

PyStackQL can be installed with pip as follows:

pip install pystackql

You 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 PyStackQL

The following example demonstrates how to run a query and return the results as a pandas.DataFrame:

from pystackql import StackQL
region = "ap-southeast-2"
stackql = StackQL(output='pandas')

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

df = stackql.execute(query)
print(df)

Using PyStackQL with Jupyter Notebook

To use the integrated Jupyter magic commands provided by PyStackQL:

  1. Load the Extension:

%load_ext pystackql.magic
  1. Execute a Query Using Line Magic:

%stackql SHOW SERVICES IN azure
  1. Or Using Cell Magic:

%%stackql
SELECT status, count(*) as num_instances
FROM google.compute.instances
WHERE project = '$project'
AND zone = '$zone'
GROUP BY status

You can find more examples in the stackql docs or the examples in readthedocs.

Supported Operating Systems

PyStackQL (and StackQL) are supported on:

  • MacOS (arm and amd)

  • Linux

  • Windows

Supported Python Versions

PyStackQL has been tested on:

  • Python 3.9

  • Python 3.10

  • Python 3.11

  • Python 3.12

  • Python 3.13

Licensing

PyStackQL is licensed under the MIT License. The license is available here

Building the docs

To build the docs, you will need to install the following packages:

pip install sphinx sphinx_rtd_theme sphinx-autodoc-typehints

Then, from the root directory of the repository, run:

cd docs
make html

The docs will be built in the docs/build/html directory.

Building the package

To build the package, you will need to install the following packages:

pip install build

Then, from the root directory of the repository, run:

rm -rf dist/*
python3 -m build

The package will be built in the dist directory.

Testing Locally

Before testing, ensure you have all the required packages installed:

pip install -r requirements.txt
pip install psycopg

Once the dependencies are installed, you can run the tests using the provided script:

sh run_tests

This script sets up the necessary environment variables and then runs the unit tests.

Note: Make sure to set up the environment variables in the tests/creds/env_vars/test.env file 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.txt

Once you’re done testing, you can deactivate the virtual environment:

deactivate

Publishing the package

To publish the package to PyPI, run the following command:

twine upload --config-file .pypirc dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystackql-3.8.2.tar.gz (46.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pystackql-3.8.2-py3-none-any.whl (37.2 kB view details)

Uploaded Python 3

File details

Details for the file pystackql-3.8.2.tar.gz.

File metadata

  • Download URL: pystackql-3.8.2.tar.gz
  • Upload date:
  • Size: 46.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pystackql-3.8.2.tar.gz
Algorithm Hash digest
SHA256 cce8c85adde216a3c96feb9bec0065d6bfade630b232f2f5cf9a7d448c382157
MD5 837529e5953daa6a542c1a508b49ff9b
BLAKE2b-256 ef9b22c9f210187e7bce2efb36ff67dd60b03c7c83a4c9dc4c4d3b550c845793

See more details on using hashes here.

File details

Details for the file pystackql-3.8.2-py3-none-any.whl.

File metadata

  • Download URL: pystackql-3.8.2-py3-none-any.whl
  • Upload date:
  • Size: 37.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for pystackql-3.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d981865419849c2721b3ceda86f9941148b746c8c6019ecd00592f5798d489b8
MD5 97fe723676e2ba287ec5fbb03d3bec33
BLAKE2b-256 4e54f683377b20cc4f972a3b9c08f166834f3975048938b783d45f21af6983bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page