Blog

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

How to Use the Built-in VNC Client on macOS

Sometimes for work, you might need to connect to a remote server’s desktop using VNC (Virtual Network Computing) to view its display. Often, this means installing tools like TeamViewer, TightVNC, or VNC Viewer.

However, macOS actually has a built-in VNC client that you can use. If you require a simple VNC client for work, you can use the built-in VNC client on macOS.

Posted on  Dec 25, 2022  in  macOS  by  Amo Chen  ‐ 1 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

Practical Python Performance Profiling - From cProfile to py-spy

It’s a well-known fact that Python applications run slower compared to those written in languages like Go or C. However, Python’s ease of use significantly speeds up development, and its philosophy of batteries included along with its rich ecosystem can greatly reduce the need to reinvent the wheel, shortening the path from concept to product.

The issue of slower execution can be mitigated through various techniques such as using distributed systems, multithreading, multiprocessing, optimizing algorithms, or extending Python with more efficient languages (e.g., using the ctypes module). Although it might not achieve the same high efficiency as Go or C, it can certainly reach an acceptable level of performance.

Before tackling real efficiency issues, it’s often the case that inefficiencies are due to poorly written Python applications rather than limitations of Python itself.

Analyzing Python code to find hidden performance issues is crucial. This article starts by introducing the cProfile module and demonstrates with examples how to identify bottlenecks in Python programs. We then use the py-spy tool to precisely locate issues, improving the efficiency of finding problems.

Posted on  Dec 9, 2021  in  Python Programming - Advanced Level  by  Amo Chen  ‐ 6 min read

Practical Use of Fil to Improve Python Memory Usage

In the post Identifying Peak Memory Usage with Python’s Resource Module, we showed how to use the Python resource module to identify peak memory usage. However, this module doesn’t provide detailed memory stats nor does it show which parts of your Python code consume the most memory. Therefore, we need a tool to profile memory usage in detail to help pinpoint problems.

In this post, we’ll introduce how to use Fil to profile Python programs’ memory usage. We’ll go through a simple example to identify and optimize the parts of the program that consume the most memory.

Posted on  Nov 29, 2021  in  Python Programming - Advanced Level  by  Amo Chen  ‐ 4 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