Skip to main content

pref - a simple local preferences store

Since 0.5:

  • Where the file lives. By default the per-user config directory from platformdirs (e.g. %LOCALAPPDATA%\\author\\myapp on Windows, ~/.config/myapp on Linux). Pass config_dir= to Pref, PrefOrderedSet or PrefStore to put it anywhere else, such as beside an application's own data files. On macOS a directory left by pref 0.4 under ~/Library/Preferences keeps being used.
  • Validation and conversion. An attribute's attrs converter and validator run on every set and on every load, not only at construction: a rejected value raises on set and is neither stored nor kept, and a stored value that no longer validates (a hand-edited file, an older version's range) falls back to the default with a warning.
@attrs
class MyPref(Pref):
    poll_seconds: float = attrib(default=2.0, converter=float, validator=attr.validators.ge(0.5))

Persistent storage of attrs attributes or an ordered set (like a list, but no duplicates) to a local SQLite database file.

Example

from attr import attrib, attrs
from ismain import is_main

from pref import Pref, PrefOrderedSet

application_name = "myapp"
author = "me"


@attrs
class MyPref(Pref):
    first_name: str = attrib(default=None)
    last_name: str = attrib(default=None)
    has_subscription: bool = attrib(default=False)  # start off with no subscription


def get_pref() -> MyPref:
    return MyPref(application_name, author)


def get_ordered_set() -> PrefOrderedSet:
    return PrefOrderedSet(application_name, author, "mylist")


if is_main():

    # set a variable
    preferences = get_pref()
    preferences.first_name = "James"
    preferences.last_name = "Abel"

    # read it back
    preferences = get_pref()
    print(preferences.first_name)  # James
    print(preferences.last_name)  # Abel
    print(preferences.has_subscription)  # evaluates as False (is actually int of 0)

    # set an ordered set (list-like, but no duplicates)
    my_list = get_ordered_set()
    my_list.set(["a", "b", "c"])

    # read the ordered set back in
    my_list = get_ordered_set()
    print(my_list.get())  # ['a', 'b', 'c']

Metadata

Release files for pref 0.5.0

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

Source distribution (sdist)

Source distribution for pref 0.5.0
File Size Uploaded
pref-0.5.0.tar.gz 7.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pref 0.5.0
File Interpreter ABI Platform
pref-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / pref-0.5.0.tar.gz

Download URL pref-0.5.0.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ab549479e3c724675b966356a302060c09470ab1c868e88fb7b020fda2e4581b
BLAKE2b-256 checksum
How to use checksums
48789da9f362aa55df44b0f2ec69d7ee3efcb23582d950789f6bdbb0b4fa189e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / pref-0.5.0-py3-none-any.whl

Download URL pref-0.5.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc0b6339ed53ca4bd93cb03770f27260d9c3d946482f0a0484d3e4f3ed8aa391
BLAKE2b-256 checksum
How to use checksums
868d506573b49e7773a8ac69a1edb7b342e5504c6813a9a1e3fd7cc84b3a203b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

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