Home

Python has become essential in todays IT world.

Posts

  • Reorder items in a list - Dec 13, 2024

    Lists in Python are ordered collections of items. You can reorder the items in a list using the sort method or by manually moving items around in the list. So let’s start with an example of how you can print an unsorted list as items at index 2 and 3 are not in order.

    Below is an example of how you can reorder items in a list with the sort method and is in this case sufficient as the items are sorted in ascending order. With the parameter reverse=True, you can sort the items in descending order. And with the parameter key, you can specify a custom sorting function.

    Read more ...

  • Speeding up functions in Python - Jan 20, 2024

    Python’s versatility and ease of use make it a popular choice for a wide range of programming tasks. However, performance can sometimes be a concern, especially when working with large data sets or computationally intensive tasks. Repetitive calculations can be a particular problem, but Python provides a number of tools to help speed up these functions. One way is to use the functools.cache decorator to cache the results of a function which was introduced in Python 3.9. This can be used to avoid repeating calculations, and can significantly speed up the execution of a function.

    Lets take a look at a simple example. The following function calculates the factorial of a number and is a good candidate for caching. With the timeit module we can see how long it takes to run the function.

    Read more ...

  • Using the itertools.batched function in Python 3.12 - Jan 15, 2024

    Python 3.12 introduced a new feature called itertools.batched() within the itertools module, which simplifies the process of creating iterators that return chunks (batches) of a given iterable. Before this function, developers typically had to write their own custom code to achieve the same result.

    Prior to Python 3.12, there were a few common ways to create batched iterators. One of the most popular and Pythonic methods involved a custom generator function. This approach was highly flexible and efficient, as it processed data lazily (on demand).

    Read more ...

  • Format datetime easily - Dec 21, 2023

    Formatting datetime is a common task in Python and could be a daunting task for those new to Python. Especially when you need to format datetime in a specific way and looking at the documentation can be daunting for some. The example below shows how to format datetime in a specific way with the time.strftime() method, which can be complicated due to all the extra lines.

    Since the introduction of Python 3.6, there is a new way to format datetime with the f-string. This makes it easier to format datetime in a specific way. The example below shows how to format datetime in a specific way with the f-string.

    Read more ...

  • Use dotenv to load environment variables - Nov 14, 2023

    Developing and deploying applications often requires the use of configuration variables such as passwords, API keys, and secret keys. These variables should not be hardcoded in the source code, as they can be accessed by unauthorized users. The dotenv library can be used to load environment variables from a file when it is started. This allows you to store sensitive information in a file that is not uploaded to a repository and to override the environment variables for development, testing, and debugging. This allows an application to be 12-factor compliant and to be deployed to different environments without changing the source code.

    As mentioned variables that contain sensitive information, such as passwords, API keys, and secret keys, should not be hardcoded in the source code. The example code below shows a hardcoded secret key and should not be uploaded to a repository.

    Read more ...

See all posts ➡