QuLab 2.3.5__tar.gz → 2.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. {qulab-2.3.5 → qulab-2.4.0}/PKG-INFO +6 -3
  2. {qulab-2.3.5 → qulab-2.4.0}/QuLab.egg-info/PKG-INFO +6 -3
  3. {qulab-2.3.5 → qulab-2.4.0}/QuLab.egg-info/SOURCES.txt +12 -0
  4. {qulab-2.3.5 → qulab-2.4.0}/QuLab.egg-info/requires.txt +4 -1
  5. {qulab-2.3.5 → qulab-2.4.0}/pyproject.toml +5 -2
  6. {qulab-2.3.5 → qulab-2.4.0}/qulab/__main__.py +11 -7
  7. qulab-2.4.0/qulab/dicttree.py +511 -0
  8. qulab-2.4.0/qulab/executor/__init__.py +5 -0
  9. qulab-2.4.0/qulab/executor/__main__.py +89 -0
  10. qulab-2.4.0/qulab/executor/load.py +202 -0
  11. qulab-2.4.0/qulab/executor/schedule.py +223 -0
  12. qulab-2.4.0/qulab/executor/storage.py +143 -0
  13. qulab-2.4.0/qulab/executor/transform.py +90 -0
  14. qulab-2.4.0/qulab/executor/utils.py +107 -0
  15. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/curd.py +1 -1
  16. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/expression.py +4 -0
  17. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/models.py +10 -11
  18. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/server.py +6 -3
  19. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/device/basedevice.py +8 -0
  20. qulab-2.4.0/qulab/sys/device/utils.py +79 -0
  21. qulab-2.4.0/qulab/sys/rpc/router.py +35 -0
  22. qulab-2.4.0/qulab/version.py +1 -0
  23. {qulab-2.3.5 → qulab-2.4.0}/qulab/visualization/_autoplot.py +4 -3
  24. qulab-2.4.0/qulab/visualization/plot_circ.py +319 -0
  25. qulab-2.4.0/qulab/visualization/plot_seq.py +242 -0
  26. qulab-2.4.0/qulab/visualization/rot3d.py +23 -0
  27. qulab-2.4.0/tests/test_kad.py +341 -0
  28. qulab-2.3.5/qulab/sys/device/utils.py +0 -46
  29. qulab-2.3.5/qulab/version.py +0 -1
  30. qulab-2.3.5/qulab/visualization/plot_seq.py +0 -90
  31. {qulab-2.3.5 → qulab-2.4.0}/LICENSE +0 -0
  32. {qulab-2.3.5 → qulab-2.4.0}/MANIFEST.in +0 -0
  33. {qulab-2.3.5 → qulab-2.4.0}/QuLab.egg-info/dependency_links.txt +0 -0
  34. {qulab-2.3.5 → qulab-2.4.0}/QuLab.egg-info/entry_points.txt +0 -0
  35. {qulab-2.3.5 → qulab-2.4.0}/QuLab.egg-info/top_level.txt +0 -0
  36. {qulab-2.3.5 → qulab-2.4.0}/README.md +0 -0
  37. {qulab-2.3.5 → qulab-2.4.0}/qulab/__init__.py +0 -0
  38. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/__init__.py +0 -0
  39. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/__main__.py +0 -0
  40. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/config.py +0 -0
  41. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/dataset.py +0 -0
  42. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/event_queue.py +0 -0
  43. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/mainwindow.py +0 -0
  44. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/monitor.py +0 -0
  45. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/ploter.py +0 -0
  46. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/qt_compat.py +0 -0
  47. {qulab-2.3.5 → qulab-2.4.0}/qulab/monitor/toolbar.py +0 -0
  48. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/__init__.py +0 -0
  49. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/optimize.py +0 -0
  50. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/query.py +0 -0
  51. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/record.py +0 -0
  52. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/scan.py +0 -0
  53. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/space.py +0 -0
  54. {qulab-2.3.5 → qulab-2.4.0}/qulab/scan/utils.py +0 -0
  55. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/__init__.py +0 -0
  56. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/__main__.py +0 -0
  57. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/backend/__init__.py +0 -0
  58. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/backend/redis.py +0 -0
  59. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/base_dataset.py +0 -0
  60. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/chunk.py +0 -0
  61. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/dataset.py +0 -0
  62. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/file.py +0 -0
  63. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/__init__.py +0 -0
  64. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/base.py +0 -0
  65. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/config.py +0 -0
  66. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/file.py +0 -0
  67. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/ipy.py +0 -0
  68. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/models.py +0 -0
  69. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/record.py +0 -0
  70. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/report.py +0 -0
  71. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/models/tag.py +0 -0
  72. {qulab-2.3.5 → qulab-2.4.0}/qulab/storage/storage.py +0 -0
  73. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/__init__.py +0 -0
  74. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/chat.py +0 -0
  75. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/device/__init__.py +0 -0
  76. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/device/loader.py +0 -0
  77. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/drivers/FakeInstrument.py +0 -0
  78. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/drivers/__init__.py +0 -0
  79. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/ipy_events.py +0 -0
  80. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/__init__.py +0 -0
  81. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/bencoder.py +0 -0
  82. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/cli.py +0 -0
  83. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/dhcp.py +0 -0
  84. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/dhcpd.py +0 -0
  85. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/kad.py +0 -0
  86. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/kcp.py +0 -0
  87. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/net/nginx.py +0 -0
  88. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/progress.py +0 -0
  89. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/__init__.py +0 -0
  90. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/client.py +0 -0
  91. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/exceptions.py +0 -0
  92. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/msgpack.py +0 -0
  93. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/msgpack.pyi +0 -0
  94. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/rpc.py +0 -0
  95. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/serialize.py +0 -0
  96. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/server.py +0 -0
  97. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/socket.py +0 -0
  98. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/utils.py +0 -0
  99. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/worker.py +0 -0
  100. {qulab-2.3.5 → qulab-2.4.0}/qulab/sys/rpc/zmq_socket.py +0 -0
  101. {qulab-2.3.5 → qulab-2.4.0}/qulab/visualization/__init__.py +0 -0
  102. {qulab-2.3.5 → qulab-2.4.0}/qulab/visualization/__main__.py +0 -0
  103. {qulab-2.3.5 → qulab-2.4.0}/qulab/visualization/plot_layout.py +0 -0
  104. {qulab-2.3.5 → qulab-2.4.0}/qulab/visualization/qdat.py +0 -0
  105. {qulab-2.3.5 → qulab-2.4.0}/qulab/visualization/widgets.py +0 -0
  106. {qulab-2.3.5 → qulab-2.4.0}/setup.cfg +0 -0
  107. {qulab-2.3.5 → qulab-2.4.0}/setup.py +0 -0
  108. {qulab-2.3.5 → qulab-2.4.0}/src/qulab.h +0 -0
  109. {qulab-2.3.5 → qulab-2.4.0}/tests/test_scan.py +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: QuLab
