File size: 681 Bytes
3f50570
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from types import SimpleNamespace


def dict2cfg(d):
    """
    Converts a dictionary into a SimpleNamespace
    """
    for k, v in d.items():
        if type(v) == dict:
            d[k] = SimpleNamespace(**v)
    c = SimpleNamespace(**d)
    c.audio.max_len = int(c.audio.max_time * c.audio.sample_rate)
    return c


def cfg2dict(cfg):
    """
    Converts a SimpleNamespace into a dictionary without modifying the original cfg.
    """
    d = vars(cfg).copy()  # Make a shallow copy of the cfg's __dict__
    for k, v in d.items():
        if isinstance(v, SimpleNamespace):
            d[k] = cfg2dict(v)  # Recursively convert nested SimpleNamespace objects
    return d