How-to articles, tricks, and solutions about PYTHON

pip install from git repo branch

You can use pip to install a package from a git repository and specify a branch by using the following syntax:

How to retrieve a module's path?

You can use the __file__ attribute of a module to retrieve its path.

Writing a pandas DataFrame to CSV file

In the above code snippet, the to_csv method is used to write a DataFrame to a CSV file.

Python's equivalent of && (logical-and) in an if-statement

In Python, the logical "and" operator is represented by the and keyword.

How can I write a `try`/`except` block that catches all exceptions?

You can catch all exceptions by using the Exception class in the except block, like this:

Find all files in a directory with extension .txt in Python

Here is a code snippet that uses the os and glob modules to find all files in a directory with the extension '.txt':

Find which version of package is installed with pip

You can use the following command to check the version of a package installed with pip:

How to set environment variables in Python?

In Python, you can set environment variables using the os module.

How do I unload (reload) a Python module?

To unload a Python module, you can use the del statement to remove it from memory.

How to concatenate (join) items in a list to a single string

You can use the join() method to concatenate (join) items in a list to a single string in Python.

How do I remove/delete a folder that is not empty?

You can use the shutil module in Python to remove a folder that is not empty.

List comprehension vs. lambda + filter

List comprehension and the combination of lambda functions and the filter() function in Python are both used to filter a list and return a new list with only the elements that satisfy a certain condition.

How to create a GUID/UUID in Python

You can use the uuid module in Python to generate a globally unique identifier (GUID), also known as a universally unique identifier (UUID).

What is the easiest way to remove all packages installed by pip?

The easiest way to remove all packages installed by pip is to use the command pip freeze to get a list of all installed packages, and then pipe that list to pip uninstall -y, like this:

Should I use 'has_key()' or 'in' on Python dicts?

It is recommended to use the in keyword to check if a key exists in a Python dict, rather than the has_key() method.

How do I type hint a method with the type of the enclosing class?

In Python, you can use the self keyword to refer to the instance of the enclosing class within a method.

Converting unix timestamp string to readable date

You can use the datetime module in Python to convert a UNIX timestamp string to a readable date.

Convert list of dictionaries to a pandas DataFrame

Here is an example of how to convert a list of dictionaries to a pandas DataFrame:

How to print a number using commas as thousands separators

In Python, you can use the built-in function format() to print a number with commas as thousands separators.

Convert a String representation of a Dictionary to a dictionary

You can use the json module in Python to convert a string representation of a dictionary to a dictionary.

How to subtract a day from a date?

You can use the datetime module in Python to subtract a day from a date.

Pretty-print an entire Pandas Series / DataFrame

You can use the .head() method to print the first few rows of a Pandas Series or DataFrame in a "pretty" format.