The easy way to inspect variables in Python
Project description
easierlog: the easy way to inspect variables in Python
1. Basic usage
Roughly every software developer needs to check how variables behave in runtime. In Python, this can be done by using print()
function, that prints out the value of the passed variable. Sometimes, instead, the variable value is not enough. This is specially true when there are many variables to be inspected, or even when the same variable needs to be inspected in many parts of the code.
To inspect both variable name and value, print()
is not so straightforward:
print('some_variable =', some_variable)
It became a little bit easier since Python 3.8:
print(f'{some_variable=}')
But not so easy when comparing to easierlog:
log(some_variable)
This can better understood in the script:
1 # script1.py
2
3 def function1():
4 a = 4
5 print(a)
6
7
8 def function2():
9 a = 3
10 b = 'Hello World'
11 print(a)
12 print(b)
13
14
15 function1()
16 function2()
17
Running the script above, it will result:
4
3
Hello World
By using easierlog, the script would become:
1 # script2.py
2
3 from easierlog import log
4
5
6 def function1():
7 a = 4
8 log(a)
9
10
11 def function2():
12 a = 3
13 b = 'Hello World'
14 log(a)
15 log(b)
16
17
18 function1()
19 function2()
20
Running the script above, it will result:
[script2.py (line 08) in function1] (int) a = 4
[script2.py (line 14) in function2] (int) a = 3
[script2.py (line 15) in function2] (str) b = 'Hello World'
As shown above, log()
provides information about where it was called:
- File name
- Line number
- Function name
And about the variable passed as argument:
- Variable type
- Variable name
- Variable value
2. Multiple variables
It is also possible to pass multiple variables at once:
1 # script3.py
2
3 from easierlog import log
4
5
6 def function():
7 x = 2.3
8 y = 1.5
9 z = 0.1
10 log(x, y, z)
11
12
13 function()
14
It will result:
[script3.py (line 10) in function] (float) x = 2.3
[script3.py (line 10) in function] (float) y = 1.5
[script3.py (line 10) in function] (float) z = 0.1
3. All variables
Calling log()
without any arguments it will log all declared variables in the function:
1 # script4.py
2
3 from easierlog import log
4
5
6 def function():
7 x = 2.3
8 y = 1.5
9 z = 0.1
10 log()
11
12
13 function()
14
It will result:
[script4.py (line 10) in function] (float) x = 2.3
[script4.py (line 10) in function] (float) y = 1.5
[script4.py (line 10) in function] (float) z = 0.1
Installation
To install the latest release, type:
pip install easierlog
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for easierlog-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36c82a46332a643c90f88b03e56c8eb1317a978e6caddc7c230fbb11d2238fb8 |
|
MD5 | 0bdd2f4c876a493fd7e2d65c897ff375 |
|
BLAKE2b-256 | b5cd2ef7c960ab19d11474535ca18d52cbbca0c7177e84c6d46508bc30c9fdf5 |