Skip to content

Databricks Api Client Result

Represent an item from a workspace.

Source code in databricks_testing_tools/api_client_results/worskpace_object.py
class WorkspaceObject:
    """
        Represent an item from a workspace.
    """

    def __init__(self, path: str) -> None:
        """
        Init a workspace obj.

        Args:
            path (str): the path
        """
        self.path = path

__init__(self, path: str) -> None special

Init a workspace obj.

Parameters:

Name Type Description Default
path str

the path

required
Source code in databricks_testing_tools/api_client_results/worskpace_object.py
def __init__(self, path: str) -> None:
    """
    Init a workspace obj.

    Args:
        path (str): the path
    """
    self.path = path

Represent a notebook item from the workspace.

Source code in databricks_testing_tools/api_client_results/notebook_object.py
class NotebookObject(WorkspaceObject):
    """
    Represent a notebook item from the workspace.
    """

    def __init__(self, path: str) -> None:
        """
        Initialization.

        Args:
            path (str): the notebook path
        """
        self.name = self._get_notebook_name_from_path(path)
        super().__init__(path)

    @staticmethod
    def _get_notebook_name_from_path(path: str) -> str:
        """
        Get the notebook name from path.

        Args
            path (str): the notebook path

        Returns:
            str: the notebook name
        """
        segments = path.split('/')
        if len(segments) == 0:
            raise ValueError('Invalid path. Path must start /')
        name = segments[len(segments) - 1]
        return name

    @property
    def is_test_notebook(self) -> bool:
        """
        Property of notebook which name starts with 'test_'

        Returns:
            bool: True or False
        """
        return self.name.lower().startswith('test_')

is_test_notebook: bool property readonly

Property of notebook which name starts with 'test_'

Returns:

Type Description
bool

True or False

__init__(self, path: str) -> None special

Initialization.

Parameters:

Name Type Description Default
path str

the notebook path

required
Source code in databricks_testing_tools/api_client_results/notebook_object.py
def __init__(self, path: str) -> None:
    """
    Initialization.

    Args:
        path (str): the notebook path
    """
    self.name = self._get_notebook_name_from_path(path)
    super().__init__(path)

Represent a directory item from the workspace.

Source code in databricks_testing_tools/api_client_results/directory_object.py
class Directory(WorkspaceObject):
    """
    Represent a directory item from the workspace.
    """

    def __init__(self, path: str) -> None:
        """
        Initialization.

        Args:
            path (str): the directory path
        """
        super().__init__(path)

__init__(self, path: str) -> None special

Initialization.

Parameters:

Name Type Description Default
path str

the directory path

required
Source code in databricks_testing_tools/api_client_results/directory_object.py
def __init__(self, path: str) -> None:
    """
    Initialization.

    Args:
        path (str): the directory path
    """
    super().__init__(path)

Object that represent a workspace.

Source code in databricks_testing_tools/api_client_results/workspace_path.py
class WorkspacePath:
    """
    Object that represent a workspace.
    """

    def __init__(self, notebooks: List[NotebookObject], directories: List[Directory]) -> None:
        """
        Set workspace notebooks and directories.

        Args:
            notebooks (List[NotebookObject]): the list of notebooks in workspace
            directories (List[Directory]): the list of directories in workspace
        """
        self.notebooks = notebooks
        self.directories = directories
        self.test_notebooks = self._set_test_notebooks()

    @classmethod
    def from_api_response(cls, objects: Dict[str, Any]) -> 'WorkspacePath':
        """
        Create a WorkspacePath objects from json.

        Args:
            objects (Dict[str, Any]): dictionary that represents api response

        Returns:
             WorkspacePath: WorkspacePath object
        """
        notebooks = cls._set_notebooks(objects)
        directories = cls._set_directories(objects)
        return cls(notebooks, directories)

    @classmethod
    def _set_notebooks(cls, objects: Dict[str, Any]) -> List[NotebookObject]:
        """
        Get workspace notebooks from json response.

        Args:
            objects (Dict[str, Any]): dictionary that represents api response

        Returns:
             List[NotebookObject]: list of notebooks in workspace
        """
        if 'objects' not in objects:
            return []
        return [NotebookObject(obj['path']) for obj in objects['objects']
                if obj['object_type'] == 'NOTEBOOK']

    @classmethod
    def _set_directories(cls, objects: Dict[str, Any]) -> List[Directory]:
        """
        Get workspace directories from json response.

        Args:
            objects (Dict[str, Any]): dictionary that represents api response

        Returns:
             List[Directory]: list of directories in workspace
        """
        if 'objects' not in objects:
            return []
        return [Directory(obj['path']) for obj in objects['objects']
                if obj['object_type'] == 'DIRECTORY']

    def _set_test_notebooks(self) -> List[NotebookObject]:
        """
        Get workspace notebooks that start with 'test_' from json response.

        Returns:
             List[NotebookObject]: list of tests notebooks in workspace
        """
        return [notebook for notebook in self.notebooks
                if notebook.is_test_notebook]

__init__(self, notebooks: List[databricks_testing_tools.api_client_results.notebook_object.NotebookObject], directories: List[databricks_testing_tools.api_client_results.directory_object.Directory]) -> None special

Set workspace notebooks and directories.

Parameters:

Name Type Description Default
notebooks List[NotebookObject]

the list of notebooks in workspace

required
directories List[Directory]

the list of directories in workspace

required
Source code in databricks_testing_tools/api_client_results/workspace_path.py
def __init__(self, notebooks: List[NotebookObject], directories: List[Directory]) -> None:
    """
    Set workspace notebooks and directories.

    Args:
        notebooks (List[NotebookObject]): the list of notebooks in workspace
        directories (List[Directory]): the list of directories in workspace
    """
    self.notebooks = notebooks
    self.directories = directories
    self.test_notebooks = self._set_test_notebooks()

from_api_response(objects: Dict[str, Any]) -> WorkspacePath classmethod

Create a WorkspacePath objects from json.

Parameters:

Name Type Description Default
objects Dict[str, Any]

dictionary that represents api response

required

Returns:

Type Description
WorkspacePath

WorkspacePath object

Source code in databricks_testing_tools/api_client_results/workspace_path.py
@classmethod
def from_api_response(cls, objects: Dict[str, Any]) -> 'WorkspacePath':
    """
    Create a WorkspacePath objects from json.

    Args:
        objects (Dict[str, Any]): dictionary that represents api response

    Returns:
         WorkspacePath: WorkspacePath object
    """
    notebooks = cls._set_notebooks(objects)
    directories = cls._set_directories(objects)
    return cls(notebooks, directories)