Data Science: From School to Work, Part III

Introduction
The Writing Code is about resolving problems, but not all problems are visible. In real world, your software will experience unexpected situations: lost files, invalid user input, network exit, or hardware failure. This is why handling mistakes is not just a good thing; It is a critical part of building strong and reliable productive applications.
Think about e-commerce website. The customer places order, but during the output process, data data issues occur. Without proper treatment, the matter can cause the app to disappear, leaving the customers that are frustrated and implemented. The worst, it can create unrelated data, which leads to very big problems. Therefore, handling error is a basic skill in any Python developer you want to write a manufacturing code.
However, good mistake management is also in line with a good cutting system. It is rare to reach the consolent where the code works in production. So there is no chance to print your human observations. Ensure that you can view your app and investigate any events, you need to set up a logio program. This is where the Loguru's package begins when the Loguru is going to present in this text.
I – how do I treat Python errors?
In this section I introduced the best of the best mistakes of Python error, from attempting-without blocks and the use of raise
in finally
statement. These concepts will help you text, last additional correct code in the production area.
This page Finds blocks
Block-without block is a major tool to manage the Epython errors. Allows you to hold potential errors at the time of the killing of the code and protect the program from getting hit.
def divide(a, b):
try:
return a / b
except ZeroDivisionError:
print(f"Only Chuck Norris can divide by 0!")
In this unwittingly, a block-without block allows the error caused by division by 0 to be separated. Block blocking code is made, and in case of error, block checks unless ZeroDivisionError
Then print a message. But this kind of error is only held. For example, if b The thread, it happens. To avoid this, you can add a TypeError
. Therefore, it is important to test all possible mistakes.
The work becomes:
def divide(a, b):
try:
return a / b
except ZeroDivisionError:
print(f"Only Chuck Norris can divide by 0!")
except TypeError:
print("Do not compare apples and orange!")
Lift different
You can use a lift statement to lift the opposite. This is useful if you want to report an error specified by a user or place a specific limit in your code.
def divide(a, b):
if b == 0:
raise ValueError("Only Chuck Norris can divide by 0!")
return a / b
try:
result = divide(10, 0)
except ValueError as e:
print(f"Error: {e}")
except TypeError:
print("Do not compare apples and orange!")
In this example, a ValueError
Release is created if the separation is zero. In this way, you can clearly control the mistake conditions. In print work, the message will be “Error: Only Chuck Norris can separate 0!“.
Some of the most common ideas
VALUYROR: The value of the value is correct but its amount is invalid.
try:
number = math.sqrt(-10)
except ValueError:
print("It's too complex to be real!")
Orsor: Trying to access the non-dictionary key.
data = {"name": "Alice"}
try:
age = data["age"]
except KeyError:
print("Never ask a lady her age!")
Indication: Attempting to reach the unavailable index.
items = [1, 2, 3]
try:
print(items[3])
except IndexError:
print("You forget that indexing starts at 0, don't you?")
Typerror: To do the work in non-relevant types.
try:
result = "text" + 5
except TypeError:
print("Do not compare apples and orange!")
FileTontourqupererererer: To try to open an unable file.
try:
with open("notexisting_file.txt", "r") as file:
content = file.read()
except FileNotFoundError:
print("Are you sure of your path?")
Custom error: You can cause different specified or explain your exception classes:
class CustomError(Exception):
pass
try:
raise CustomError("This is a custom error")
except CustomError as e:
print(f"Catched error: {e}")
Clean with finally narration
This page finally
The block is made from all cases, regardless of the error has occurred or not. It is usually used to perform cleaning actions, such as closing the connection to details or releasing resources.
import sqlite3
try:
conn = sqlite3.connect("users_db.db") # Connect to a database
cursor = conn.cursor()
cursor.execute("SELECT * FROM users") # Execute a query
results = cursor.fetchall() # Get result of the query
print(results)
except sqlite3.DatabaseError as e:
print("Database error:", e)
finally:
print("Closing the database connection.")
if 'conn' in locals():
conn.close() # Ensures the connection is closed
The best habits to treat the error
- Hold out of something: Avoid using the generic without a block without a different specifying, because it can prevent unexpected errors. Prefer to specify the opposite:
# Bad practice
try:
result = 10 / 0
except Exception as e:
print(f"Error: {e}")
# Good practice
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
- Give clear messages: Put clear and descriptive messages when lifting or fabrics outside.
- Avoid Silent Failure: If you hold a different, make sure to be entered or re-elimination so it cannot be built.
import logging
logging.basicConfig(level=logging.ERROR)
try:
result = 10 / 0
except ZeroDivisionError:
logging.error("Division by zero detected.")
- Use
else
includingfinally
blocks: Theelse
Block runs only if no one is released intry
block.
try:
result = 10 / 2
except ZeroDivisionError:
logging.error("Division by zero detected.")
else:
logging.info(f"Success: {result}")
finally:
logging.info("End of processing.")
II – How to Manage Psyth Coops?
Beautiful Error – Backing One, but if no one knows what's wrong, the whole point is lost. As described in the introduction, monitoring is rarely consulting or visible when the system works in production. No one will see printing. Therefore, handling a good mistake must be accompanied by a good logging program.
What are logs?
Logs are recorded in the message records to trace events taking place during its murder. These messages may contain information about errors, alerts, effective actions, important processes or other relevant events. Lugs are important to Debugging, working in tracking and looking for app life. They allow developers to understand what is going on in the system without disturbing its production, making it easier to solve problems and continue to improve the software.
Loguuru package
Python has already had a native logging package: login. But we choose a Loguuru package, which is very simple to use and easy to edit. In fact, complete formatting of the output is already defined.
from loguru import logger
logger.debug("A pretty debug message!")
All important things are entered directly to the message:
- Time Stamp
- Login level, showing the seriousness of the message.
- File location, module and line number. In this example, the location of the file is the__ of__ is because it was directly executed from the command line. Module is
Because the log is not available in class or work. - Message.
Different levels of logging
There are a few levels of log to pay attention to the value of the message shown (more complex printed). Each level has a related number and number:
- It marked (5): used to record detailed information in a diagnostic purposes.
- To adjust the error (10): Developing messages recording for correcting objectives.
- Knowledge (20): used to record information details that explain the performance of the standard system.
- Success (25): The same as information, but used to indicate the success of surgery.
- Discipline (30): It is used to demonstrate an unusual event that you may need to continue the investigation.
- Error (40): It is used to record the error conditions affected by something.
- Critical (50): used to record the event conditions that prevent the main function to work.
Package naturally manages different formatting in terms of use used
from loguru import logger
logger.trace("A trace message.")
logger.debug("A debug message.")
logger.info("An information message.")
logger.success("A success message.")
logger.warning("A warning message.")
logger.error("An error message.")
logger.critical("A critical message.")

