Python
-
Learn Type Variables and Generic Types in Python with Simple Examples
In this post, we will introduce the basics of type variables and generic functions and classes with simple code examples. Using type variables and generic types can make our code more robust and easier to read. Even if we may not have many chances to use them directly in our own code, it’s still very… Continue reading
-
Some Special Use Cases of Regular Expressions in Python
In this post, we will showcase a selection of special applications for regular expressions in Python, aimed at resolving real-world challenges. As we will see in our examples, we can use word boundaries and lookahead assumptions to achieve more accurate matching. A small trick will also be introduced for how to create consistent match strings… Continue reading
-
Use pipdeptree to Check Python Package Dependencies and Create a Clean requirements.txt
Learn a handy tool to check package dependencies in Python Writing a requirements.txt file from scratch for your application can be cumbersome because you need to sort out what packages need to be installed and what are their dependencies. Worse still, it’s tedious to resolve conflicting dependencies, especially on legacy systems when an older version of… Continue reading
-
Compare Dictionary, Named Tuple, Data Class, and Pydantic Model in Python
Python is an object-oriented programming language and there are various ways to represent data as objects. Simple objects can be represented by dictionaries with key-value pairs. And more complex ones can be represented by some special data classes including namedtuple, data class, and Pydantic models. In this post, we will introduce these data types with… Continue reading
-
How to Debug Your Microservice Python Code Running Remotely in Docker
Nowadays, it’s a good practice to Dockerize our Python API code running as microservices, which can make the code easier to develop, deploy, and maintain. During the development or maintenance stages, we often need to debug our code that doesn’t run properly. We cannot use the traditional PDB library to debug API code. Instead, some… Continue reading
-
How to Add Command-line Arguments for a Python Program with argparse
When writing executable Python programs, we should always add some command-line arguments to make them easier to use. Most users will run the program with the -h or–help option to see the usage instructions of the program. A native way to parse command-line arguments is by using sys.argv. However, it’s not user-friendly and we need… Continue reading
-
How to Handle Errors and Exceptions Properly in Python
Error or exception handling in Python may seem pretty simple to us, as we just need to use try and except to catch the exception and deal with it. Yes, it is simple to use. However, some bad habits of dealing with exceptions can make our code buggy and difficult to maintain. Many developers may… Continue reading
-
How to Send Emails in Python with GCP Cloud Function as a Microservice
Learn different ways to send emails in Python and trigger GCP Cloud Functions It’s common to send emails programmatically in an application. For example, emails can be sent to developers when critical API errors occur. Besides, emails need to be sent out to users automatically for registration, password reset, or service confirmation. In this post,… Continue reading
-
How to Download Images From URLs, Convert the Type, and Save Them in the Cloud With Python
It can be unstable to serve third-party images because there can be latencies and they can be changed or removed without prior notice. Therefore, for the robustness of your service, sometimes you need to download the images and serve a stable version of them. In this post, we will introduce how to download images from… Continue reading
-
How to Add Google Logger to Scrapy in Python
It’s important to keep a record of the scraping projects with logging so we can know which spiders are working properly and which ones are not. It is cumbersome to write the logs to some files and try to analyze the plain texts later. A better way is to send the logs to the cloud… Continue reading