3
- Version: 2.3.5
3
+ Version: 2.4.0
4
4
  Summary: contral instruments and manage data
5
5
  Author-email: feihoo87 <feihoo87@gmail.com>
6
6
  Maintainer-email: feihoo87 <feihoo87@gmail.com>
@@ -32,14 +32,17 @@ Requires-Dist: ipython>=7.4.0
32
32
  Requires-Dist: ipywidgets>=7.4.2
33
33
  Requires-Dist: loguru>=0.7.2
34
34
  Requires-Dist: matplotlib>=3.7.2
35
+ Requires-Dist: msgpack>=1.0.5
35
36
  Requires-Dist: nevergrad>=1.0.2
36
37
  Requires-Dist: numpy>=1.13.3
37
38
  Requires-Dist: ply>=3.11
38
39
  Requires-Dist: pyzmq>=25.1.0
39
40
  Requires-Dist: scipy>=1.0.0
41
+ Requires-Dist: scikit-optimize>=0.9.0
42
+ Requires-Dist: SQLAlchemy>=2.0.19
40
43
  Requires-Dist: watchdog>=4.0.0
44
+ Requires-Dist: waveforms>=1.9.4
41
45
  Provides-Extra: full
42
- Requires-Dist: SQLAlchemy>=2.0.19; extra == "full"
43
46
  Requires-Dist: uvloop>=0.19.0; extra == "full"
