[Python] Efficient way to cyclically update data of a class instance already saved on disk

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    There is a class Class containing an initial empty list list and a method to add an element to the list. Each time an element was added to the list the whole instance should be saved to disk. So in most cases the already saved data only needs to be extended by the data of the last added element.

    Is there an efficient way to achive such a behavior? Deleting the saved data and writing it to disk again seems not to be very efficient. I'm looking for a kind of "update already saved data" like i.e. Git does when doing a commit.

    At the moment I save the data using pickle. I prefer to save the whole Class-instance to not loose the class reference.

    from pathlib import Path

    class Class():
    def __init__(self) -> None:
    self.list: list[object] = []

    def add_element(self, obj: object, path: Path) -> None:
    self.list.append(obj)
    # now update already saved data in directory given by `path`

    Continue reading...

Compartilhe esta Página