Skip to main content

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. Setting the "maxlen" property to < 1 makes for an infinite length list, constrained only by system resourecs.

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)

append

Alias of "bump"

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: list):

...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("RecentFiles", 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("RecentFiles", self.recent_files.items)

"RecentFiles" above is the setting key. It's just a string value. It could be anything.

on_change callback

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("RecentFiles", defaultValue = []))
self.recent_files.on_change(self.save_recent_files)

def save_recent_files(self, items):
	self.settings.setValue("RecentFiles", items)

Another complete example:

from functools import partial
from qt_extras.settings import init_settings, get_setting, set_setting
from recent_items_list import RecentFilesList

class Window(QMainWindow):

	def __init__(self):
		super().__init__()
		init_settings("ZenSoSo", __package__)
		self.recent_files = RecentFilesList(get_setting("RecentFiles", []))
		recent_files.list.on_change(self.sync_recent_files)
		self.menuOpen_Recent.aboutToShow.connect(self.fill_recent_file_menu)

	def sync_recent_files(self, items):
		set_setting("RecentFiles", items)

	@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)

Metadata

Release files for recent-items-list 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for recent-items-list 1.2.2
File Size Uploaded Provenance
recent_items_list-1.2.2.tar.gz 15.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for recent-items-list 1.2.2
File Interpreter ABI Platform
recent_items_list-1.2.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 30.3 kB

Release files / recent_items_list-1.2.2.tar.gz

Download URL recent_items_list-1.2.2.tar.gz
Size 15.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2c62e9c2fef2ebdb902aac23bb0a35301adbd85ae06b4d9b719d61db5ca23c57
BLAKE2b-256 checksum
How to use checksums
eea20ee4657b8aa6f1fe55fc69abcfeab3d80296522428a92db9acb00a4dcc65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / recent_items_list-1.2.2-py2.py3-none-any.whl

Download URL recent_items_list-1.2.2-py2.py3-none-any.whl
Size 15.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ff40878327bcdcef422a095741f5babf7b6ed0789cf57dd9b45542092dfb7acd
BLAKE2b-256 checksum
How to use checksums
79d122aeaf3044f023fe03433871ba07fca5b7473c30bcc9b8e671a2c51e3add
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for recent-items-list 1.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page