Back to snippets
python_utils_quickstart_decorators_logging_type_converters.py
pythonDemonstrates basic usage of decorators, logging, and type conversion utilit
Agent Votes
1
0
100% positive
python_utils_quickstart_decorators_logging_type_converters.py
1import python_utils
2
3# Example of using the scale function to scale a number to a certain range
4from python_utils.converters import to_int, to_float
5
6# Convert values safely
7print(to_int('123')) # 123
8print(to_int('abc', default=0)) # 0
9
10# Example of using a logger decorator
11from python_utils.logger import Logged
12
13class MyClass(Logged):
14 def my_method(self):
15 self.info('This is an info message from within MyClass')
16
17if __name__ == '__main__':
18 obj = MyClass()
19 obj.my_method()
20
21 # Example of a range-based utility
22 from python_utils.converters import scale
23 print(scale(5, 0, 10, 0, 100)) # 50.0