win_magnification._object_utils

@MaxBQb

Aug 11, 2023

Internal module
class PropertiesObserver[source]

Bases: object

Usage example:
>>> class MyPropertiesObserver(PropertiesObserver):
...     def __init__(self):
...         self.property1 = 10
...         self.property2 = 10
...         super().__init__()
...
>>> property_observer = MyPropertiesObserver()
>>> property_observer.subscribe(lambda *_: print('update'))
>>> with property_observer.batch() as value:
...     value.property1 += 1  # or property_observer.property1 += 1
...     value.property2 -= 1  # or property_observer.property2 -= 1
update
>>> property_observer.property1 += 1; property_observer.property2 -= 1
update
update
subscribe(on_change: Callable)[source]
The on_change functions will be called when a class property changes (mostly for internal use)
See example above
Parameters

on_change – Function to call

batch()[source]
Use this contextmanager to apply changes at once
See example above
class DataSource[source]

Bases: Generic[T]

Wraps interaction with outer world data sources

use_cache: bool
Save last value or get fresh one each time?
Accessors: Get Set
source() T[source]

Overridable method of getting fresh data

setter(value: T) None[source]

Overridable method of updating data

property has_cache: bool
True if last value saved, and it should be used instead of getting fresh one
Accessors: Get Readonly
property data: T
Value from datasource
When use_cache enabled stores and reuses the last value retrieved
Accessors: Get Set
classmethod dynamic(source: Callable[[], T], setter: Callable[[T], None]) WrappedFieldType[source]

Creates DataSource with source/setter specified

Parameters
  • source – New method of getting fresh data

  • setter – New method of updating data

Returns

New DataSource

classmethod const(value: T) WrappedFieldType[source]

Creates DataSource which constantly returns the same value, that can’t be changed

Parameters

value – Const to retrieve

Returns

New DataSource

WrappedFieldType

Any child of WrappedField

alias of TypeVar(‘WrappedFieldType’, bound=WrappedField)

class WrappedField(datasource: Optional[DataSource[T]] = None)[source]

Bases: PropertiesObserver, Generic[T]

Allows to get/set/get default/reset value of field wrapped
Mostly used to allow selective changes of complex fields
batch()[source]
Use this contextmanager to read/write fields at one shot
See example in parent
property default: WrappedFieldType
Default value of wrapped field
Accessors: Get Readonly
property raw: T
Raw value with no wrappers used
Accessors: Get Set Delete
Deleter: resets value with default
reset()[source]

Resets value of wrapped field to default

ensure_same(*values: T) Optional[T][source]