Skip to content

serialize.py

is_serializable

is_serializable(value: Any) -> bool

Check value is serializable and can be written to a JSON file.

Parameters:

  • value

    (Any) –

    to be checked.

Returns:

  • bool

    true if value is serializable.

Source code in .venv/lib/python3.14/site-packages/lvn/base/serialize.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def is_serializable(value: Any,) -> bool:
    """
    Check value is serializable and can be written to a JSON file.

    Args:
        value: to be checked.

    Returns: 
        true if value is serializable.
    """
    try:
        dumps(value)
        return True
    except TypeError:
        return False

from_serializable

from_serializable(value: Any, module: Any) -> Any

Convert dict from serializable version.

Parameters:

  • value

    (Any) –

    to be converted.

  • module

    (Any) –

    dplvn or other class module

Returns: converted value.

Source code in .venv/lib/python3.14/site-packages/lvn/base/serialize.py
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
def from_serializable(value: Any, module: Any) -> Any:
    """
    Convert dict from serializable version.

    Args:
        value: to be converted.
        module: dplvn or other class module

    Returns:  converted value.
    """
    match type(value):
        case builtins.str:
            match value:
                case "D1":
                    return module.D1
                case "D2":
                    return module.D2
                case "D3":
                    return module.D3
                case "RANDOM_UNIFORM":
                    return module.RANDOM_UNIFORM
                case "RANDOM_GAUSSIAN":
                    return module.RANDOM_GAUSSIAN
                case "CONSTANT_VALUE":
                    return module.CONSTANT_VALUE
                case "SINGLE_SEED":
                    return module.SINGLE_SEED
                case "RUNGE_KUTTA":
                    return module.RUNGE_KUTTA
                case "EULER":
                    return module.EULER
                case _:
                    return None
        case builtins.tuple | builtins.list:
            combo: list = []
            if type(value[0])!=builtins.str:
                return tuple(value)
            for value_ in value:
                match value_:
                    case "BOUNDED":
                        combo += [module.BOUNDED]
                        continue
                    case "PERIODIC":
                        combo += [module.PERIODIC]
                        continue
                    case "FLOATING":
                        combo += [module.FLOATING]
                        continue
                    case "FIXED_VALUE":
                        combo += [module.FIXED_VALUE]
                        continue
                    case "FIXED_FLUX":
                        combo += [module.FIXED_FLUX]
                        continue
                    case _:
                        combo += [None]
                        continue
            return tuple(combo)
        case _:
            return value

to_serializable

to_serializable(value: Any, module: Any) -> Any

Convert value into serializable version.

Parameters:

  • value

    (Any) –

    to be converted.

  • module

    (Any) –

    dplvn or other class module

Returns:

  • Any

    serializable value.

Source code in .venv/lib/python3.14/site-packages/lvn/base/serialize.py
 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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 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
def to_serializable(value: Any, module: Any,) -> Any:
    """
    Convert value into serializable version.

    Args:
        value: to be converted.
        module: dplvn or other class module

    Returns: 
        serializable value.
    """
    match type(value):
        case builtins.str:
            return value
        case np.float64 | builtins.float:
            return float(value)
        case builtins.int:
            return int(value)
        case builtins.bool:
            return value
        case builtins.list:
            return value
        case module.GridDimension:
            match value:
                case module.D1:
                    return "D1"
                case module.D2:
                    return "D2"
                case module.D3:
                    return "D3"
                case _:
                    return None
        case module.InitialCondition:
            match value:
                case module.RANDOM_UNIFORM:
                     return "RANDOM_UNIFORM"
                case module.RANDOM_GAUSSIAN:
                     return "RANDOM_GAUSSIAN"
                case module.CONSTANT_VALUE:
                     return "CONSTANT_VALUE"
                case module.SINGLE_SEED:
                     return "SINGLE_SEED"
                case _:
                    return None
        case module.IntegrationMethod:
            match value:
                case module.RUNGE_KUTTA:
                    return "RUNGE_KUTTA"
                case module.EULER:
                    return "EULER"
                case _:
                    return None
        case builtins.tuple:
            if is_serializable(value[0]) and is_serializable(value):
                return value
            combo: list = []
            for value_ in value:
                match value_:
                    case module.BOUNDED:
                        combo += ["BOUNDED"]
                        continue
                    case module.PERIODIC:
                        combo += ["PERIODIC"]
                        continue
                    case module.FLOATING:
                        combo += ["FLOATING"] 
                        continue
                    case module.FIXED_VALUE:
                        combo += ["FIXED_VALUE"] 
                        continue
                    case module.FIXED_FLUX :
                        combo += ["FIXED_FLUX"] 
                        continue
                    case _:
                        combo += [None]
                        continue
            return combo
        case np.ndarray:
            return value.tolist()
        case _:
            return value