WhintPy 1.1

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

Module whintpy.config

Class WhintPySettings

Description

Manage the configuration settings for the WhintPy library.

This class defines default naming conventions and rules for folders and files, including separators, minimum name lengths, and invalid characters. Once initialized, the settings become immutable, enforcing consistency throughout the application. The class also supports context management, allowing temporary changes using the 'with' statement.

Attributes:

  • FOLDERNAMESEPARATOR (str): Character used to separate parts of a folder name.
  • FIELDSNAMESEPARATOR (str): Character used to separate fields within a folder name.
  • MINFILENAME_LENGTH (int): Minimum length required for a valid file name.
  • INVALIDCHARSFOR_FOLDERS (str): String of characters that are disallowed in folder names.
  • INVALIDCHARSFOR_FIELDS (str): String of characters that are disallowed in file names.
  • DOWNLOADS_FILENAME (str): Default name for the downloads file.
  • DESCRIPTION_FILENAME (str): Default name for the description file.

Constructor

Initialize the default settings for WhintPy library.

Sets default values for folder and file name separators, as well as restrictions on file name lengths and characters. After initialization, the settings are frozen to prevent modifications unless explicitly unfrozen.

View Source
def __init__(self):
    """Initialize the default settings for WhintPy library.

    Sets default values for folder and file name separators, as well as
    restrictions on file name lengths and characters. After initialization,
    the settings are frozen to prevent modifications unless explicitly
    unfrozen.

    """
    self.__dict__ = dict(FOLDER_NAME_SEPARATOR='.', FIELDS_NAME_SEPARATOR='_', MIN_FILE_NAME_LENGTH=4, INVALID_CHARS_FOR_FOLDERS='/\\.$@#%&*()[]{}<>:;,?"\'`!^+=|~', INVALID_CHARS_FOR_FIELDS='/$@#%&*()[]{}<>:;,?"\'`!^+=|~', DOWNLOADS_FILENAME='downloads.txt', DESCRIPTION_FILENAME='description.txt')
    self._is_frozen = True

Public functions

freeze

Freeze the settings to make them immutable.

Once frozen, any attempt to modify or delete attributes will raise an AttributeError.

View Source
def freeze(self):
    """Freeze the settings to make them immutable.

        Once frozen, any attempt to modify or delete attributes will raise
        an AttributeError.

        """
    super().__setattr__('_is_frozen', True)

unfreeze

Unfreeze the settings, allowing temporary modifications.

This allows attributes to be modified, but should be used with care.

View Source
def unfreeze(self):
    """Unfreeze the settings, allowing temporary modifications.

        This allows attributes to be modified, but should be used with care.

        """
    super().__setattr__('_is_frozen', False)

Overloads

__setattr__

Override the default behavior to prevent attribute modification when frozen.

Parameters
  • key: The attribute name.
  • value: The new value to set for the attribute.
Raises
  • AttributeError: If the class is frozen and an attempt is made to set an attribute.
View Source
def __setattr__(self, key, value):
    """Override the default behavior to prevent attribute modification when frozen.

        :param key: The attribute name.
        :param value: The new value to set for the attribute.
        :raises: AttributeError: If the class is frozen and an attempt is made to set an attribute.

        """
    if getattr(self, '_is_frozen', False):
        raise AttributeError(f'{self.__class__.__name__} object is immutable')
    super().__setattr__(key, value)

__delattr__

Override the default behavior to prevent deletion of attributes.

Parameters
  • key: The attribute name.
Raises
  • AttributeError: Always raised since attribute deletion is not allowed.
View Source
def __delattr__(self, key):
    """Override the default behavior to prevent deletion of attributes.

        :param key: The attribute name.
        :raises: AttributeError: Always raised since attribute deletion is not allowed.

        """
    raise AttributeError(f'{self.__class__.__name__} object does not allow attribute deletion')

__enter__

Override to allow the support the 'with' statement.

To be used for temporary settings changes.

Returns
  • the object itself for use in 'with' blocks.
View Source
def __enter__(self):
    """Override to allow the support the 'with' statement.

        To be used for temporary settings changes.
        :return: the object itself for use in 'with' blocks.

        """
    return self

__exit__

Context manager exit method, no specific handling required.

This ensures that when exiting a 'with' block, the settings remain unchanged.

Parameters
  • exc_type: The exception type.
  • exc_value: The exception value.
  • traceback: The traceback.
View Source
def __exit__(self, exc_type, exc_value, traceback):
    """Context manager exit method, no specific handling required.

        This ensures that when exiting a 'with' block, the settings remain unchanged.

        :param exc_type: The exception type.
        :param exc_value: The exception value.
        :param traceback: The traceback.

        """
    pass

__iter__

Iterate over the settings attributes.

This allows iterating through all configuration settings, providing access to each key.

Returns
  • An iterator over the dictionary keys.
View Source
def __iter__(self):
    """Iterate over the settings attributes.

        This allows iterating through all configuration settings, providing access to each key.

        :return: An iterator over the dictionary keys.

        """
    for item in self.__dict__.keys():
        yield item