Spaces:
Sleeping
Sleeping
File size: 2,251 Bytes
ceed47a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
class _C2G_CONFIG:
"""
This class provides functionalities for managing configurations and modules within the C2G system.
Methods
clean_config(): Resets the configuration dictionary.
clean_dir(): Clears the module directory and module list.
display_available_modules(): Prints the names of available modules.
update_inverse_list(): Updates the inverse dictionary of module names.
Attributes:
_C: A dictionary storing configuration settings.
_dir: A dictionary mapping module names to their corresponding objects.
_module_list: A list containing names of registered modules.
Example:
```
>>> @register_module('add')
>>> def add(a, b):
>>> return a+b
>>> add(-10, 12)
2
>>> cfg.display_available_modules()
Available Modules:
ADD
>>> cfg.clean_dir()
>>> cfg.display_available_modules()
Available Modules:
```
"""
_C = {}
_dir = {}
_module_list = []
def clean_config(self):
self._C = {}
def clean_dir(self):
self._dir = {}
self._module_list = []
def display_available_modules(self):
print('Available Modules:')
for module_name, _ in self._dir.items():
print(module_name)
def update_inverse_list(self):
self._inv_dir = {value : key for key, value in self._dir.items()}
class register_module:
"""
This class is a decorator used for registering modules within the C2G system.
Methods
__init__(self, name): Initializes the module with a given name.
__call__(self, module): Registers the module with the provided name and updates the module list.
Examples
```
>>> @register_module('add')
>>> def add(a, b):
>>> return a+b
>>> add(-10, 12)
2
>>> cfg.display_available_modules()
Available Modules:
ADD
```
"""
def __init__(self, name):
self.name = name.upper()
def __call__(self, module):
cfg._dir[self.name] = module
cfg.update_inverse_list()
cfg._module_list.append(self.name)
return cfg._dir[self.name]
|