Skip to main content

QUERY FACTORY

This tool should help organizing SQL queries into python projects.

USAGE

You should seperate query template in a yaml file as in the following example:

# template.yaml
description: |
  This is a simple query for demonstration purpose.

variables:
  start_date:
    description: UTC datetime string to gather data from (inclusive)
    required: true
  end_date:
    description: UTC datetime string to gather data to (exclusive)
    required: true
  category_id:
    description: Category id to filter on. If null, filter won't apply.
    required: false
    default: null
  market:
    description: Market scope (either 'pro' or 'part').
    required: false
    default: part

query_template: |
  SELECT *
  FROM db.table
  WHERE event_date >= {{ start_date }}
  AND event_date < {{ end_date }}
  AND market = {{ market }}
  {% if category_id %}
  AND category_id = {{ category_id }}
  {% endif %}
  LIMIT = 100;

Then get your factory up and run some queries:

from query_factory import SQLQueryFactory

# factory setup.
factory = SQLQueryFactory("/path/to/template.yaml")

Factory carries some information about template as:

>>> set(factory.required_variables)
{'end_date', 'start_date'}

>>> set(factory.optional_variables)
{'category_id'}

>>> factory.describe("start_date")
'UTC datetime string to gather data from (inclusive)'

Here is how you can variabilize your queries using a factory as define above:

import pandas as pd

connection = connect_to_sql_query_engine()

data_2020_02_01 = pd.read_sql(
    factory(
        start_date="2020-02-01",
        end_date="2020-02-02"
    ),
    con=connection
)
data_2020_02_02_filtered_on_categ1 = pd.read_sql(
    factory(
        start_date="2020-02-02", 
        end_date="2020-02-03", 
        category_id="categ_1"
    ),
    con=connection
)

Metadata

Release files for query-factory 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for query-factory 0.1.0
File Size Uploaded Provenance Actions
query_factory-0.1.0.tar.gz 4.5 kB Without provenance Details for query_factory-0.1.0.tar.gz

Release files / query_factory-0.1.0.tar.gz

Download URL query_factory-0.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8e5a944540db53e9a56f834a3f7343dce0e115cdd9dd475c03bd89c663adbd76
BLAKE2b-256 checksum
How to use checksums
af6e891d9908eccbe3a5eb31fcf63ca6fe648a01212406f920e144a97e3a33c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

Security for query-factory 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page