Skip to main content

persistentvalues

Contains a PersistentValue object that stores the input object as self.value and allows you to interact with it directly as if the object is its own value.


To modify the stored value, use:

persistentValue.value = (something)

or

persistentValue.set((something))

To get the stored value, use:

(variable) = persistentValue.value

or

(variable) = persistentValue()

Mutations work too!

persistentValue = PersistentValue([2, 3, 1])
persistentValue.sort()
print(persistentValue)
[1, 2, 3]

Unfortunately I could not yet implement generic hinting for persistent values, so here is one of the ways to tell your IDE that your persistent value should behave almost the same as its value:

l: Union[list, PersistentValue] = PersistentValue([1, 2, 3])
"""
^ l now has both 'PersistentValue' and 'list' hints
"""

or if you're on Python 3.10 and higher:

l: list | PersistentValue = PersistentValue([1, 2, 3])

(Remember that assignment to l is still requiring l.value to be modified, it's just now easier to work with your value)


The input value of PersistentValue(0) is an id determining what cache corresponds to which PersistentValue. That means the following code will use the same cached value despite the intention obviously being in creation of two different persistent values:

a = PersistentValue(0) # <- create a new persistentValue
b = PersistentValue(0) # <- has the same id as the previous one, so behaves like the same object

a += 2 # <- now stores 2
b += 3 # <- now stores 5

You can define your own id using:

a = PersistentValue(0, id=(your id))

Which means:

a = PersistentValue(0, "a") # <- create a new persistentValue with id "a"
b = PersistentValue(0, "b") # <- create a new persistentValue with id "b"

a += 2 # <- "a" now stores 2
b += 3 # <- "b" now stores 3

Your persistent values may have different types of access depending on your intentions:

acc_0 = PersistentValue(None, id="acc_0", access=("script" or 0))
"""
^ (Default) Local script access, values with the same id from different scripts don't overlap.
  Python console is the exception, it is actually access=1 by default.
"""

acc_1 = PersistentValue(None, id="acc_1", access=("cwd" or "local" or 1))
"""
^ Local current working directory access, values with the same id from different working directories
  don't overlap, but can be accessed across different scripts from the same working directory.
"""

acc_2 = PersistentValue(None, id="acc_2", access=("global" or 2))
"""
^ Global device access, any values with the same id will be accessible across any scripts on your
  device from any projects as long as they all use the same cache_dir folder (Default "~/.cachier/").
  Requires access to the cache_dir from the script context.
"""

Additional parameters:

PersistentValue(cache_dir=(your path))
"""
^ Cache path to store values at. Overrides the default automatic one determined by access.
  
  Default:
      access=0 -> ".cache/"
      access=1 -> ".cache/"
      access=2 -> "~/.cachier/"
"""

PersistentValue(separate_files=(True or False))
"""
^ Instead of a single cache file per-function, each function's cache is split between
  several files, one for each argument set. This can help if your per-function cache
  files become too large.
  
  Default: False
"""

PersistentValue functions:

persistentValue.sync() # <- Syncs the uncached stored value with the cached one. (sets self._value = (cached)self.value)

persistentValue.update_cache() # <- Syncs the cached stored value with the uncached one. (sets (cached)self.value = self._value)

persistentValue.clear_cache() # <- Clears the cached value

Example usage:

import random
from persistentvalues import PersistentValue

a = """Option 0
Option 1
Option 2
Option 3
Option 4
Option 5
Option 6""".split("\n")

b: list | PersistentValue = PersistentValue([(i, 0) for i in a], "b") # <- store the initial value under id "b"
last: list | PersistentValue = PersistentValue([None for _ in range(2)]) # <- store the initial value under procedural id derived from the initial value

def get_random_option():
    global last, b
    ch = random.choices(b(), weights=[(1 / j if i not in last() else 0) if j > 0 else 1 for i, j in b()], k=1)[0] # <- get the values here and use them just like the initial ones

    b[b.index(ch)] = (ch[0], ch[1] + 1) # <- modify b using assignment

    last.insert(0, ch); last.pop(-1) # <- modify last using mutation

    return ch[0]

if __name__ == "__main__":
    print(get_random_option()) # <- each time the program is executed we update the list of already chosen options 

Console Example:

>>> from persistentvalues import PersistentValue
>>> a = PersistentValue(0)
>>> a.value += 1
>>> a
1

Process finished with exit code 0

>>> from persistentvalues import PersistentValue
>>> a = PersistentValue(0)
>>> a
1

Process finished with exit code 0

Local current working directory access:

# ./test_pers0.py

from persistentvalues import PersistentValue

a = PersistentValue(0, access=1)

a.value += 1

print(a)
1
# ./test_pers1.py

from persistentvalues import PersistentValue

a = PersistentValue(0, access=1)

print(a)
1

Metadata

Release files for persistentvalues 0.0.5

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

Built distribution (wheel)

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

Release files / persistentvalues-0.0.5-py3-none-any.whl

Download URL persistentvalues-0.0.5-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4c8ff212de56a2d14ab314f6241218fb21939d162e182d36db776fc14f0c142
BLAKE2b-256 checksum
How to use checksums
b3429ac5652e97559d3b6541deedc4453c0d378fdd0dd81ecf0f03413b99b6b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.0

Release history Release notifications | RSS feed

0.0.6

1 release file

This release

0.0.5 This release

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