Skip to content

Commit eeb9d1a

Browse files
committed
can now create a spec usable by mosaic_widget
1 parent 1e3b019 commit eeb9d1a

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

packages/schema_wrapper/src/utils.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ def get_valid_identifier(
102102
valid += "_"
103103
return valid
104104

105+
def revert_validation(field):
106+
return field.strip('_')
107+
105108
def get_key_by_value(dictionary, target_value):
106109
for key, value in dictionary.items():
107110
if value == target_value:
@@ -126,12 +129,16 @@ def get_dependencies(data) -> List[str]:
126129

127130
def to_dict(cur_object):
128131
if isinstance(cur_object, dict):
129-
return {k: to_dict(v) for k, v in cur_object.items()}
132+
return {revert_validation(k): to_dict(v) for k, v in cur_object.items() if v is not None}
130133
elif isinstance(cur_object, list):
131-
return [to_dict(i) for i in cur_object]
134+
return [to_dict(i) for i in cur_object if i is not None]
132135
elif hasattr(cur_object, '__dict__'):
133-
return {k: to_dict(v) for k, v in cur_object.__dict__.items()}
134-
elif isinstance(cur_object, (str, int, float, bool, type(None))):
136+
obj_asdict = cur_object.__dict__
137+
if len(obj_asdict.keys()) == 1:
138+
return to_dict(list(obj_asdict.values())[0])
139+
else:
140+
return {revert_validation(k): to_dict(v) for k, v in obj_asdict.items() if v is not None}
141+
elif isinstance(cur_object, (str, int, float, bool)):
135142
return cur_object
136-
else:
143+
elif cur_object != None:
137144
return str(cur_object)

packages/schema_wrapper/test/test_generated_classes.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
"""
22
pytest packages/schema_wrapper/test/test.py
33
"""
4-
54
import unittest
65
from pathlib import Path
76
import pytest
7+
from ..src.utils import to_dict
88

9-
from ..generated_classes import (
10-
AggregateExpression,
11-
AggregateTransform,
12-
ChannelValue,
13-
PlotMarkData,
14-
ChannelValueSpec,
15-
Argmin,
16-
Argmax,
17-
Avg,
18-
CSSStyles,
19-
Plot,
20-
BrushStyles,
21-
DataCSV,
22-
DataQuery,
23-
DataSpatial,
24-
DataTable
25-
)
9+
from ..generated_classes import *
2610

2711
@pytest.fixture
2812
def sample_data():
@@ -111,6 +95,7 @@ def test_plot_components():
11195
assert styles.opacity == 0.7
11296
assert styles.stroke == "black"
11397
assert styles.strokeOpacity == 0.3
98+
print(to_dict(styles))
11499

115100
def test_argmin_argmax():
116101
# Test Argmin
@@ -127,5 +112,25 @@ def test_argmin_argmax():
127112
assert argmax.distinct == True
128113
assert argmax.orderby == "date"
129114

115+
def test_composite():
116+
argmax = Argmax([5, 6, True, 0.5], False, TransformField("test"), [TransformField("1"), TransformField("2"), TransformField("3")], ParamRef())
117+
print(to_dict(argmax))
118+
119+
def test_plot():
120+
plot_spec = Plot(
121+
122+
plot = [PlotMark(Dot(mark="dot", data=PlotFrom(from_="weather", filterBy="$click"), x=ChannelValueSpec(ChannelValue({"dateMonthDay": "date"})), y=ChannelValueSpec(ChannelValue("temp_max")), fill="weather", r="precipitation"))],
123+
xyDomain = "Fixed",
124+
xTickFormat = "%b",
125+
colorDomain = "$domain",
126+
colorRange = "$colors",
127+
rDomain = "Fixed",
128+
rRange = [2, 10],
129+
width = 800
130+
)
131+
132+
print(to_dict(plot_spec))
133+
130134
if __name__ == '__main__':
131-
pytest.main([__file__])
135+
pytest.main([__file__])
136+

0 commit comments

Comments
 (0)