Python Programming - Intermediate Level

How to Neatly Display Parallel Output in Terminal?

When using modules like multiprocessing or threading for parallel processing, have you ever wondered how to print string messages from each process or thread effectively?

Most people would simply use print() in the terminal. However, when the output is too long or too much, it might not be particularly useful. In such scenarios, it’s often best to write the outputs to a file so you can trace back any issues later.

Still, most of the time, we want to know the latest status of each executing unit, like which step it’s currently at. If these outputs continue to pile up in the terminal, it inevitably makes the terminal quite messy.

In this article, I will introduce a method to refresh and overlay outputs of parallel processing, so you can see the latest status of each unit while keeping the terminal clean and elegant!

Posted on  Mar 13, 2024  in  Python Programming - Intermediate Level  by  Amo Chen  ‐ 3 min read

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

Python Tips - Saving Time and Effort with CSVDictWriter and defaultdict

Python’s CSVDictWriter is a handy module that allows you to write dictionary data directly into a CSV file. Here’s how you can use it:

import csv

with open('my.csv', 'w') as csvf:
    writer = csv.DictWriter(csvf, fieldnames=[
        'field_1',
        'field_2',
        'field_3'
    ])

    writer.writeheader()
    writer.writerow({'field_1': '', 'field_2': 'b', 'field_3': ''})
    writer.writerow({'field_1': '', 'field_2': 'b', 'field_3': ''})

While CSVDictWriter is convenient, it stops if any fields in a row are missing. This can be a hassle if you have many fields, and not all of them have values. Do you really need to manually fill in every single field?

Actually, you can solve this problem by using defaultdict.

Posted on  Sep 2, 2016  in  Python Programming - Intermediate Level  by  Amo Chen  ‐ 1 min read