Skip to content

general

Mihaly Konda edited this page Jul 11, 2025 · 5 revisions

general

Version: v1.1.2

A module for general utilities for the package.

class BijectiveDict

A custom dictionary providing bijective mapping between a main type and a hashable secondary type. It works like a two-way dictionary as in you can each key-value pair is a value-key pair as well. The keys() method returns all keys, or optionally just the primary ones, while the values() method returns all values or optionally just the secondary ones (classic keys and values).

def __init__(self, primary_type: type) -> None

Initializer for the class.

Parameters:

  • primary_type: The type of the main keys for keys() and values().

def __getitem__(self, item: Any) -> Any

Returns a value from the internal dictionary accessed with '[]' (either of the main or the secondary type).

Parameters:

  • item: The key whose associated value is to be returned.

Returns: The (primary/secondary type) value associated with the key.


def __setitem__(self, key: Any, value: Any) -> None

Sets/updates a key-value pair in a bijective way.

Parameters:

  • key: Usually the object of primary type.
  • value: Usually the object of secondary type.

def __delitem__(self, item: Any) -> None

Deletes a key-value pair in a bijective way.

Parameters:

  • item: Usually the object of primary type.

def __len__(self) -> int

Returns the adjusted size of the internal dictionary.


def __repr__(self) -> str

Returns the repr of the internal dictionary.


def keys(self, primary_only: bool = True) -> list | tuple[list, list]

Returns the keys of the internal dictionary.

Parameters:

  • primary_only: A flag to only return the keys of the primary type (default behaviour).

Returns: Either a list of keys of the primary type or additionally a list of keys of the secondary type.


def values(self, secondary_only: bool = True) -> list | tuple[list, list]

Returns the values of the internal dictionary.

Parameters:

  • secondary_only: A flag to only return the keys of the secondary type (default behaviour).

Returns: Either a list of values of the secondary type or additionally (as the first item of the return tuple) a list of values of the primary type.


class ReadOnlyDescriptor

Read-only descriptor for use with protected storage attributes.

def __set_name__(self, owner: type, name: str) -> None

Sets the name of the storage attribute.

Parameters:

  • owner: The managed class.
  • name: The managed attribute's name.

def __get__(self, instance: Any, instance_type: type = None) -> Any

Returns the value of the protected storage attribute.

Parameters:

  • instance: An instance of the managed class (managed instance).
  • instance_type: Reference to the managed class, unused here.

Returns: Either the descriptor object itself or the value in the storage attribute.


def __set__(self, instance: Any, value: Any) -> Never

Sets the value of the protected storage attribute (would, but read-only).

Parameters:

  • instance: An instance of the managed class (managed instance).
  • value: The value to set for the storage attribute.

Raises: AttributeError: Notifies the user about the attribute being read-only.


class SignalBlocker(Generic[_T])

Temporarily blocks the signals of the handled QObject.

def __init__(self, obj: _T) -> None

Initializer for the class.

Parameters:

  • obj: An object whose signals should be blocked temporarily.

def __enter__(self) -> _T

Blocks the signals of the handled QObject.

Returns: The handled object.


def __exit__(self, exc_type: Type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> None

Unblocks the signals of the handled QObject.


Example usage:

with SignalBlocker(self._cmbTestCombobox) as obj:
    obj.setCurrentIndex(3)  # The `currentIndexChanged` signal is not emitted

Note that since it uses a generic type, type checkers will see the actual type of the blocked object (obj).


class Singleton(type)

A metaclass making instance classes singletons.

def __call__(cls, *args, **kwargs) -> Any

Returns the existing instance or creates a new one.


function qt_connect()

def qt_connect(signal: Callable, slot: Callable, unique: bool = False) -> None

Connects a Qt signal to a slot method. It uses the new-style connection creation, while having a simple signature and removing the need to sprinkle 'type: ignore' around connections.

Parameters:

  • signal: The signal of a Qt-object.
  • slot: A method of a class serving as a slot for the signal.
  • unique: Whether the connection should be set to unique. The default is False.

function resource_path()

def resource_path(relative_path: str) -> str

Get absolute path to resource (for apps built with PyInstaller).

Parameters:

  • relative_path: A string representing a relative path to the requested resource.

Returns: The absolute path to the requested resource (as a simple string).


function _stub_repr_function_like()

def _stub_repr_function_like(f: cached_property | FunctionType | MethodType, class_bound: bool) -> str

Creates a stub representation for a function-like object.

Parameters:

  • f: The function-like object whose stub representation is to be made.
  • class_bound: A flag to add 'self' to the representation.

Returns: The stub representation of the input function-like object.


function stub_repr()

def stub_repr(obj: object, signals: list[str] | None = None, extra_cvs: str | None = None, add_getattr: bool = False) -> str

Creates a specifically formatted stub representation of an object. Static methods must be changed to class methods for them to be compatible with the function (which cannot yet handle static methods). Dunder methods are usually not included (apart from __init__ or when requested a special __getattr__.)

Parameters:

  • obj: The object whose stub representation is to be made.
  • signals: A list of strings representing the (Qt) signals of the object, as [sigName(carriedType1, ...)]. The default is None.
  • extra_cvs: A string of extra class variables that are not defined as CVs in the source code. The default is None.
  • add_getattr: A flag for adding a special __getattr__ method to the repr. For public Qt-based classes, it needs so that the static type checker does not warn about variables declared outside __init__. The default is False.

These stub creator functions are for internal use mainly but you can see examples for their usage at the bottom of modules in the _init_module() functions.

API reference

Base/technical modules:

Other modules

Clone this wiki locally