libb.OverrideModuleGetattr

class OverrideModuleGetattr(wrapped, override)[source]

Bases: object

Wrapper to override __getattr__ of a Python module.

Allows dynamic attribute access for modules, typically used for config.py settings. Looks up attributes in an override module before falling back to the wrapped module.

Parameters:
  • wrapped (ModuleType) – The original module to wrap.

  • override (ModuleType) – The override module to check first.

Config.py Example:

self = OverrideModuleGetattr(sys.modules[__name__], local_config)
sys.modules[__name__] = self

Usage Example:

>>> from libb import Setting
>>> create_mock_module('config', {'foo': Setting(bar=1)})
>>> original_config = sys.modules['config']

>>> override_config = ModuleType('override_config')
>>> override_config.foo = Setting(bar=2)

>>> wrapped_config = OverrideModuleGetattr('config', override_config)
>>> sys.modules['config'] = wrapped_config # important!

>>> import config
>>> assert config.foo.bar == 2

>>> sys.modules['config'] = original_config
>>> import config
>>> assert config.foo.bar == 1

>>> del sys.modules['config']  # cleanup
__getattr__(name)[source]

Get attribute, checking override module first then wrapped module.

__getitem__(name)[source]

Allow dynamic module lookups like config[‘bloomberg.data’].