Skip to content

qass.tools.analyzer.testing

Classes:

Name Description
MockBuffer

Mock buffer class that is able to parse JSON encoded files.

MockBuffer dataclass

Mock buffer class that is able to parse JSON encoded files. All attributes in the JSON are added as fields in the object.

Note

Currently this only allows the mocking of header attributes

Example
# This object can either be created using the constructor
from qass.tools.analyzer.testing import MockBuffer
buffer = MockBuffer("/my/path/to/file", header_hash="0", process=1)
Example
# By writing the desired attributes to a json file
import json
from pathlib import Path

from qass.tools.analyzer.testing import MockBuffer

file = Path("testfile.json")
with open("file", "w") as f:
    json.dump({"header_hash": "0", "process": 1})
buffer = MockBuffer(file)
# this step will load the json file into the dataclass
with buffer:
    pass

Methods:

Name Description
__enter__

Opens the file specified by self.filepath, parses its JSON content,

__exit__
__init__

Attributes:

Name Type Description
filepath
Source code in src/qass/tools/analyzer/testing.py
@dataclass
class MockBuffer:
    """Mock buffer class that is able to parse JSON encoded files.
    All attributes in the JSON are added as fields in the object.

    Note
    ----
    Currently this only allows the mocking of header attributes

    Example
    -------
    ```py
    # This object can either be created using the constructor
    from qass.tools.analyzer.testing import MockBuffer
    buffer = MockBuffer("/my/path/to/file", header_hash="0", process=1)
    ```

    Example
    -------
    ```py
    # By writing the desired attributes to a json file
    import json
    from pathlib import Path

    from qass.tools.analyzer.testing import MockBuffer

    file = Path("testfile.json")
    with open("file", "w") as f:
        json.dump({"header_hash": "0", "process": 1})
    buffer = MockBuffer(file)
    # this step will load the json file into the dataclass
    with buffer:
        pass
    ```
    """

    def __init__(self, filepath: Union[Path, str], **kwargs):
        self.filepath = filepath
        for key, value in kwargs.items():
            setattr(self, key, value)

    def __enter__(self):
        """
        Opens the file specified by ``self.filepath``, parses its JSON content,
        and populates the instance attributes with the loaded data. All key-value
        pairs from the JSON file are dynamically set as attributes on this instance.

        :returns: The instance itself with attributes populated from the file.
        :rtype: self

        Raises
        ------
        InvalidFileError
            If the file is not a valid Buffer JSON file.
        FileNotFoundError
            If the specified filepath does not exist.
        PermissionError
            If there are insufficient permissions to read the file.

        Note
        ----
        This method is automatically called when entering a ``with`` statement.

        Example
        -------
        ```py
        with BufferObject() as buffer:
            # buffer attributes are now loaded from file
            print(buffer.data)
        ```
        """
        with open(self.filepath, "r") as f:
            try:
                data = json.load(f)
            except Exception:
                raise InvalidFileError("Not a Buffer file")
        for key, value in data.items():
            setattr(self, key, value)
        return self

    def __exit__(self, *args, **kwargs):
        pass

filepath instance-attribute

filepath = filepath

__enter__

__enter__()

Opens the file specified by self.filepath, parses its JSON content, and populates the instance attributes with the loaded data. All key-value pairs from the JSON file are dynamically set as attributes on this instance.

:returns: The instance itself with attributes populated from the file. :rtype: self

Raises:

Type Description
InvalidFileError

If the file is not a valid Buffer JSON file.

FileNotFoundError

If the specified filepath does not exist.

PermissionError

If there are insufficient permissions to read the file.

Note

This method is automatically called when entering a with statement.

Example
with BufferObject() as buffer:
    # buffer attributes are now loaded from file
    print(buffer.data)
Source code in src/qass/tools/analyzer/testing.py
def __enter__(self):
    """
    Opens the file specified by ``self.filepath``, parses its JSON content,
    and populates the instance attributes with the loaded data. All key-value
    pairs from the JSON file are dynamically set as attributes on this instance.

    :returns: The instance itself with attributes populated from the file.
    :rtype: self

    Raises
    ------
    InvalidFileError
        If the file is not a valid Buffer JSON file.
    FileNotFoundError
        If the specified filepath does not exist.
    PermissionError
        If there are insufficient permissions to read the file.

    Note
    ----
    This method is automatically called when entering a ``with`` statement.

    Example
    -------
    ```py
    with BufferObject() as buffer:
        # buffer attributes are now loaded from file
        print(buffer.data)
    ```
    """
    with open(self.filepath, "r") as f:
        try:
            data = json.load(f)
        except Exception:
            raise InvalidFileError("Not a Buffer file")
    for key, value in data.items():
        setattr(self, key, value)
    return self

__exit__

__exit__(*args, **kwargs)
Source code in src/qass/tools/analyzer/testing.py
def __exit__(self, *args, **kwargs):
    pass

__init__

__init__(filepath: Union[Path, str], **kwargs)
Source code in src/qass/tools/analyzer/testing.py
def __init__(self, filepath: Union[Path, str], **kwargs):
    self.filepath = filepath
    for key, value in kwargs.items():
        setattr(self, key, value)