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

Docker multi-stage builds tutorial

The size of Docker images is also quite important in production environments.

If the Docker image is too large, not only will it occupy the transmission bandwidth, but it will also prolong the deployment time, so how to optimize the size of the Docker image is an important issue.

There are several ways to optimize the size of a Docker image, one of which is multi-stage build. However, the multi-stage builds example provided by the Docker official document does not work properly.

Last updated on  Dec 30, 2022  in  Docker  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

class Settings(object):
    DB_HOST = 'localhost'
    DB_PORT = 8888

settings = Settings()
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

Docker Environment Variable Usage Notes

Environment variables are one of the common methods used to control program behavior during development.

With Docker being increasingly used in development environments in recent years, it is essential to learn how to use environment variables in Docker.

This article introduces several methods of configuring environment variables in Docker and Docker compose.

Posted on  Jun 19, 2020  in  Docker  by  Amo Chen  ‐ 3 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
    # do production-related things

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