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.

persistentValue.(any function attribute that modifies its own value) <- doesn't work, the object requires explicit assignments to update the value and cache it.


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

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

def get_random_activity():
    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.value = [ch[0]] + last()[:-1] # <- modify last using assignment

    return ch[0]

if __name__ == "__main__":
    print(get_random_activity()) # <- each 

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

Process finished with exit code 0
# ./test_pers1.py

from persistentvalues import PersistentValue

a = PersistentValue(0, access=1)

print(a)
1

Process finished with exit code 0

Metadata

Release files for persistentvalues 0.0.1

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.1
File Interpreter ABI Platform
persistentvalues-0.0.1-py3-none-any.whl Python 3 none any Details

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

Download URL persistentvalues-0.0.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fa6ed9977c4c41b1a3557ec9779ac8b2b55d5d7786558ecfa6e1e5d3b00e39d
BLAKE2b-256 checksum
How to use checksums
3530002b8736db32bfcb849495b09793a1e6d7925947249211972cba2494dcd6
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

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

This release

0.0.1 This release

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