File size: 1,608 Bytes
3177298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from typing import Callable


def traverse(js, callback: Callable, *args, **kwargs):
    if isinstance(js, list):
        result = list()
        for l in js:
            l = traverse(l, callback, *args, **kwargs)
            result.append(l)
        return result
    elif isinstance(js, tuple):
        result = list()
        for l in js:
            l = traverse(l, callback, *args, **kwargs)
            result.append(l)
        return tuple(result)
    elif isinstance(js, dict):
        result = dict()
        for k, v in js.items():
            k = traverse(k, callback, *args, **kwargs)
            v = traverse(v, callback, *args, **kwargs)
            result[k] = v
        return result
    elif isinstance(js, int):
        return callback(js, *args, **kwargs)
    elif isinstance(js, str):
        return callback(js, *args, **kwargs)
    else:
        return js


def demo1():
    d = {
        "env": "ppe",
        "mysql_connect": {
            "host": "$mysql_connect_host",
            "port": 3306,
            "user": "callbot",
            "password": "NxcloudAI2021!",
            "database": "callbot_ppe",
            "charset": "utf8"
        },
        "es_connect": {
            "hosts": ["10.20.251.8"],
            "http_auth": ["elastic", "ElasticAI2021!"],
            "port": 9200
        }
    }

    def callback(s):
        if isinstance(s, str) and s.startswith('$'):
            return s[1:]
        return s

    result = traverse(d, callback=callback)
    print(result)
    return


if __name__ == '__main__':
    demo1()