File: shorthand_box.py

package info (click to toggle)
python-box 7.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 572 kB
  • sloc: python: 3,471; makefile: 4
file content (69 lines) | stat: -rw-r--r-- 1,470 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Dict

from box.box import Box

__all__ = ["SBox", "DDBox"]


class SBox(Box):
    """
    ShorthandBox (SBox) allows for
    property access of `dict` `json` and `yaml`
    """

    _protected_keys = dir({}) + [
        "to_dict",
        "to_json",
        "to_yaml",
        "json",
        "yaml",
        "from_yaml",
        "from_json",
        "dict",
        "toml",
        "from_toml",
        "to_toml",
    ]

    @property
    def dict(self) -> Dict:
        return self.to_dict()

    @property
    def json(self) -> str:
        return self.to_json()

    @property
    def yaml(self) -> str:
        return self.to_yaml()

    @property
    def toml(self) -> str:
        return self.to_toml()

    def __repr__(self):
        return f"{self.__class__.__name__}({self})"

    def copy(self) -> "SBox":
        return SBox(super(SBox, self).copy())

    def __copy__(self) -> "SBox":
        return SBox(super(SBox, self).copy())


class DDBox(SBox):
    def __init__(self, *args, **kwargs):
        kwargs["box_dots"] = True
        kwargs["default_box"] = True
        super().__init__(*args, **kwargs)

    def __new__(cls, *args, **kwargs):
        obj = super().__new__(cls, *args, **kwargs)
        obj._box_config["box_dots"] = True
        obj._box_config["default_box"] = True
        return obj

    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({self})"