Skip to content

data.queries.Queries

Bases: BaseData

The Passages class inherits from the BaseData class. It is used to handle passages of text data.

Source code in pirate/data/passages.py
class Passages(BaseData):
    """
    The Passages class inherits from the BaseData class. It is used to handle passages of text data.
    """
    def __init__(
        self,
        data: Union[str, List, Mapping],
        id_key: Optional[str] = None, 
        content_key: Optional[str] = None
    ):
        """
        Initialize the Passages object.

        Args:
            data: The data to be loaded. It can be a string (path to a file), a list, or a dictionary.
            id_key: The key used for the id in the data. Defaults to 'pid'.
            content_key: The key used for the content in the data. Defaults to 'passage'.
        """
        id_key = id_key or "pid"
        content_key = content_key or "passage"

        self.data: dict  # This is for type hinting only

        super().__init__(data, id_key, content_key)


    def get_id(self, content: str) -> Optional[str]:
        """
        Get the ID of a passage given its content.

        Args:
            content: The content of the passage.

        Returns:
            The ID of the passage if it exists, otherwise None.
        """
        for pid, passage in self.data.items():
            if passage[self.content_key] == content:
                return pid

        return None


    def add(self, content: str) -> str:
        """
        Add a passage to the data.

        Args:
            content: The content of the passage.

        Returns:
            The ID of the passage.
        """
        pid = str(len(self.data))
        self.data[pid] = {self.id_key: pid, self.content_key: content}

        return pid


    def __repr__(self):
        """ Return the string representation of the BaseData object. """
        string = textwrap.dedent(
            f"""Passages(
                data: {len(self.data)} entries
                id_key: {self.id_key}
                content_key: {self.content_key}
            )"""
        )

        return string

__init__(data, id_key=None, content_key=None)

Initialize the Passages object.

Parameters:

Name Type Description Default
data Union[str, List, Mapping]

The data to be loaded. It can be a string (path to a file), a list, or a dictionary.

required
id_key Optional[str]

The key used for the id in the data. Defaults to 'pid'.

None
content_key Optional[str]

The key used for the content in the data. Defaults to 'passage'.

None
Source code in pirate/data/passages.py
def __init__(
    self,
    data: Union[str, List, Mapping],
    id_key: Optional[str] = None, 
    content_key: Optional[str] = None
):
    """
    Initialize the Passages object.

    Args:
        data: The data to be loaded. It can be a string (path to a file), a list, or a dictionary.
        id_key: The key used for the id in the data. Defaults to 'pid'.
        content_key: The key used for the content in the data. Defaults to 'passage'.
    """
    id_key = id_key or "pid"
    content_key = content_key or "passage"

    self.data: dict  # This is for type hinting only

    super().__init__(data, id_key, content_key)

__repr__()

Return the string representation of the BaseData object.

Source code in pirate/data/passages.py
def __repr__(self):
    """ Return the string representation of the BaseData object. """
    string = textwrap.dedent(
        f"""Passages(
            data: {len(self.data)} entries
            id_key: {self.id_key}
            content_key: {self.content_key}
        )"""
    )

    return string

add(content)

Add a passage to the data.

Parameters:

Name Type Description Default
content str

The content of the passage.

required

Returns:

Type Description
str

The ID of the passage.

Source code in pirate/data/passages.py
def add(self, content: str) -> str:
    """
    Add a passage to the data.

    Args:
        content: The content of the passage.

    Returns:
        The ID of the passage.
    """
    pid = str(len(self.data))
    self.data[pid] = {self.id_key: pid, self.content_key: content}

    return pid

get_id(content)

Get the ID of a passage given its content.

Parameters:

Name Type Description Default
content str

The content of the passage.

required

Returns:

Type Description
Optional[str]

The ID of the passage if it exists, otherwise None.

Source code in pirate/data/passages.py
def get_id(self, content: str) -> Optional[str]:
    """
    Get the ID of a passage given its content.

    Args:
        content: The content of the passage.

    Returns:
        The ID of the passage if it exists, otherwise None.
    """
    for pid, passage in self.data.items():
        if passage[self.content_key] == content:
            return pid

    return None

options: show_source: false heading_level: 2