Understanding and Practical Experience with Python's Logging Library

This article starts with the basic concepts of the Python logging library to understand its execution flow and some details that might be overlooked. Log Levels The logging library has 5 preset error levels, plus a NOTSET level, which is the default value for a logger. CRITICAL = 50 ERROR = 40 WARNING = 30 INFO = 20 DEBUG = 10 NOTSET = 0 The logging library also supports custom error levels. As seen in the source code above, there are 10 numeric positions reserved between different error levels, allowing us to add more detailed error levels on top of the preset ones. ...

January 25, 2019 · 7 min · Zhiya

Translation | Faster Python (Part 2)

Faster Python (Python Faster Way) uses code examples to demonstrate how writing Python code can lead to higher performance. This article explains the code, choosing the most suitable approach from the perspectives of performance and readability. Example 11: String Concatenation Worst/Best Time Ratio: 1.15 Recommendation: Use join when concatenating multiple (more than 3) strings at once; otherwise, use the plus sign or f-string. Explanation: This is another string concatenation issue, but the example is not well chosen. join is suitable for scenarios where multiple strings are concatenated at once, which is much faster than using the plus sign to concatenate multiple strings (the plus sign is equivalent to concatenating one by one). Example 12: Number Formatting ...

October 25, 2018 · 4 min · Zhiya

Translation | Faster Python (Part 1)

Faster Python (Python Faster Way) uses code examples to demonstrate how writing Python code can achieve higher performance. This article explains the code, selecting the most suitable approach from the perspectives of performance and readability. Example 1: String Formatting Worst/Best Time Ratio: 1.95 Recommendation: Use f-string for Python 3.7 or above, and the format method for other versions. Explanation: String formatting is a common scenario in code. Although using the + operator is optimal for concatenating a small number of strings, it results in the least readable code. If using Python 3.7 or above, f-string can solve this issue, offering better performance than both the format method and the % operator, while also being more readable than the + operator. Example 2: Dictionary Initialization ...

October 8, 2018 · 3 min · Zhiya

Writing Algorithms with Python | Implementing Reservoir Sampling for Random Sampling

Given a set of numbers where the total count is unknown, describe an algorithm that can randomly select k numbers from this set such that each number has an equal probability of being selected. If the set contains n numbers, then the probability of selecting each number is k/n. However, the challenge here is that the total count, n, is unknown. So how can we calculate the probability of selecting each number? ...

August 5, 2018 · 3 min · Zhiya

The Evolution of Python Function Parameters You Didn't Know About

The mechanism for handling function parameters is a crucial aspect of Python. As Python has evolved, the flexibility and richness of parameter handling have continuously increased, allowing us to write simplified code and handle complex calls. Keyword Arguments Specify the parameter’s name when calling, matching the parameter name in the function declaration. Keyword arguments are the most basic and common in Python functions. Let’s write a bookkeeping function with parameters for the date and amount to be recorded. ...

July 10, 2018 · 5 min · Zhiya