Skip to content

confit.utils.xjson

Reference

A path reference to a value in the configuration.

Source code in confit/utils/xjson.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Reference:
    """
    A path reference to a value in the configuration.
    """

    def __init__(self, value: str):
        """
        Parameters
        ----------
        value: str
            The path to the value in the configuration.
        """
        self.value = value

    def __repr__(self):
        return f"${{{self.value}}}"

    def __str__(self):
        return self.__repr__()

    def __eq__(self, other):
        return self.value == other.value

__init__(value)

PARAMETER DESCRIPTION
value

The path to the value in the configuration.

TYPE: str

Source code in confit/utils/xjson.py
13
14
15
16
17
18
19
20
def __init__(self, value: str):
    """
    Parameters
    ----------
    value: str
        The path to the value in the configuration.
    """
    self.value = value

XJsonTransformer

Bases: Transformer

A Lark transformer to parse extended JSON.

Source code in confit/utils/xjson.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class XJsonTransformer(Transformer):
    """
    A Lark transformer to parse extended JSON.
    """

    def __init__(self, input_string: str):
        """
        Parameters
        ----------
        input_string: str
            The input string to parse.
        """
        super().__init__()
        self.input_string = input_string

    def string(self, s):
        """Parse string"""
        (s,) = s
        s = ast.literal_eval(s)
        return s

    def float(self, n):
        """Parse number"""
        (n,) = n
        return float(n)

    def int(self, n):
        """Parse number"""
        (n,) = n
        return int(n)

    def reference(self, tree: Tree):
        """Parse reference"""
        meta = tree[0].meta
        return Reference(self.input_string[meta.start_pos : meta.end_pos])

    list = list
    tuple = tuple
    pair = tuple
    dict = dict

    def null(self, _):
        """Parse null"""
        return None

    def true(self, _):
        """Parse true"""
        return True

    def false(self, _):
        """Parse false"""
        return False

    def plus_inf(self, _):
        """Parse infinity"""
        return float("inf")

    def minus_inf(self, _):
        """Parse -infinity"""
        return -float("inf")

    def nan(self, _):
        """Parse nan"""
        return float("nan")

__init__(input_string)

PARAMETER DESCRIPTION
input_string

The input string to parse.

TYPE: str

Source code in confit/utils/xjson.py
88
89
90
91
92
93
94
95
96
def __init__(self, input_string: str):
    """
    Parameters
    ----------
    input_string: str
        The input string to parse.
    """
    super().__init__()
    self.input_string = input_string

string(s)

Parse string

Source code in confit/utils/xjson.py
 98
 99
100
101
102
def string(self, s):
    """Parse string"""
    (s,) = s
    s = ast.literal_eval(s)
    return s

float(n)

Parse number

Source code in confit/utils/xjson.py
104
105
106
107
def float(self, n):
    """Parse number"""
    (n,) = n
    return float(n)

int(n)

Parse number

Source code in confit/utils/xjson.py
109
110
111
112
def int(self, n):
    """Parse number"""
    (n,) = n
    return int(n)

reference(tree)

Parse reference

Source code in confit/utils/xjson.py
114
115
116
117
def reference(self, tree: Tree):
    """Parse reference"""
    meta = tree[0].meta
    return Reference(self.input_string[meta.start_pos : meta.end_pos])

null(_)

Parse null

Source code in confit/utils/xjson.py
124
125
126
def null(self, _):
    """Parse null"""
    return None

true(_)

Parse true

Source code in confit/utils/xjson.py
128
129
130
def true(self, _):
    """Parse true"""
    return True

false(_)

Parse false

Source code in confit/utils/xjson.py
132
133
134
def false(self, _):
    """Parse false"""
    return False

plus_inf(_)

Parse infinity

Source code in confit/utils/xjson.py
136
137
138
def plus_inf(self, _):
    """Parse infinity"""
    return float("inf")

minus_inf(_)

Parse -infinity

Source code in confit/utils/xjson.py
140
141
142
def minus_inf(self, _):
    """Parse -infinity"""
    return -float("inf")

nan(_)

Parse nan

Source code in confit/utils/xjson.py
144
145
146
def nan(self, _):
    """Parse nan"""
    return float("nan")

loads(s)

Load an extended JSON string into a python object. Takes care of detecting references and tuples

PARAMETER DESCRIPTION
s

TYPE: str

RETURNS DESCRIPTION
Any
Source code in confit/utils/xjson.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def loads(s: str):
    """
    Load an extended JSON string into a python object.
    Takes care of detecting references and tuples

    Parameters
    ----------
    s: str

    Returns
    -------
    Any
    """
    try:
        return XJsonTransformer(s).transform(_json_parser.parse(s))
    except Exception:
        # Fail if we suspect that it is a malformed object
        # (e.g. has ', ", {, }, [, ] in it)
        if set(s) & set(",'\"{}[]$"):
            raise MalformedValueError(s)
        return s

dumps(o)

Dump a python object into an extended JSON string. Takes care of serializing references and tuples

PARAMETER DESCRIPTION
o

TYPE: Any

RETURNS DESCRIPTION
str
Source code in confit/utils/xjson.py
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def dumps(o: Any):
    """
    Dump a python object into an extended JSON string.
    Takes care of serializing references and tuples

    Parameters
    ----------
    o: Any

    Returns
    -------
    str
    """
    return "".join(_make_iterencode()(o))