olivia_finder.utilities
Subpackage utilities
The utility package is designed to contain source code that is used in the library on a recurring basis, and whose functionality is to provide a certain utility to the library.
Module structure
Package structure
../../olivia_finder/olivia_finder/utilities
├── config.py
├── exception.py
├── __init__.py
├── logger.py
├── singleton_decorator.py
└── utilities.py
Module utilities.config
Provides the configuration class, which is used to obtain the configuration variables defined in the .ini configuration file
It is a Singleton instance so only one instance is accessible from any part of the code through the constructor
from olivia_finder.utilities.config import Configuration
You need to provide a configuration file
The configuration file is located in the root of the olivia_finder package
In this execution we are using a personalized configuration file for demonstration
!cat ./config.ini
[olivia_finder_logger]
; Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
name = olivia_finder
level = DEBUG
status = ENABLED
file_handler = ENABLED
console_handler = ENABLED
; Myrequests configuration
[logger_myrequests]
name = olivia_finder.myrequests
level = DEBUG
status = DISABLED
file_handler = DISABLED
console_handler = DISABLED
; Package Manager configuration
[logger_packagemanager]
name = olivia_finder.packagemanager
level = DEBUG
status = DISABLED
file_handler = DISABLED
console_handler = DISABLED
; CSV Datasource configuration
[logger_datasource]
name = olivia_finder.datasource
level = DEBUG
status = DISABLED
file_handler = DISABLED
console_handler = DISABLED
; Output directory for the results
[folders]
logger = logs/
working_dir = results/working/
; Libraries.io configuration
[librariesio]
api_key = 558f419425861e607e78cd4e3a0b129b
Acess the keys using get_key method
Configuration().get_key('olivia_finder_logger', 'status')
``
'ENABLED'
## Module `utilities.logger`
```python
from olivia_finder.utilities.logger import MyLogger
The class MyLogger implements an utility loging tools to register the actions of execution. It is based on the default Python logging module
logger = MyLogger.get_logger("olivia_finder_logger")
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
# Acess the logger using the default logging class
import logging
logging.getLogger(logger.name).debug('Debug message')
[32;20m2023-06-28 19:26:34,316 [olivia_finder(DEBUG)] -> 4423775.py:3[0m
Debug message
[34;20m2023-06-28 19:26:34,318 [olivia_finder(INFO)] -> 4423775.py:4[0m
Info message
[33;20m2023-06-28 19:26:34,318 [olivia_finder(WARNING)] -> 4423775.py:5[0m
Warning message
[38;5;208;20m2023-06-28 19:26:34,319 [olivia_finder(ERROR)] -> 4423775.py:6[0m
Error message
[31;20m2023-06-28 19:26:34,319 [olivia_finder(CRITICAL)] -> 4423775.py:7[0m
Critical message
[32;20m2023-06-28 19:26:34,320 [olivia_finder(DEBUG)] -> 4423775.py:10[0m
Debug message
[32;20m2023-06-28 19:26:34,320 [olivia_finder(DEBUG)] -> 4423775.py:14[0m
Debug message
Default logger is root
# The default logger is root and it is not configured
MyLogger.get_logger().debug('Debug message')
MyLogger.get_logger().info('Info message')
MyLogger.get_logger().warning('Warning message')
MyLogger.get_logger().error('Error message')
MyLogger.get_logger().critical('Critical message')
Warning message
Error message
Critical message
Change log level
MyLogger.configure_level("olivia_finder_logger", 'console', 'warning')
logger = MyLogger.get_logger("olivia_finder_logger")
logger.debug('Debug message')
logger.info('Info message')
logger.warning('Warning message')
logger.error('Error message')
logger.critical('Critical message')
[33;20m2023-06-28 19:26:34,331 [olivia_finder(WARNING)] -> 446635553.py:4[0m
Warning message
[38;5;208;20m2023-06-28 19:26:34,332 [olivia_finder(ERROR)] -> 446635553.py:5[0m
Error message
[31;20m2023-06-28 19:26:34,333 [olivia_finder(CRITICAL)] -> 446635553.py:6[0m
Critical message
Module utilities.exceptions
Includes a series of exceptions to be used by the library and provide a more generic context in the case of being rised
from olivia_finder.utilities.exception import OliviaFinderException
OliviaFinderException('Test exception')
OliviaFinderException: Test exception
Module utilities.singleton_decorator
This module includes a decorator-based implementation of the Singleton design pattern
from olivia_finder.utilities.singleton_decorator import singleton
# Dummy class
@singleton
class Dummy:
def __init__(self, name):
self.name = name
print(Dummy('test').name)
print(Dummy('test2').name)
test
test
Module utilities.utilities
A module containing common source code to be reused
1''' 2# Subpackage utilities 3 4The utility package is designed to contain source code that is used in the library on a recurring basis, and whose functionality is to provide a certain utility to the library. 5 6## Module structure 7 8**Package structure** 9 10```bash 11../../olivia_finder/olivia_finder/utilities 12├── config.py 13├── exception.py 14├── __init__.py 15├── logger.py 16├── singleton_decorator.py 17└── utilities.py 18``` 19 20 21## Module `utilities.config` 22 23 24Provides the configuration class, which is used to obtain the configuration variables defined in the .ini configuration file 25 26.. warning:: 27 28 It is a Singleton instance so only one instance is accessible from any part of the code through the constructor 29 30 31 32 33 34```python 35from olivia_finder.utilities.config import Configuration 36``` 37 38You need to provide a configuration file 39 40The configuration file is located in the root of the olivia_finder package 41 42In this execution we are using a personalized configuration file for demonstration 43 44 45```python 46!cat ./config.ini 47``` 48 49 [olivia_finder_logger] 50 ; Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL 51 name = olivia_finder 52 level = DEBUG 53 status = ENABLED 54 file_handler = ENABLED 55 console_handler = ENABLED 56 57 ; Myrequests configuration 58 [logger_myrequests] 59 name = olivia_finder.myrequests 60 level = DEBUG 61 status = DISABLED 62 file_handler = DISABLED 63 console_handler = DISABLED 64 65 ; Package Manager configuration 66 [logger_packagemanager] 67 name = olivia_finder.packagemanager 68 level = DEBUG 69 status = DISABLED 70 file_handler = DISABLED 71 console_handler = DISABLED 72 73 ; CSV Datasource configuration 74 [logger_datasource] 75 name = olivia_finder.datasource 76 level = DEBUG 77 status = DISABLED 78 file_handler = DISABLED 79 console_handler = DISABLED 80 81 ; Output directory for the results 82 [folders] 83 logger = logs/ 84 working_dir = results/working/ 85 86 87 ; Libraries.io configuration 88 [librariesio] 89 api_key = 558f419425861e607e78cd4e3a0b129b 90 91Acess the keys using `get_key` method 92 93 94```python 95Configuration().get_key('olivia_finder_logger', 'status') 96`` 97 'ENABLED' 98 99 100## Module `utilities.logger` 101 102 103 104```python 105from olivia_finder.utilities.logger import MyLogger 106``` 107 108The class MyLogger implements an utility loging tools to register the actions of execution. It is based on the default Python logging module 109 110 111```python 112 113logger = MyLogger.get_logger("olivia_finder_logger") 114 115logger.debug('Debug message') 116logger.info('Info message') 117logger.warning('Warning message') 118logger.error('Error message') 119logger.critical('Critical message') 120 121# Acess the logger using the default logging class 122import logging 123logging.getLogger(logger.name).debug('Debug message') 124``` 125 126 [32;20m2023-06-28 19:26:34,316 [olivia_finder(DEBUG)] -> 4423775.py:3[0m 127 Debug message 128 [34;20m2023-06-28 19:26:34,318 [olivia_finder(INFO)] -> 4423775.py:4[0m 129 Info message 130 [33;20m2023-06-28 19:26:34,318 [olivia_finder(WARNING)] -> 4423775.py:5[0m 131 Warning message 132 [38;5;208;20m2023-06-28 19:26:34,319 [olivia_finder(ERROR)] -> 4423775.py:6[0m 133 Error message 134 [31;20m2023-06-28 19:26:34,319 [olivia_finder(CRITICAL)] -> 4423775.py:7[0m 135 Critical message 136 [32;20m2023-06-28 19:26:34,320 [olivia_finder(DEBUG)] -> 4423775.py:10[0m 137 Debug message 138 [32;20m2023-06-28 19:26:34,320 [olivia_finder(DEBUG)] -> 4423775.py:14[0m 139 Debug message 140 141 142Default logger is root 143 144 145```python 146# The default logger is root and it is not configured 147MyLogger.get_logger().debug('Debug message') 148MyLogger.get_logger().info('Info message') 149MyLogger.get_logger().warning('Warning message') 150MyLogger.get_logger().error('Error message') 151MyLogger.get_logger().critical('Critical message') 152``` 153 Warning message 154 Error message 155 Critical message 156 157Change log level 158 159 160```python 161MyLogger.configure_level("olivia_finder_logger", 'console', 'warning') 162logger = MyLogger.get_logger("olivia_finder_logger") 163logger.debug('Debug message') 164logger.info('Info message') 165logger.warning('Warning message') 166logger.error('Error message') 167logger.critical('Critical message') 168``` 169 170 [33;20m2023-06-28 19:26:34,331 [olivia_finder(WARNING)] -> 446635553.py:4[0m 171 Warning message 172 [38;5;208;20m2023-06-28 19:26:34,332 [olivia_finder(ERROR)] -> 446635553.py:5[0m 173 Error message 174 [31;20m2023-06-28 19:26:34,333 [olivia_finder(CRITICAL)] -> 446635553.py:6[0m 175 Critical message 176 177## Module `utilities.exceptions` 178 179Includes a series of exceptions to be used by the library and provide a more generic context in the case of being rised 180 181 182```python 183from olivia_finder.utilities.exception import OliviaFinderException 184``` 185 186 187```python 188OliviaFinderException('Test exception') 189``` 190 OliviaFinderException: Test exception 191 192 193 194## Module `utilities.singleton_decorator` 195 196This module includes a decorator-based implementation of the Singleton design pattern 197 198 199```python 200from olivia_finder.utilities.singleton_decorator import singleton 201``` 202 203 204```python 205# Dummy class 206@singleton 207class Dummy: 208 def __init__(self, name): 209 self.name = name 210``` 211 212 213```python 214print(Dummy('test').name) 215print(Dummy('test2').name) 216``` 217 218 test 219 test 220 221 222## Module `utilities.utilities` 223 224A module containing common source code to be reused 225 226'''