44
47
 
45
48
  # QuLab
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: QuLab
3
- Version: 2.3.5
3
+ Version: 2.4.0
4
4
  Summary: contral instruments and manage data
5
5
  Author-email: feihoo87 <feihoo87@gmail.com>
6
6
  Maintainer-email: feihoo87 <feihoo87@gmail.com>
@@ -32,14 +32,17 @@ Requires-Dist: ipython>=7.4.0
32
32
  Requires-Dist: ipywidgets>=7.4.2
33
33
  Requires-Dist: loguru>=0.7.2
34
34
  Requires-Dist: matplotlib>=3.7.2
35
+ Requires-Dist: msgpack>=1.0.5
35
36
  Requires-Dist: nevergrad>=1.0.2
36
37
  Requires-Dist: numpy>=1.13.3
37
38
  Requires-Dist: ply>=3.11
38
39
  Requires-Dist: pyzmq>=25.1.0
39
40
  Requires-Dist: scipy>=1.0.0
41
+ Requires-Dist: scikit-optimize>=0.9.0
42
+ Requires-Dist: SQLAlchemy>=2.0.19
40
43
  Requires-Dist: watchdog>=4.0.0
44
+ Requires-Dist: waveforms>=1.9.4
41
45
  Provides-Extra: full
42
- Requires-Dist: SQLAlchemy>=2.0.19; extra == "full"
43
46
  Requires-Dist: uvloop>=0.19.0; extra == "full"
44
47
 
45
48
  # QuLab
@@ -11,7 +11,15 @@ QuLab.egg-info/requires.txt
11
11
  QuLab.egg-info/top_level.txt
12
12
  qulab/__init__.py
13
13
  qulab/__main__.py
14
+ qulab/dicttree.py
14
15
  qulab/version.py
16
+ qulab/executor/__init__.py
17
+ qulab/executor/__main__.py
18
+ qulab/executor/load.py
19
+ qulab/executor/schedule.py
20
+ qulab/executor/storage.py
21
+ qulab/executor/transform.py
22
+ qulab/executor/utils.py
15
23
  qulab/monitor/__init__.py
16
24
  qulab/monitor/__main__.py
17
25
  qulab/monitor/config.py
@@ -74,6 +82,7 @@ qulab/sys/rpc/client.py
74
82
  qulab/sys/rpc/exceptions.py
75
83
  qulab/sys/rpc/msgpack.py
76
84
  qulab/sys/rpc/msgpack.pyi
85
+ qulab/sys/rpc/router.py
77
86
  qulab/sys/rpc/rpc.py
78
87
  qulab/sys/rpc/serialize.py
79
88
  qulab/sys/rpc/server.py
@@ -84,9 +93,12 @@ qulab/sys/rpc/zmq_socket.py
84
93
  qulab/visualization/__init__.py
85
94
  qulab/visualization/__main__.py
86
95
  qulab/visualization/_autoplot.py
96
+ qulab/visualization/plot_circ.py
87
97
  qulab/visualization/plot_layout.py
88
98
  qulab/visualization/plot_seq.py
89
99
  qulab/visualization/qdat.py
100
+ qulab/visualization/rot3d.py
90
101
  qulab/visualization/widgets.py
91
102
  src/qulab.h
103
+ tests/test_kad.py
92
104
  tests/test_scan.py
@@ -6,13 +6,16 @@ ipython>=7.4.0
6
6
  ipywidgets>=7.4.2
7
7
  loguru>=0.7.2
8
8
  matplotlib>=3.7.2
9
+ msgpack>=1.0.5
9
10
  nevergrad>=1.0.2
10
11
  numpy>=1.13.3
11
12
  ply>=3.11
12
13
  pyzmq>=25.1.0
13
14
  scipy>=1.0.0
15
+ scikit-optimize>=0.9.0
16
+ SQLAlchemy>=2.0.19
14
17
  watchdog>=4.0.0
