Customise supported formats

By default, the extensions supported by the loader/saver the following: csv, docx, jpg, json, md, parquet, pdf, pickle, png, pptx, sql, toml, txt, xlsx, xml, yaml, yml.

However, through the extra_loader_config and extra_saver_config parameters, Easy Environment allows you to customise any file format and integrate it into the tool.

  1. Create a loader/saver function: create a loader or saver function for the corresponding extension.

A load function must contain the path argument, which represents the path to the file. It is also recommanded to pass a **kwargs arguments in order to allows greater flexibility when calling the loader though the EasyEnvironment instance.

def new_format_loader(path, **kwargs):
    # Loading process
    func.load(path, **kwargs)
        
    return object

A save function must contain the path and obj arguments, representing respectively the object to be saved and the access path to which to save.

def new_format_saver(obj, path, **kwargs):
    # saving process
    func.save(path, **kwargs)
    ...
  1. Initialise Easy Environment: create an instance of the EasyEnvironment class by specifying your loader and/or saver functions.

Loader and saver functions must be specified by a dictionnary in which the keys correspond to the extensions and the values correspond to a tuple. The tuple contains two elements : the created function and a string representing the reading/writing mode (either "rb" or "rt" for a loader / either "wb" or "wt" for a saver).

envi = EasyEnvironment(
    local_path='path/to/project/root',
    
    gcloud_project_id='your-project-id',
    gcloud_credential_path="path/to/credentials.json",
    GCS_path='gs://your-bucket-name/',
    
    extra_loader_config={'new_format': (new_format_loader, 'rb')},
    extra_saver_config={'new_format': (new_format_saver, 'wb')}
)

Example of customisation:

In the above example, you can specify custom loader and saver functions for file extensions not covered by the default configurations. Just replace txt_loader_function and txt_saver_function with your own loader and saver functions, respectively.

Make sure to import the necessary functions and libraries for your custom loaders and savers.

# Defining loader and saver functions

def txt_loader(path, **kwargs):
    return path.read(**kwargs)

def txt_saver(obj, path, **kwargs):
    path.write(obj, **kwargs)

def json_loader(path, **kwargs):
    import json
    return json.load(path, **kwargs)

def json_saver(obj, path, **kwargs):
    import json
    json.dump(obj, path, **kwargs)

# Creating an instance of the EasyEnvironment class specifying the loader and saver
extra_loader_config = {
    'json': (json_loader, 'rt'),
    'txt':  (txt_loader, 'rt')
}

extra_writer_config = {
    'json': (json_writer, 'wt'),
    'txt':  (txt_writer, 'wt')
}

envi = EasyEnvironment(
    local_path='path/to/project/root',
    
    gcloud_project_id='your-project-id',
    gcloud_credential_path="path/to/credentials.json",
    GCS_path='gs://your-bucket-name/',
    
    extra_loader_config=extra_loader_config ,
    extra_saver_config=extra_saver_config 
)

# Using the tool
my_text = envi.local.load("folder/my_text.txt")
my_dict = {"text": my_text}
envi.local.save("folder/my_json.json")

Other examples of loader/saver functions are available in Source Code.

Last updated