How-to articles, tricks, and solutions about PYTHON

Getting a list of all subdirectories in the current directory

Here is a code snippet in Python that gets a list of all subdirectories in the current directory:

How to identify which OS Python is running on?

You can use the platform module in Python to identify which operating system the code is running on.

What is __future__ in Python used for and how/when to use it, and how it works

The __future__ module in Python allows you to enable new language features which are not compatible with the current version of Python.

open() in Python does not create a file if it doesn't exist

Here is a code snippet that demonstrates how to use the open() function in Python to create a new file if it does not already exist:

What does -> mean in Python function definitions?

In Python, the "->" symbol is used to indicate the return type of a function.

Shuffling a list of objects

The random module in Python provides a function called shuffle() which can be used to shuffle the elements of a list.

List of lists changes reflected across sublists unexpectedly

In Python, when you create a list of lists and modify one of the sublists, the change is reflected in all other sublists as well because they are all pointing to the same object in memory.

What does functools.wraps do?

functools.wraps is a decorator that can be used to modify a function or method by updating its metadata.

Convert date to datetime in Python

You can convert a date to datetime in Python using the datetime module.

Get key by value in dictionary

You can use the in keyword to check if a value exists in a dictionary, and then use the items() method to return a list of key-value pairs.

Why does Python code run faster in a function?

Python code can run faster in a function because of something called "Just-In-Time" (JIT) compilation.

What is a clean "pythonic" way to implement multiple constructors?

A "pythonic" way to implement multiple constructors in Python is to use the @classmethod decorator.

What is the best project structure for a Python application?

There is no one "best" project structure for a Python application, as it often depends on the specific requirements and goals of the project.

python setup.py uninstall

The command python setup.py uninstall is not a built-in command in Python and it may not work as expected.

List comprehension vs map

List comprehension and map() are both used to create new lists in Python, but they are used in slightly different ways.

Writing a list to a file with Python, with newlines

Here is a code snippet that demonstrates how to write a list to a file with newlines in Python:

How do you get the logical xor of two variables in Python?

You can use the ^ operator to get the logical XOR of two variables in Python.

Remove empty strings from a list of strings

This code uses a list comprehension to iterate through the original list and only keep the elements that are not empty strings.

How to properly ignore exceptions

It's generally not recommended to ignore exceptions, as they often indicate a problem in the code that should be addressed.

How do I do a not equal in Django queryset filtering?

You can use the exclude() method to filter out records where a certain field is not equal to a certain value.

How do I detect whether a variable is a function?

In Python, you can use the built-in callable() function to check if a variable is a function.

UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

This error occurs when trying to decode a string using the 'charmap' codec, which is typically used for Windows-1252 character encoding.

How do I expand the output display to see more columns of a Pandas DataFrame?

You can expand the output display of a Pandas DataFrame by setting the option 'display.max_columns' in pandas.

pip install mysql-python fails with EnvironmentError: mysql_config not found

This error occurs when the mysql-config command is not in the system's PATH.

How to remove items from a list while iterating?

It is generally not recommended to remove items from a list while iterating over it because it can cause unexpected behavior.