18
+ waveforms>=1.9.4
15
19
 
16
20
  [full]
17
- SQLAlchemy>=2.0.19
18
21
  uvloop>=0.19.0
@@ -36,18 +36,21 @@ dependencies = [
36
36
  "ipywidgets>=7.4.2",
37
37
  "loguru>=0.7.2",
38
38
  "matplotlib>=3.7.2",
39
+ "msgpack>=1.0.5",
39
40
  "nevergrad>=1.0.2",
40
41
  "numpy>=1.13.3",
41
42
  "ply>=3.11",
42
43
  "pyzmq>=25.1.0",
43
44
  "scipy>=1.0.0",
44
- "watchdog>=4.0.0"
45
+ "scikit-optimize>=0.9.0",
46
+ "SQLAlchemy>=2.0.19",
47
+ "watchdog>=4.0.0",
48
+ "waveforms>=1.9.4"
45
49
  ]
46
50
  dynamic = ["version"]
47
51
 
48
52
  [project.optional-dependencies]
49
53
  full = [
50
- "SQLAlchemy>=2.0.19",
51
54
  "uvloop>=0.19.0"
52
55
  ]
53
56
 
@@ -1,5 +1,6 @@
1
1
  import click
2
2
 
3
+ from .executor.__main__ import create, maintain, run
3
4
  from .monitor.__main__ import main as monitor
4
5
  from .scan.server import server
5
6
  from .sys.net.cli import dht
@@ -7,20 +8,23 @@ from .visualization.__main__ import plot
7
8
 
8
9
 
9
10
  @click.group()
10
- def main():
11
+ def cli():
11
12
  pass
12
13
 
13
14
 
14
- @main.command()
15
+ @cli.command()
15
16
  def hello():
16
17
  """Print hello world."""
17
18
  click.echo('hello, world')
18
19
 
19
20
 
20
- main.add_command(monitor)
21
- main.add_command(plot)
22
- main.add_command(dht)
23
- main.add_command(server)
21
+ cli.add_command(monitor)
22
+ cli.add_command(plot)
23
+ cli.add_command(dht)
24
+ cli.add_command(server)
25
+ cli.add_command(maintain)
26
+ cli.add_command(run)
27
+ cli.add_command(create)
24
28
 
25
29
  if __name__ == '__main__':
