krish-adi/barfi

Enhancment (with code): Add a delete_schema function in schema_loader.py

zabrewer opened this issue · 1 comments

In my testing I have many schemas and needed a way to allow users to delete and manage schemas without coding.

Schemas get somewhat unwieldy if there are many in barfi.schema. I have a multi-page streamlit app (barfi is the first page) and one of the other pages is a schema manager that utilizes barfi's schema_loader.py to allow users to view, add, or clone existing pickled schemas in the schemas.barfi file. I could write my own utility module but I'm using barfi's own schema loader functions since they already manage schema.barfi.

You don't have a contribution guide in this repo so I am adding the code here instead of making a pull request.

def delete_schema(schema_name: str):
    try:
        with open('schemas.barfi', 'rb') as handle_read:
            schemas = pickle.load(handle_read)
    except FileNotFoundError:
        schemas = {}
    if schema_name in schemas:
        del schemas[schema_name]
    else:
        raise ValueError(
            f'Schema :{schema_name}: not found in the saved schemas')
    with open('schemas.barfi', 'wb') as handle_write:
        pickle.dump(schemas, handle_write, protocol=pickle.HIGHEST_PROTOCOL)

Added this to v 0.7.0. Thanks @zabrewer :)