Skip to main content

A Python library to simplify Hasura, GraphQL and Machine Learning

Project description

PyHasura

A library for conveniently working with Hasura, GraphQL, File Formats, and some basic Machine Learning.

Getting Started

HasuraClient

# Create Hasura Client
import os
from dotenv import load_dotenv
from pyhasura import gql_client, HasuraClient, ExportFormat
from pprint import pprint

load_dotenv()  # Load environment variables from .env

hasura_client = HasuraClient(uri=os.environ.get("HASURA_URI"), admin_secret=os.environ.get("HASURA_ADMIN_SECRET"))

Query for a Result

result = hasura_client.execute("""
        query findCarts {
            carts {
                is_complete
                cart_items {
                    quantity
                    product {
                        price
                    }
                }
            }
            cart_items {
                id
            }
        }
    """)

pprint(result)

Convert Results to a Dictionary of Alternate Formats

result = hasura_client.convert_output_format(ExportFormat.ARROW)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.CSV)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.PARQUET)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.DATAFRAME)
pprint(result)
result = hasura_client.convert_output_format(ExportFormat.FLAT)
pprint(result)

Write Results, one file for each root entry in the query

result = hasura_client.write_to_file(output_format=ExportFormat.ARROW)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.CSV)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.PARQUET)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.FLAT)
pprint(result)
result = hasura_client.write_to_file(output_format=ExportFormat.NATURAL)
pprint(result)

Detect Anomalies

Uses DictVectorizer. Assumes text is categorical, or enumerators. To Do - allow an alternate vectorizer - e.g. Word2Vec. To include more semantic meaning in anomaly detection.

result = hasura_client.anomalies()
pprint(result)
result = hasura_client.anomalies(threshold=.03)
pprint(result)

Train and Serialize then Re-Use for Anomaly Detection

Typically, do this to train on some historical dataset and then search for anomalies in an alternate (maybe current) dataset.

result = hasura_client.anomalies_training()
pprint(result)
result = hasura_client.anomalies(training_files=result, threshold=0)
pprint(result)

Clustering

Uses KMedoids clustering. You are always working on a dictionary of datasets. You need to define the number of clusters for each dataset in a corresponding input dictionary. You can auto-generate the optimal number of clusters and use that as the input.

result = hasura_client.optimal_number_of_clusters(1,8)
pprint(result)
result = hasura_client.clusters(result)
pprint(result)

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

pyhasura-1.0.21.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

pyhasura-1.0.21-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

Details for the file pyhasura-1.0.21.tar.gz.

File metadata

  • Download URL: pyhasura-1.0.21.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for pyhasura-1.0.21.tar.gz
Algorithm Hash digest
SHA256 765e3edbc723b6428db573223c43bc2e1aa2533a332486f51708214f7b0569e6
MD5 87cf6d2a71466208aae853298690cd68
BLAKE2b-256 781abb65d86020cc103d3df81fe3110d3d28849b3f37274ec1d14f337767717e

See more details on using hashes here.

File details

Details for the file pyhasura-1.0.21-py3-none-any.whl.

File metadata

  • Download URL: pyhasura-1.0.21-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for pyhasura-1.0.21-py3-none-any.whl
Algorithm Hash digest
SHA256 08d19dcdd84fab53a727f6b7b4c23a9a268bfe2e92d8d2dd725264ba136696f5
MD5 38e869b705fabf76d4b3fd859cc6a617
BLAKE2b-256 44ad5e59edfd3a66c5e132af0b12e55e8573cc8a1c574bf5a8c8f04d5a65d22e

See more details on using hashes here.

Supported by

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