Skip to main content

RecentItemsList acts like a list, except that calling the "bump()" method on it

Project description

recent_items_list

A list -like class which can "bump" items to the top.

By default, only the last 10 items are kept in the list. If a new item is "bumped" to the beginning of a RecentItemsList that already has 10 items, the item at the end of the list is dropped. You can change this by setting the "maxlen" property on an instance of RecentItemsList.

The two most used methods are "bump()" and "remove()".

When an item needs to be added OR moved to the top:

self._recent_files.bump(filename)

When item needs to be removed:

self._recent_files.remove(filename)

Example:

This is a simple implementation of a "Recent Files" menu.

Initializing

def __init__(self):
	self._recent_files = RecentItemsList(self.settings.value("recent_files", defaultValue = []))
	self.menuOpen_Recent.aboutToShow.connect(self.fill_recent_files)

Filling the menu:

@pyqtSlot()
def fill_recent_files(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)

Saving to QSettings:

self.settings.setValue("recent_files", self._recent_files.items)

Auto-save

Optionally, you provide a callback which will be called whenever the list changes:

self._recent_files = RecentItemsList(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 implementation:

def recent_files():
	global RECENT_FILES
	def sync(items):
		settings().setValue(KEY_RECENT_FILES, items)
	if RECENT_FILES is None:
		RECENT_FILES = RecentItemsList(settings().value(KEY_RECENT_FILES, []))
		RECENT_FILES.on_change(sync)
	return RECENT_FILES

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recent_items_list-1.1.2.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recent_items_list-1.1.2-py2.py3-none-any.whl (14.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file recent_items_list-1.1.2.tar.gz.

File metadata

  • Download URL: recent_items_list-1.1.2.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for recent_items_list-1.1.2.tar.gz
Algorithm Hash digest
SHA256 1285ca8fbc4441e273a5b379d382f061293a008ab4ef3fa5186d8591eefb1609
MD5 9e68df1de6b89b541273afecac797a12
BLAKE2b-256 f34278f972405637fa1d1d0e3a3b31e57bdf2995901add40e8d3b2aa15903e5d

See more details on using hashes here.

File details

Details for the file recent_items_list-1.1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for recent_items_list-1.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2797e965220ea5d403e40aad7622a220db73f15131787acefe0029ff3954abdd
MD5 89758a0552ebd87a30a380f43d75860b
BLAKE2b-256 4b58e3ffcbdd7358e2230432abdc891f9ae0b85a3370d7507caa2b370aacda2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page