python

Python Module Tutorial - dataclasses

Python’s dataclasses is a new module added in Python 3.7, mainly used to define structured data in the form of classes.

The dataclasses module provides some convenient features to help automatically generate commonly used class methods such as __init__, __repr__, __eq__, etc., saving developers time in writing repetitive code.

Using dataclasses can make Python programs more concise and improve code readability.

Are you ready to show off your skills using dataclasses in your Python code?

Posted on  Feb 1, 2023  in  Python Programming - Intermediate Level  by  Amo Chen  ‐ 8 min read

Designed for Counting - Python Counter Class

The Python collections module provides several convenient classes for developers to use, among which the Counter class (a subclass of dict) can be applied in counting-related scenarios:

A Counter is a dict subclass for counting hashable objects.

This article will introduce the usage of the Counter class and compare its performance with dict and defaultdict.

Posted on  Apr 19, 2022  in  Python Programming - Beginner Level  by  Amo Chen  ‐ 5 min read

Python Module Tutorial - pathlib

Python’s os module provides many convenient functions for us to manipulate file/folder paths. After Python 3.4, a new module called pathlib was introduced, which encapsulates various file/folder related operations in classes such as Path, making file/folder operations more object-oriented.

This article will explain and demonstrate the pathlib module.

Posted on  Oct 19, 2020  in  Python Programming - Beginner Level  by  Amo Chen  ‐ 5 min read

Learn Python Multiprocessing Module Easily with Examples

Python’s built-in multiprocessing module is quite important if there are requirements for parallelism processing, in addition to the built-in threading module, another one is multiprocessing.

The advantage of using multiprocessing is that it can greatly avoid the impact of Python GIL on program performance, but the bad thing is that it consumes more memory. Even so, it is still a module that must be understood.

This article will learn how to use the multiprocessing module through several examples.

Last updated on  Feb 8, 2023  in  Python Programming - Beginner Level  by  Amo Chen  ‐ 8 min read

Easily manage settings with Pydantic

During development, configuration files are often needed to easily change the behavior of the system. For example, the switch of Debug mode, database related settings, etc. are usually placed in configuration files.

In addition to the built-in module configparser provided by Python to easily implement configuration files, it can also easily implement configuration files using classes, such as the following two files are examples of configuration

# settings.py
class Settings(object):
    DB_HOST = 'localhost'
    DB_PORT = 8888


settings = Settings()
# test.py
from settings import settings


print(settings.DB_HOST, settings.DB_PORT)

However, nowadays development projects also often use dotenv (e.g. python-dotenv) to make configuration easier.

In addition to developing the functionality of dotenv by yourself, you can actually choose to integrate class-style configuration files and dotenv easily with pydantic.

Posted on  Jul 1, 2020  in  Useful Python Modules , Python Programming - Advanced Level  by  Amo Chen  ‐ 5 min read

Python dotenv introduction and tutorial

Storing important data in environment variables is a common practice when developing, not only to avoid accidentally committing important data into the codebase, but also to use environment variables to store system or program settings. It is also often used in practice to separate development environment from production environment, for example:

if os. getenv('MODE') == 'development':
    # do development-related things
    pass
else:
    # do production-related things
    pass

But as the number of environment variables to be set increases, it may lead to a lot of environment variables to be filled every time you do development. If you have encountered such a situation, why not give python-dotenv a try!

Last updated on  Mar 13, 2023  in  Useful Python Modules  by  Amo Chen  ‐ 2 min read

Pyenv - A great tool for Python version management

Python has been evolving for nearly 10 years, so the development of Python projects also needs to consider the issue of version.

Especially now that Python 2 has retired, most Python projects now use Python 3 as the main version, but some of the old projects in companies still use Python 2 for development, so developers must switch between Python 2 and 3.

If you have trouble switching between Python versions, then pyenv will be your best friend!

Last updated on  Mar 13, 2023  in  Python Programming - Beginner Level , Useful Python Modules  by  Amo Chen  ‐ 3 min read