WhintPy 1.1

https://sourceforge.net/projects/whintpy/

Module whintpy.deposit

Class ImmutableDocument

Description

Ensure instances become immutable after their creation.

This is achieved by using isfrozen attribute to True after the instance is created, preventing further modifications to its attributes.

Example
 >>> doc = ImmutableDocument(
 >>> author="Alice",
 >>> filename="Doc1.txt",
 >>> content="a cool content",
 >>> date=datetime.date(2024, 1, 1),
 >>> filetype="txt")
 >>> print(doc.author)
 > "Alice"
 >>> doc.author = "Bob"  # Raises AttributeError
 >>> del doc.author  # Raises AttributeError

Constructor

View Source
def __init__(self, author: str, filename: str, date: datetime.date | datetime.datetime | None=None, content: str | bytes='', description: str='', downloads: int=0):
    self._is_frozen = False
    self.author = DocumentUtils.format_author(author)
    self.content = content
    self.date = date
    self.description = description
    self.filename = DocumentUtils.format_filename(filename)
    self.filetype = DocumentUtils.get_filetype(filename)
    self.downloads = downloads
    self._is_frozen = True

Public functions

get_folder_name

Return the name of the folder in which the document is stored.

The folder_name can be used for both:

  • an identifier for the document, and
  • get information about the document: author, filename, date and filetype
View Source
def get_folder_name(self) -> str:
    """Return the name of the folder in which the document is stored.

        The folder_name can be used for both:

        - an identifier for the document, and
        - get information about the document: author, filename, date and filetype

        """
    return DocumentUtils.get_folder_name(self.author, self.filename, self.date, self.filetype)

to_immutable

View Source
def to_immutable(self) -> ImmutableDocument:
    return self

Overloads

__setattr__

Override to prevent any attribute setter.

Parameters
  • key
  • value
View Source
def __setattr__(self, key, value):
    """Override to prevent any attribute setter."""
    if getattr(self, '_is_frozen', False):
        raise AttributeError(f'{self.__class__.__name__} object is immutable')
    super().__setattr__(key, value)

__delattr__

Override to prevent any attribute deletion.

Parameters
  • key
View Source
def __delattr__(self, key):
    """Override to prevent any attribute deletion."""
    if getattr(self, '_is_frozen', False):
        raise AttributeError(f'{self.__class__.__name__} object is immutable')
    super().__delattr__(key)

__str__

View Source
def __str__(self):
    return f'ImmutableDocument({self.author}, {self.filename}, {self.date}, {self.filetype})'

__repr__

View Source
def __repr__(self):
    return f'ImmutableDocument(author={self.author}, filename={self.filename}, date={self.date}, filetype={self.filetype})'