kioku-space

As a personal memory space.

Implementing TTL Cache in Python (1)

In this article, we’ll discuss how to implement a caching mechanism in Python that has a Time-To-Live (TTL).
This cache can be useful in scenarios like:

  • Functions that connect to the internet to fetch data, but the data remains constant over a period.
    (e.g., weather forecast data)
  • Functions that are executed frequently but don’t require real-time data.

First, let’s see how to add caching to a Python function.

How to Get Notifications When Exceptions Occur in Jupyter Notebook

Have you ever been running a time-consuming code in Jupyter Notebook, like training a machine learning model, only to realize later that it had stopped due to an unexpected error?

In this article, I’ll introduce a method to automatically send notifications to platforms like Slack or Discord when such exceptions occur.

Note
This method also works with Google Colaboratory.

First, let’s prepare a function for sending notifications. If you’re using Slack or Discord, you can notify with the following functions:

How to Save Jupyter Notebook's Code Execution History as a Python Script

In this article, I’ll introduce how to save the code execution history from Jupyter Notebook (JupyterLab) as a Python script.

First, I’ll go over the commonly used %history magic command and then share a custom function I’ve written.

Note
This method also works with Google Colaboratory.
  • By running the following command in a notebook cell, you can display the code execution history: