Skip to main content

Python

2024


__import__ in Python

·2 mins
It’s known that Python’s import statement is implemented by __import__ function. In general, if we want to import a module dynamically, we can use import_module function, which is a wrapper around __import__. The most important difference between these two functions is that import_module() returns the specified package or module (e.

2023


Python 3.11 changes

·4 mins
In [Packaging] Support Python 3.11 by bebound · Pull Request #26923 · Azure/azure-cli (github.com) , I bumped azure-cli to use Python 3.11. We’ve bump the dependency in other PRs, I thought it should be a small PR, but in the end, a lot of changes are made.

2022


Memory Leak in Python multiprocessing.Pool

·4 mins
There is a historical memory leak problem in our Django app and I fixed it recently. As time goes by, the memory usage of app keeps growing and so does the CPU usage. After some research, I figure out the cause.

2021


How to disable auto strip in Charfield in Django

·2 mins
In Django, when edit field in admin page or post data to forms, the leading and tailing whitespace in CharField and TextField are removed. The reason is strip=True parameter in forms.CharField, which is added in Djagno 1.9. You can see the discussion in django tiket #4960 and here is source code.

Using JSONField before Django 3.1

·2 mins
In Django 3.1, Django support save python data into database as JSON encoded data and it is also possible to make query based on field value in JSONField. The detailed usage can be found here. If you are using older version and want to try this feature.

2020


Using cibuildwheel to Create Python Wheels

·2 mins
Have you ever tried to install MySQL-python? It contains the C code and need to compile the code while install the package. You have to follow the steps in this articles: Install MySQL and MySQLClient(Python) in MacOS. Things get worse if you are using Windows.

Program Crash Caused by CPU Instruction

·3 mins
It’s inevitable to dealing with bugs in coding career. The main part of coding are implementing new features, fixing bugs and improving performance. For me, there are two kinds of bugs that is difficult to tackle: those are hard to reproduce, and those occur in code not wrote by you.

Import custom package or module in PySpark

·1 min
First zip all of the dependencies into zip file like this. Then you can use one of the following methods to import it. |-- kk.zip | |-- kk.py Using –py-files in spark-submit #When submit spark job, add --py-files=kk.zip parameter. kk.zip will be distributed with the main scrip file, and kk.

C3 Linearization and Python MRO(Method Resolution Order)

·3 mins
Python supports multiple inheritance, its class can be derived from more than one base classes. If the specified attribute or methods was not found in current class, how to decide the search sequence from superclasses? In simple scenario, we know left-to right, bottom to up.

2019


Torchtext snippets

·1 min
Load separate files #data.Field parameters is here. When calling build_vocab, torchtext will add <unk> in vocabulary list. Set unk_token=None if you want to remove it. If sequential=True (default), it will add <pad> in vocab. <unk> and <pad> will add at the beginning of vocabulary list by default.