Skip to content

classtools.require

sikvelsigma edited this page Aug 18, 2022 · 5 revisions

Summery

  • class RequireAttrs

Inhereting from RequireAttrs forces you to specify what attributes must be set at the end of __init__ call by defining require tuple

  • class RequireDictParser

Inhereting from RequireDictParser let's you parse a dict (or json file) which must containg all attributes specified in require tuple and some combination of parameters for each tuple in require_any tuple. All remaning attributes not in the dict but present in require_any msut be set in set_args method. declare_args must be used for type annotations and inital values (mainly for setting optional args as None)

Contains

RequireAttrs

from pyuseful.classtools.require import RequireAttrs

class Test(RequireAttrs):
    # self.a and self.b must be set by the end of init
    require = ("a", "b")
    def __init__(self):
        self.a = 1
        self.b = 2

t = Test()

RequireDictParser

from pyuseful.classtools.require import RequireDictParser

class Test(RequireDictParser):
    # keys 'a' and 'b' must in a dict or json file
    require = ("a", "b")     

    require_any = (
        # either key 'x' or 'y' must be in a dict or json file
        (("x", "y"), 1),  
        # either combination must be in a dict or json file: 
        # 'q' & 'w', 'q' & 'e', 'w' & 'e'      
        (("q", "w", "e"), 2)    
    )

    # parse str values as numbers ('True' by default)
    infer_numbers = True

    def declare_args(self):
        """ Typing the attributes or setting init values as 'None',
        will raise an error on trying to set a different value
        """
        self.a: float
        self.b: float

        self.x: float
        self.y: float

        self.q: float = None
        self.w: float = None
        self.e: float = None

    def set_args(self):
        # all remaining unset attributes must be set here
        if self.x:
            self.y = self.x + 1
        else:
            self.x = self.y - 1

        if self.q is None:
            self.q = self.w + self.e
        elif self.w is None:
            self.w = self.q - self.e
        else:
            self.e = self.q - self.w

# to read from json
# t = Test.from_json(<path>)

data = dict(
    a=1.1,
    b=2,
    x="10.1",
    q=100,
    w=200,
)
t = Test(data)

Clone this wiki locally