Trace message was not displayed because the default low level used by Loguru. So ignore all messages at lower levels.
It is possible to describe the new levels of the log in a level and used in the log
logger.level("FATAL", no=60, color="", icon="!!!")
logger.log("FATAL", "A FATAL event has just occurred.")
- Name: The name of the log level.
- No: Corresponding compatible amount (must be number).
- Color: The color mark.
- Thumbnail: The level icon.
Logger Configuration
You may have re-observed the logger with a new configuration with the old removing with remove
order and produce a new logger with new configuration with add
work. This work takes the following issues:
- go down [mandatory]: Specifies the target set for each data set made by logger. Automatically, this value is set to
sys.stderr
(which corresponds to normal error issuance). We can also keep everything out of the “.log” file (except if you have a log collector). - appropriate: Suspend the lower level of login.
- format: It helps to explain the custom format for your own logs. To save logs of logs in the term, this should be described (see the example below).
- squeeze: used to determine whether the log should be recorded or not.
- color: It takes a boolean value and determines that the terminal color should work or not.
- ceive: It causes a record to be shown in JSON format if set to
True
. - backup: He decides that a different trail should go over the point where an error is made so easy to solve the problem.
- diagnose: It determines whether the variable values should be shown in a different tracking. This option must be set to
False
In the production areas so that there is no sensitive information. - Enquee: If this method is activated, log data records are stored in line to avoid conflicts if several processes connect to the same target.
- catch: In the event of an unexpected error when connecting to a server described in the pond, you can get this option to
True
. The error will be displayed with a general error.
import sys
from loguru import logger
logger_format = (
"{time:YYYY-MM-DD HH:mm:ss.SSS} | "
"{level: <8} | "
"{name}:{function}:{line}"
)
logger.remove()
logger.add(sys.stderr, format=logger_format)
Booklet:
The colors disappear in the file. This is because there are special characters (called kind Codes) show colors in the forum, but this format is not in files.
Adding the context to songs
For complex applications, it can be helpful to add more information to logs to enable sorting and facilitating solving.
For example, if the user changes the database, it can be helpful to have a user ID above the exchange experience.
Before you start recording the context data, you need to make sure that {extra}
Directlove included in your custom format. This differs from the Python dictionary containing each log data data (if applicable).
Here is an example of making a customization when it is more user_id
it is added. In this way, the colors.
import sys
from loguru import logger
logger_format = (
"{time:YYYY-MM-DD HH:mm:ss.SSS} | "
"{level: <8} | "
"{name} :{function} :{line} | "
"User ID: {extra[user_id]} - {message} "
)
logger.configure(extra={"user_id": ""}) # Default value
logger.remove()
logger.add(sys.stderr, format=logger_format)
Now it is possible to use Bind method to create a child's logger receiving all the information from the Logger parent.
childLogger = logger.bind(user_id="001")
childLogger.info("Here a message from the child logger")
logger.info("Here a message from the parent logger")