26
- main()
30
+ cli()
@@ -0,0 +1,511 @@
1
+ import copy
2
+ import math
3
+ import operator
4
+ import pickle
5
+ import sys
6
+ from typing import Any, Generator
7
+
8
+
9
+ class Singleton(type):
10
+ _instances = {}
11
+
12
+ def __call__(cls, *args, **kwargs):
13
+ if cls not in cls._instances:
14
+ cls._instances[cls] = super(Singleton,
15
+ cls).__call__(*args, **kwargs)
16
+ return cls._instances[cls]
17
+
18
+
19
+ class _SELF(metaclass=Singleton):
20
+ __slots__ = ()
21
+
22
+ def __repr__(self):
23
+ return "self"
24
+
25
+
26
+ class _NOTSET(metaclass=Singleton):
27
+ __slots__ = ()
28
+
29
+ def __repr__(self):
30
+ return 'N/A'
31
+
32
+
33
+ class _UNKNOW(metaclass=Singleton):
34
+ __slots__ = ()
35
+
36
+ def __repr__(self) -> str:
37
+ return "Unknow"
38
+
39
+
40
+ class _DELETE(metaclass=Singleton):
41
+ __slots__ = ()
42
+
43
+ def __repr__(self):
44
+ return 'Delete'
45
+
46
+
47
+ SELF = _SELF()
48
+ NOTSET = _NOTSET()
49
+ UNKNOW = _UNKNOW()
50
+ DELETE = _DELETE()
51
+
52
+
53
+ def flattenDictIter(d: dict,
54
+ prefix: list = []
55
+ ) -> Generator[tuple[str, Any], None, None]:
56
+ for k in d:
57
+ if isinstance(d[k], dict):
58
+ yield from flattenDictIter(d[k], prefix=[*prefix, k])
59
+ else:
60
+ yield '.'.join(prefix + [k]), d[k]
61
+
62
+
63
+ def flattenDict(d: dict[str, Any]) -> dict[str, Any]:
64
+ return {k: v for k, v in flattenDictIter(d)}
65
+
66
+
67
+ def foldDict(d: dict[str, Any]) -> dict[str, Any]:
68
+ ret = {}
69
+
70
+ for k, v in d.items():
71
+ keys = k.split('.')
72
+ d = ret
73
+ parent = None
74
+
75
+ for key in keys[:-1]:
76
+ if not isinstance(d, dict):
77
+ parent[0][parent[1]] = {SELF: d}
78
+ d = parent[0][parent[1]]
79
+ if key not in d:
80
+ d[key] = dict()
81
+ parent = d, key
82
+ d = d[key]
83
+ if not isinstance(d, dict):
84
+ parent[0][parent[1]] = {SELF: d}
85
+ d = parent[0][parent[1]]
86
+ if keys[-1] in d and isinstance(d[keys[-1]], dict):
87
+ d[keys[-1]][SELF] = v
88
+ else:
89
+ d[keys[-1]] = v
90
+ return ret
91
+
92
+
93
+ class Update():
94
+ __slots__ = ('o', 'n')
95
+
96
+ def __init__(self, o, n):
97
+ self.o = o
98
+ self.n = n
99
+
100
+ def __repr__(self):
101
+ return f"Update: {self.o!r} ==> {self.n!r}"
102
+
103
+ def __eq__(self, o: object) -> bool:
104
+ return isinstance(o, Update) and self.n == o.n
105
+
106
+
107
+ class Create():
108
+ __slots__ = ('n', 'replace')
109
+
110
+ def __init__(self, n, replace=False):
111
+ """
112
+ Create a new node
113
+
114
+ :param n: the new node
115
+ :param replace: if True, replace the node if it already exists
116
+ """
117
+ self.n = n
118
+ self.replace = replace
119
+
120
+ def __repr__(self):
121
+ if self.replace:
122
+ return f"Replace: {self.n!r}"
123
+ else:
124
+ return f"Create: {self.n!r}"
125
+
126
+ def __eq__(self, o: object) -> bool:
127
+ return isinstance(
128
+ o, Create) and self.n == o.n and self.replace == o.replace
129
+
130
+
131
+ def _eq(a, b):
132
+ import numpy as np
133
+
134
+ if isinstance(a, np.ndarray):
135
+ return np.array_equal(a, np.asarray(b))
136
+
137
+ try:
138
+ return a == b
139
+ except:
140
+ pass
141
+ if isinstance(a, (list, tuple)):
142
+ return len(a) == len(b) and all(_eq(a[i], b[i]) for i in range(len(a)))
143
+ if isinstance(a, dict):
144
+ return set(a.keys()) == set(b.keys()) and all(
145
+ _eq(a[k], b[k]) for k in a)
146
+
147
+ try:
148
+ return pickle.dumps(a) == pickle.dumps(b)
149
+ except:
150
+ return False
151
+
152
+
153
+ def diff(d1: dict, d2: dict) -> dict:
154
+ """
155
+ Compute the difference between two dictionaries
156
+
157
+ Args:
158
+ d1: the original dictionary
159
+ d2: the new dictionary
160
+
161
+ Returns:
162
+ a dictionary containing the difference between d1 and d2
163
+ """
164
+ ret = {}
165
+ for k in d2:
166
+ if k in d1:
167
+ if isinstance(d2[k], type(d1[k])) and _eq(d1[k], d2[k]):
168
+ pass
169
+ elif isinstance(d1[k], dict) and isinstance(d2[k], dict):
170
+ ret[k] = diff(d1[k], d2[k])
171
+ else:
172
+ ret[k] = Update(d1[k], d2[k])
173
+ else:
174
+ ret[k] = Create(d2[k])
175
+ for k in d1:
176
+ if k not in d2:
177
+ ret[k] = DELETE
178
+ return ret
179
+
180
+
181
+ def patch(source, diff, in_place=False):
182
+ """
183
+ Patch a dictionary with a diff
184
+
185
+ Args:
186
+ source: the original dictionary
187
+ diff: the diff
188
+ in_place: if True, patch the source dictionary in place
189
+
190
+ Returns:
191
+ the patched dictionary
192
+ """
193
+ if in_place:
194
+ ret = source
195
+ else:
196
+ ret = copy.copy(source)
197
+ for k, v in diff.items():
198
+ if isinstance(v, dict):
199
+ ret[k] = patch(source[k], v, in_place=in_place)
200
+ else:
201
+ if isinstance(v, Update):
202
+ ret[k] = v.n
203
+ elif isinstance(v, Create):
204
+ if v.replace or k not in ret:
205
+ ret[k] = v.n
206
+ else:
207
+ update_tree(ret[k], v.n)
208
+ elif v is DELETE:
209
+ del ret[k]
210
+ else:
211
+ raise ValueError(f"Unsupported patch: {v!r}")
212
+ return ret
213
+
214
+
215
+ def merge(diff1, diff2, origin=None):
216
+ """
217
+ Merge two diffs
218
+
219
+ Args:
220
+ diff1: the first diff
221
+ diff2: the second diff
222
+ origin: the original dictionary
223
+
224
+ Returns:
225
+ the merged diff
226
+ """
227
+ if origin is not None:
228
+ updated = patch(patch(origin, diff1), diff2)
229
+ return diff(origin, updated)
230
+
231
+ ret = {}
232
+ for k, v in diff1.items():
233
+ if k in diff2:
234
+ v2 = diff2[k]
235
+ if isinstance(v, dict) and isinstance(v2, dict):
236
+ d = merge(v, v2)
237
+ if d:
238
+ ret[k] = d
239
+ else:
240
+ if isinstance(v, Update) and isinstance(v2, Update):
241
+ ret[k] = Update(v.o, v2.n)
242
+ elif isinstance(v, Create) and isinstance(v2, dict):
243
+ ret[k] = Create(patch(copy.copy(v.n), v2, True))
244
+ elif isinstance(v, Create) and isinstance(v2, Update):
245
+ ret[k] = Create(v2.n)
246
+ elif isinstance(v, Create) and v2 is DELETE:
247
+ pass
248
+ elif v2 is DELETE:
249
+ ret[k] = DELETE
250
+ elif v is DELETE and isinstance(v2, Create):
251
+ if isinstance(v2.n, dict):
252
+ ret[k] = Create(v2.n, replace=True)
253
+ else:
254
+ ret[k] = Update(UNKNOW, v2.n)
255
+ elif isinstance(v2, Create) and v2.replace:
256
+ ret[k] = v2
257
+ else:
258
+ raise ValueError(f"Unsupported merge: {v!r} {v2!r}")
259
+ else:
260
+ ret[k] = v
261
+ for k, v in diff2.items():
262
+ if k not in diff1:
263
+ ret[k] = v
264
+ return ret
265
+
266
+
267
+ def print_diff(d, limit=None, offset=0, file=sys.stdout, ignores=None):
268
+ """
269
+ Print a diff
270
+
271
+ Args:
272
+ d: the diff
273
+ limit: the maximum number of lines to print
274
+ offset: the offset of the first line
275
+ file: the file to print to
276
+ ignores: ignore keys starting with this prefix
277
+ """
278
+ count = 0
279
+ for i, (k, v) in enumerate(flattenDictIter(d)):
280
+ if count >= offset:
281
+ if ignores is not None and k.startswith(ignores):
282
+ continue
283
+ print(f"{k:40}", v, file=file)
284
+ count += 1
285
+ if limit is not None and count >= limit:
286
+ break
287
+
288
+
289
+ def update_tree(result, updates):
290
+ for k, v in updates.items():
291
+ if isinstance(v, dict):
292
+ if k not in result or not isinstance(result[k], dict):
293
+ result[k] = {}
294
+ update_tree(result[k], v)
295
+ else:
296
+ result[k] = v
297
+ return result
298
+
299
+
300
+ def queryref_tree(q, keys, dct, prefix=[], chain=None):
301
+ q = q[1:]
302
+ if q.startswith('.'):
303
+ while q.startswith('.'):
304
+ keys.pop()
305
+ q = q[1:]
306
+ q = '.'.join(keys + [q])
307
+
308
+ if chain is None:
309
+ chain = [q]
310
+ elif q in chain:
311
+ raise KeyError(f'Circular reference: {chain+[q]}')
312
+
313
+ return query_tree(q, dct, prefix=prefix, eval=True, chain=chain + [q])
314
+
315
+
316
+ def query_tree(q, dct, prefix=[], eval=True, chain=None):
317
+ ret = dct
318
+ keys = q.split('.')
319
+ for i, key in enumerate(keys):
320
+ if key not in ret:
321
+ return (NOTSET, '.'.join(prefix + keys[:i + 1]))
322
+ ret = ret[key]
323
+ if isinstance(ret, str) and eval:
324
+ if ret.startswith('$'):
325
+ return queryref_tree(ret, keys, dct, prefix=prefix, chain=chain)
326
+ elif ret.startswith('&'):
327
+ return eval_expr(ret[1:], Env(dct,
328
+ keys,
329
+ prefix=prefix,
330
+ chain=chain))
331
+ return ret
332
+
333
+
334
+ class Env():
335
+
336
+ def __init__(self, dct=None, keys=None, prefix=[], chain=None):
337
+ self.dct = dct if dct is not None else {}
338
+ self.keys = keys if keys is not None else []
339
+ self.chain = chain
340
+ self.prefix = prefix
341
+
342
+ def get(self, name):
343
+ return queryref_tree(name[1:],
344
+ self.keys,
345
+ self.dct,
346
+ prefix=self.prefix,
347
+ chain=self.chain)
348
+
349
+
350
+ internal_functions = {
351
+ '**': operator.pow,
352
+ '+': operator.add,
353
+ '-': operator.sub,
354
+ '*': operator.mul,
355
+ '/': operator.truediv,
356
+ '//': operator.floordiv,
357
+ '%': operator.mod,
358
+ '&': operator.and_,
359
+ '|': operator.or_,
360
+ '^': operator.xor,
361
+ '~': operator.invert,
362
+ '<<': operator.lshift,
363
+ '>>': operator.rshift,
364
+ 'getitem': operator.getitem,
365
+ 'contains': operator.contains,
366
+ 'not': operator.not_,
367
+ 'getattr': getattr,
368
+ 'abs': abs,
369
+ 'min': min,
370
+ 'max': max,
371
+ 'sum': sum,
372
+ 'len': len,
373
+ 'any': any,
374
+ 'all': all,
375
+ 'ord': ord,
376
+ 'chr': chr,
377
+ 'bin': bin,
378
+ 'oct': oct,
379
+ 'hex': hex,
380
+ 'int': int,
381
+ 'round': round,
382
+ 'real': lambda x: x.real,
383
+ 'imag': lambda x: x.imag,
384
+ 'sqrt': math.sqrt,
385
+ 'sin': math.sin,
386
+ 'cos': math.cos,
387
+ 'tan': math.tan,
388
+ 'asin': math.asin,
389
+ 'acos': math.acos,
390
+ 'atan': math.atan,
391
+ 'atan2': math.atan2,
392
+ 'sinh': math.sinh,
393
+ 'cosh': math.cosh,
394
+ 'tanh': math.tanh,
395
+ 'asinh': math.asinh,
396
+ 'acosh': math.acosh,
397
+ 'atanh': math.atanh,
398
+ 'ceil': math.ceil,
399
+ 'floor': math.floor,
400
+ 'trunc': math.trunc,
401
+ 'log10': math.log10,
402
+ 'log': math.log,
403
+ 'exp': math.exp,
404
+ }
405
+
406
+
407
+ def eval_expr(expression, env=None, functions=None):
408
+ from pyparsing import (Forward, Literal, ParseResults, Suppress, Word,
409
+ alphanums, delimitedList, infixNotation, oneOf,
410
+ opAssoc, pyparsing_common)
411
+ if functions is None:
412
+ functions = internal_functions
413
+
414
+ def lookup(name):
415
+ return env.get(name)
416
+
417
+ def apply(fun, *args):
418
+ return functions[fun](*args)
419
+
420
+ def eval_expr(t):
421
+ if not isinstance(t, ParseResults):
422
+ return t
423
+ if t[0] == '-' and len(t) == 2:
424
+ return -t[1]
425
+ return apply(t[1], t[0], t[2])
426
+
427
+ # Define pyparsing grammar
428
+ expr = Forward()
429
+
430
+ complex_ = (pyparsing_common.number +
431
+ 'j').setParseAction(lambda s, l, t: complex(t[0]))
432
+ number = pyparsing_common.number | complex_
433
+
434
+ const = oneOf('pi e').setParseAction(lambda s, l, t: {
435
+ 'pi': math.pi,
436
+ 'e': math.e
437
+ }.get(t[0]))
438
+
439
+ variable = Word("$", alphanums +
440
+ "._").setParseAction(lambda s, l, t: lookup(t[0]))
441
+
442
+ bracket = Suppress('(') + expr + Suppress(')')
443
+ bracket.setParseAction(lambda s, l, t: t[0])
444
+
445
+ operand = const | variable | number | bracket
446
+
447
+ func = pyparsing_common.identifier
448
+ func_call = func + Suppress("(") + delimitedList(expr) + Suppress(")")
449
+ func_call.setParseAction(lambda s, l, t: apply(t[0], *t[1:]))
450
+
451
+ term = operand | func_call
452
+
453
+ expr << infixNotation(term, [
454
+ (Literal('**'), 2, opAssoc.RIGHT),
455
+ (Literal('~'), 1, opAssoc.RIGHT),
456
+ (Literal('-'), 1, opAssoc.RIGHT),
457
+ (oneOf('* / // %'), 2, opAssoc.LEFT),
458
+ (oneOf('+ -'), 2, opAssoc.LEFT),
459
+ (oneOf('>> <<'), 2, opAssoc.LEFT),
460
+ (Literal('&'), 2, opAssoc.LEFT),
461
+ (Literal('^'), 2, opAssoc.LEFT),
462
+ (Literal('|'), 2, opAssoc.LEFT),
463
+ ])
464
+
465
+ expr.setParseAction(lambda s, l, t: eval_expr(t[0]))
466
+
467
+ parsed = expr.parseString(expression, parseAll=True)
468
+ return parsed[0]
469
+
470
+
471
+ def sorted_tree(dct, *, keys=None):
472
+ if keys is None or callable(keys):
473
+ key = keys
474
+ elif isinstance(keys, list):
475
+ key = keys[0]
476
+ if len(keys) > 1:
477
+ keys = keys[1:]
478
+ else:
479
+ keys = None
480
+ elif (isinstance(keys, tuple) and len(keys) == 2
481
+ and isinstance(keys[1], dict)):
482
+ key = keys[0]
483
+ keys = keys[1]
484
+ else:
485
+ raise Exception(f"Unsupported keys: {keys!r}")
486
+
487
+ if isinstance(dct, dict):
488
+ if isinstance(keys, dict):
489
+ default = keys.get('default', None)
490
+ return {
491
+ k: sorted_tree(dct[k], keys=keys.get(k, default))
492
+ for k in sorted(dct.keys(), key=key)
493
+ }
494
+ else:
495
+ return {
496
+ k: sorted_tree(dct[k], keys=keys)
497
+ for k in sorted(dct.keys(), key=key)
498
+ }
499
+ elif isinstance(dct, set):
500
+ if isinstance(keys, dict):
501
+ default = keys.get('default', None)
502
+ return set([
503
+ sorted_tree(v, keys=keys.get(v, default))
504
+ for v in sorted(list(dct), key=key)
505
+ ])
506
+ else:
507
+ return set([
508
+ sorted_tree(v, keys=keys) for v in sorted(list(dct), key=key)
509
+ ])
510
+ else:
511
+ return dct
@@ -0,0 +1,5 @@
1
+ from loguru import logger
2
+
3
+ from .schedule import maintain, run
4
+
5
+ # logger.configure(handlers=[])