Quick Note on pyenv: Python Version Management Tool
This is a quick note on how to use pyenv, a tool for switching between different Python versions.
List of Commands
For Installing Python
-
Display the list of installed Python versions:
Implementing TTL Cache in Python (3)
In this article, we will extend our previously implemented TTL cache decorator with a toggle feature for enabling and disabling the cache.
Overview of Cache Toggle Feature
Here are the specifications for the new cache toggle feature:
- Functions decorated with
lru_cache
will have an additionaluse_cache
argument, allowing one to toggle the cache functionality. - If
use_cache
is set toFalse
, the function will execute without using the cache,
but its result will still be stored in the cache. - If toggled back to
use_cache=True
, the cached content will be utilized.
Implementation
There are various possible ways to implement this feature. Here’s how I approached it:
Implementing TTL Cache in Python (2)
Overview
Continuing from our last article, we’ll delve deeper into implementing a cache with Time-to-Live (TTL).
Let’s quickly recap what we covered previously.Summary of Part 1
- We learned how to equip functions with cache capabilities using
lru_cache
. - We implemented the
get_ttl_hash
function, which updates return values at specified intervals. - We were able to give functions TTL caching by:
- Enhancing them with the caching functionality of
lru_cache
. - Adding a dummy argument.
- Inputting the return value of
get_ttl_hash
to the dummy argument.
- Enhancing them with the caching functionality of
Our goal in this article is to create a decorator that automates this process.
Implementing TTL Cache in Python (1)
Overview
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.
Adding Caching to a Function
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.
NoteThis method also works with Google Colaboratory.Creating a Notification Function
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.NoteThis method also works with Google Colaboratory.1.
%history
Magic Command-
By running the following command in a notebook cell, you can display the code execution history:
- Functions decorated with