One way to do this is to use the content method in block.
with logger.contextualize(user_id="001"):
logger.info("Here a message from the logger with user_id 001")
logger.info("Here a message from the logger without user_id")

Instead of block, you can use a lonter. The preceding code then becomes
@logger.contextualize(user_id="001")
def child_logger():
logger.info("Here a message from the logger with user_id 001")
child_logger()
logger.info("Here a message from the logger without user_id")
A way to hold
Errors can automatically log in when it happens to use a host.
def test(x):
50/x
with logger.catch():
test(0)

But it is easy to use this method as a decorator. This results in the following code
@logger.catch()
def test(x):
50/x
test(0)
Log file
Productive application is intended to run continuously and not interrupted. In some cases, it is important to predict the file functioning, if not it will be able to contact the prison pages in case of error.
Here are different situations where the file can be changed:
- revolution: Specifies the situation when the current log file is off and created by a new file. This situation can be an int, datettome or str. Stra is recommended as easy to read.
- captivity: Specifies how much the log file should last before removing from the file system.
- repression: Log file is altered to the specified press format if this method is activated.
- detain: If this option is set to TRUE, the creation of a new log file is delayed until the first log message is pressed.
- practice, contrary, the following- : Parameters forwarded by the Python open work and determine how Python opens log files.
Booklet:
Usually, in the position of production app, the Log collector will be provided for the specific application results. So it is not necessary to create a log file.
Store
Error hosting in Python is an important step in writing correct and reliable code. By combining blocks without blocks, lift statement, and a block eventually, you can make mistakes in speculation while storing the readable and storage code.
In addition, a good login program promotes the power of monitoring and remoding your application. Loguuru provides simple and variable package to log messages and therefore can be easily integrated into your code code.
Summarizing, integrating effective error management by the full note system can enhance reliability, maintenance and python.
Progress
1 – Error hosting in Python: Legal Python Outside Books
2 – Loguuri Documents:
3 – Guide about Guru: