Wraps and Lambda methods

I am working Databricks developing a ContextManager class in python to read and write new metadata for connections, catalogs, tables, etc.

As I was doing this I generalized the access pattern that seemed to be happening over and over which was get a value from Databricks. I am using the python sdk which doesn't seem to be natively geared towards to python in my opinion because it throws exceptions when a value can't be found instead of allowing me the choice to provide a default in that case and handle it or let the default operation of throwing an exception happen.

That is why I am combining lambda methods with the wraps function and setting up the explicit case to mimic the approach you would do when accessing a dictionary using the get method with a default.

Method

import databricks.sdk.service.iam as iam
from databricks.sdk import WorkspaceClient
from typing import Any, TypeVar, Callable, Tuple
from functools import wraps

T = TypeVar("T")

@staticmethod
def get_dbx_value(dbx_client: WorkspaceClient,
                    run_fn: Callable[[WorkspaceClient], T],
                    default_value: T | None = None) -> T:
    @wraps(run_fn)
    def wrapper(*args, **kwargs):
        try:
            return run_fn(*args, **kwargs)
        except Exception:
            if default_value is not None:
                warnings.warn(f"Method failed {wrapper.__name__} within Databricks. Using default value.")
                return default_value
            raise

    return wrapper(dbx_client)

Use with default

import databricks.sdk.service.iam as iam

# dbx is the current Databricks WorkspaceClient of the context manager
result = context.get_dbx_value(context.dbx, lambda z: z.users.get("abc"), iam.User())

Result

UserWarning: Method failed <lambda> within Databricks. Using default value.
  warnings.warn(f"Method failed {wrapper.__name__} within Databricks. Using default value.")

Use without default, returns default behavior (throws exception)

import databricks.sdk.service.iam as iam

try:
    # dbx is the current Databricks WorkspaceClient of the context manager
    result = context.get_dbx_value(context.dbx, lambda z: z.users.get("abc"))
except Exception e:
    # handle exception or not
    print(e)

Result with default behavior and try block

unable to parse response. This is likely a bug in the Databricks SDK for Python or the underlying API. Please report this issue with the following debugging information to the SDK issue tracker at https://github.com/databricks/databricks-sdk-go/issues. Request log:```GET /api/2.0/preview/scim/v2/Users/abc
< 403 Forbidden