recent_items_list
Provides finite length Last Recently Used (LRU) caches.
RecentItemsList
A class which stores any type of object.
Appending or "bump"-ing items adds them to the beginning of the list. If the "maxlen" number of items already exist, the last item is dropped from the list.
By default, only the last 10 items are kept in the list. You can change this by setting the "maxlen" property on an instance of RecentItemsList. Setting the "maxlen" property to < 1 makes for an infinite length list, constrained only by system resourecs.
The two most used methods are "bump" and "remove".
bump
Bump the given item to the beginning of the list. If the given item is not in the list, inserts it at the beginning.
recent_items.bump(thing)
remove
Removes the given item if it is in the list. Does nothing if the item is not in the list.
recent_items.remove(thing)
on_change
Registers a callback which is run every time the list changes. The callback must have the signature:
def item_list_changed(self, items):
...where "item_list_changed" is an example function name.
clear
Removes all items from the list
RecentFilesList
This class extends RecentItemsList, providing a "prune" function to clear the list only of files which don't exist.
prune
Removes files which no longer exist.
Example:
This is a simple implementation of a "Recent Files" menu in PyQt.
Instantiation:
def __init__(self):
self.recent_files = RecentFilesList(self.settings.value(
"recent_files", defaultValue = []))
self.menuOpen_Recent.aboutToShow.connect(self.fill_recent_file_menu)
Filling the Qt menu:
@pyqtSlot()
def fill_recent_file_menu(self):
self.menuOpen_Recent.clear()
actions = []
for filename in self.recent_files:
action = QAction(filename, self)
action.triggered.connect(partial(self.load_file, filename))
actions.append(action)
self.menuOpen_Recent.addActions(actions)
Opening a file:
def open(self, filename):
self.recent_files.bump(filename)
[...]
Saving to a QSettings instance:
self.settings.setValue("recent_files", self.recent_files.items)
An alternate method of saving the most recent file list without having to explicitly save the changes, is to provide a callback which will be called whenever the list changes, which saves the list to QSettings:
self.recent_files = RecentFilesList(self.settings.value(
"recent_files", defaultValue = []))
self.recent_files.on_change(self.save_recent_files)
def save_recent_files(self, items):
self.settings.setValue("recent_files", items)
Another complete example:
from functools import partial
from recent_items_list import RecentFilesList
VENDOR_NAME = 'ZenSoSo'
def recent_files():
def sync(items):
settings().setValue("recent_files", items)
if not hasattr(recent_files, 'list'):
recent_files.list = RecentFilesList(settings().value("recent_files", []))
recent_files.list.on_change(sync)
return recent_files.list
def settings():
if not hasattr(settings, 'object'):
settings.object = QSettings(VENDOR_NAME, __package__)
return settings.object
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.menuOpen_Recent.aboutToShow.connect(self.fill_recent_file_menu)
@pyqtSlot()
def fill_recent_file_menu(self):
self.menuOpen_Recent.clear()
actions = []
for filename in recent_files().prune():
action = QAction(filename, self)
action.triggered.connect(partial(self.load_file, filename))
actions.append(action)
self.menuOpen_Recent.addActions(actions)
def load_file(self, filename):
self.recent_files.bump(filename)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file recent_items_list-1.1.3.tar.gz.
File metadata
- Download URL: recent_items_list-1.1.3.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0722a501bddea72a6d749f8c08ef04153aecab991a51b5cd9271f4d086d428c8
|
|
| MD5 |
62153781644d091fc3eb8abdbc954627
|
|
| BLAKE2b-256 |
2cc8e014f1d28de44699e30fd0d00a25c3e70fbf5777c51140ac6a2d8fce4684
|
File details
Details for the file recent_items_list-1.1.3-py2.py3-none-any.whl.
File metadata
- Download URL: recent_items_list-1.1.3-py2.py3-none-any.whl
- Upload date:
- Size: 15.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0718e964bb94bc228c15cbf4e8dcd085cd082a6fad64bf51c4467377926b2cdb
|
|
| MD5 |
db173f180466270b336c5064ae399909
|
|
| BLAKE2b-256 |
47fb00f520290a03f6fb7a8f22557494c0b35f7055b312b62d704843b1ae1f9b
|