Python
/
Scheduler
- 1 Language 9
-
Hello World S
-
Variables S
-
Functions S
-
Conditional A S
-
Operators S
-
While S
-
Turtle S
-
Script Mode S
-
Debugging S
- 2 Strings 7
-
Slice S
-
Raw Strings S
-
Regex A S
-
Validation S
-
Config S
-
Security S
-
Encrypt A S
- 3 Collections 6
-
Lists S
-
Dictionaries S
-
Efficiency S
-
Tree S
-
Tuples S
-
References S
- 4 Functions 5
-
Recursion S
-
Factorial S
-
Modulus S
-
Reassignment S
-
Approximate S
- 5 Storage 8
-
Files S
-
Databases S
-
Pipes S
-
With open S
-
Shelve A S
-
Zip S
-
Csv S
-
Json S
- 6 Class 4
-
Definition S
-
Attributes S
-
Functional S
-
Methods S
- 7 Goodies 5
-
Conditional Expression S
-
List Comprehension A S
-
Generator S
-
Named Tuple S
-
Modules S
- 8 Applications 5
-
Pythagora A S
-
Palindrome A S
-
Binary Search A S
-
Conway Game A S
-
Coin Flip A S
- 9 Scheduler 4
-
Time S
-
Multithreading A S
-
Subprocess S
-
Logging S
- 10 Packages 6
-
Clipboard A S
-
Ocr A S
-
Socket S
-
Image S
-
Virtualenv S
-
Jupyter S
S
R
Q
Python Scheduler Logging
import logging loggin.debug('my message')
Logging
p255 Logging is a great way to understand what's happening in your program.
"""Logging
Set basic configuration to add logging to your program.
Factorial: n! = n(n-1)!
"""
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def factorial(n):
if (n == 0): return 1
logging.debug('factorial(%s)' % n)
n = n * factorial(n-1)
logging.debug('n is ' + str(n))
return n
factorial(4)
"""
2021-12-10 16:04:03,046 - DEBUG - factorial(4)
2021-12-10 16:04:03,046 - DEBUG - factorial(3)
2021-12-10 16:04:03,047 - DEBUG - factorial(2)
2021-12-10 16:04:03,047 - DEBUG - factorial(1)
2021-12-10 16:04:03,047 - DEBUG - n is 1
2021-12-10 16:04:03,047 - DEBUG - n is 2
2021-12-10 16:04:03,047 - DEBUG - n is 6
2021-12-10 16:04:03,047 - DEBUG - n is 24
"""
Debug
p259 It's bettter not to debug a code with print() function.
"""Debug
If you use print() calls to debug your code,
you'll spend a lot of time to remove thems, once you are done.
With logging module you can easily switch between ...
showing or hidding messages.
"""
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def factorial(n):
if (n < 0):
logging.critical('n is not positive!')
return None
if (n == 0): return 1
n = n * factorial(n-1)
logging.debug('n is ' + str(n))
return n
"""Logging messages enabled:
"""
assert factorial(-1) == None
assert factorial(4) == 24
# 2021-12-10 16:31:44,977 - CRITICAL - n is not positive!
# 2021-12-10 16:18:59,379 - DEBUG - n is 1
# 2021-12-10 16:18:59,379 - DEBUG - n is 2
# 2021-12-10 16:18:59,379 - DEBUG - n is 6
# 2021-12-10 16:18:59,379 - DEBUG - n is 24
"""Logging messages disabled:
Only level > ERROR messages are displayed
"""
logging.disable(logging.ERROR)
assert factorial(-1) == None
assert factorial(0) == 1
assert factorial(3) == 6
assert factorial(4) == 24
# 2021-12-10 16:34:58,048 - CRITICAL - n is not positive!
File
Insteed of displaying messages to the screen, you can write them to a file.
"""Logging messages to file:
Screen messages are helpful, but they can make the output ...
hard to read.
Writting logging messages to a file keeps your screen clean.
"""
import logging, pathlib
DIR = pathlib.Path(__file__).resolve().parent
logging.basicConfig(
level=logging.DEBUG,
filename=DIR/'logs/file1.log',
format='%(asctime)s - %(levelname)s - %(message)s'
)
def factorial(n):
if (n < 0):
logging.critical(f'Number {n} is not positive!')
return None
if (n == 0): return 1
n = n * factorial(n-1)
logging.debug('n is ' + str(n))
return n
assert factorial(-1) == None
assert factorial(4) == 24
assert factorial(-11) == None
""" logs/file1.log
--------------------------------------------------------------
2021-12-10 17:09:40,229 - CRITICAL - Number -1 is not positive!
2021-12-10 17:09:40,229 - DEBUG - n is 1
2021-12-10 17:09:40,229 - DEBUG - n is 2
2021-12-10 17:09:40,229 - DEBUG - n is 6
2021-12-10 17:09:40,229 - DEBUG - n is 24
2021-12-10 17:09:40,229 - CRITICAL - Number -11 is not positive!
2021-12-10 17:09:59,231 - CRITICAL - Number -1 is not positive!
2021-12-10 17:09:59,231 - DEBUG - n is 1
2021-12-10 17:09:59,231 - DEBUG - n is 2
2021-12-10 17:09:59,231 - DEBUG - n is 6
2021-12-10 17:09:59,232 - DEBUG - n is 24
2021-12-10 17:09:59,232 - CRITICAL - Number -11 is not positive!
"""
➥ Questions