-1

I was making this settings manager which saves and loads data using json everytime any of the variables is updated.

like if i change variable named value , class should on its own run function named save() and load()

my folder Tree.

in Backend.Attributes file

class Attributes:

    def __init__(self, name: str , value = None, minimum: int|float|complex = 0, maximum : int|float|complex = 0, default : int|float|complex = 0 , use_default : bool = True , *args , **kwargs):

        '''
        Attributes for objects.
        name : compulsory: any name
        value: not cumpolsary : any
        minimum: not compulsory: minimum value. 
        maximum: not cumpulsory: maximum value
        default : default value
        use_default, if want to use default value as main value

        infinite number of arguments and keyword arguments
        '''

        self.name      = name
        self.value     = value if minimum == 0 and maximum == 0 else self.default if use_default else value
        self.minimum   = minimum
        self.maximum   = maximum
        self.undefined = args
        self.__d       = default
        for keys, values in kwargs.items():
            self.__dict__[keys] = values

    @property
    def default(self):
        return (self.maximum - self.minimum)/2 if self.__d == 0 else self.__d
    
    @property
    def range(self):
        return self.maximum - self.minimum

    def __contains__(self, __v):
        return True if __v <= self.maximum and __v >= self.minimum else False

    def __setitem__(self, __a, __v):
        self.__dict__[__a] = __v

    def __getitem__(self, __a):
        return self.__dict__[__a]
    
    def __delitem__(self, __i):
        del self.__dict__[__i]
    
    def __len__(self):
        return self.range

in Settings.global file

import Backend
import json

class Settings(Backend.Attributes):

    def __init__(self, name: str, value=None, minimum: int | float | complex = 0, maximum: int | float | complex = 0, default: int | float | complex = None, use_default: bool = True, *args, **kwargs):
        super().__init__(name, value, minimum, maximum, default, use_default, *args, **kwargs)
        self.BaseLocation = f"{Backend.Locations.Settings.path}/{self.name}"
        try:
            self.Load()
        except:
            self.Save()
        else:
            self.Load()
   
    def Save(self):
        print("saved")
        with open(f"{self.BaseLocation}.json", "w") as file: 
            json.dump(self.__dict__ , file , indent = 4)
        return True

    def Load(self):
        print("loaded")
        with open(f"{self.BaseLocation}.json", "r") as file:
            self.__dict__ = json.load(file)
        return True

All i want to do is to run save() and load() function everytime any of variables in attributes class is updated. without constantly comparing previous and new variables by threading (its so ram consuming).

Mohd Zaid
  • 13
  • 2

1 Answers1

0

You can use the setter property:

class Test:
    def __init__(self, x):
        self.x = x
    @property
    def x(self):
        return self._x
    @x.setter
    def x(self, value):
        self._x = value
        self.test() # call your function
    def test(self):
        print('x is set')

t = Test(1)
>>> 'x is set'
t.x = 2
>>> 'x i set'

3dSpatialUser
  • 2,034
  • 1
  • 9
  • 18