Git Product home page Git Product logo

useful's Introduction

Quick Start

  1. pip install -q git+https://github.com/mberk06/useful.git
  2. Run one of the exmaple usages below

Files

useful/api_client.py

This api client is a wrapper around the requests library and provides the following functionality. To implement, you should follow steps 1-4 in the comments below.

Here are the core advantages of using this over the basic requests library:

  • Session Management: by leveraging a context manager, request sessions will be properly closed.
  • Auth Management: by simply passing a token in a SecretStr, you don't have to think about auth after that.
  • Error Handling: there is built-in logic to identify retryable exceptions based on HTTP error codes.
  • Retrying Mechanism: retry logic is built in. Max retries is 5 and there is exponential backoff.
  • Logging: API calls, retries, and errors are logged.
from pydantic import SecretStr
from useful import Client

class DatabricksClient(Client):
    def __init__(self, host: str, token: SecretStr):
        super().__init__(host, token)

    # Step 1: add use-case-specific methods here
    def get_warehouse_list(self) -> dict:
        return self._execute(
            http_command="GET",
            endpoint="/api/2.0/sql/warehouses",
        )
    
databricks_client = DatabricksClient(
    host="YOUR_DATABRICKS_WORKSPACE_URL", # Step 2: add databricks host
    token=SecretStr(dbutils.secrets.get(scope="berk-scope", key="pat")) # Step 3: add your PAT as a SecretStr
)

# Step 4: call the functions
warehouse_list = databricks_client.get_warehouse_list()
print(warehouse_list)

useful/add_to_databricks_secrets.py

This module lets you add a secret to a given workspace's secrets API. It's most secure to run this in a CLI on your local machine because notebooks auto-save and thereby will store your credentials, but you can technically run this anywhere.

from pydantic import SecretStr
from useful import AddSecretToDatabricksAPI

databricks_client = AddSecretToDatabricksAPI(host="XXX", token=SecretStr("XXX"))
databricks_client.put_secret_safe(scope="berk", key="pat", value=SecretStr("XXX"))

useful/checkpoint.py

This module allows you to incrementally store a json-serializable dataclass object in a JSON file.

from useful import Checkpoint, ExampleDataclass

# Step 1: overwrite ExampleDataclass. It must be imported into the checkpoint.py file and all references must be modified.

# Step 2: run the checkpoint
checkpoint = Checkpoint("x.json", 2)
checkpoint.append(dataclass_object_1)
checkpoint.append(dataclass_object_2)
assert checkpoint.is_complete() == 2

# Step 3: delete the checkpoint manually

useful/log.py

Example get_or_create singleton for instantiating a logger.

from useful.log import get_or_create_logger

_logger = get_or_create_logger()

useful's People

Contributors

michael-berk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.