Exemple #1
0
    def test_named_tuple_to_dict(self):
        class Foo(NamedTuple):
            x: int
            y: str

        foo = Foo(x=5, y="g")
        self.assertEqual(named_tuple_to_dict(foo), {"x": 5, "y": "g"})

        bar = {"x": 5, "foo": foo, "y": [(1, True), foo]}
        self.assertEqual(
            named_tuple_to_dict(bar),
            {"x": 5, "foo": {"x": 5, "y": "g"}, "y": [(1, True), {"x": 5, "y": "g"}]},
        )
Exemple #2
0
 def __new__(cls, data: Dict[str, Any],
             plot_type: enum.Enum) -> "AxPlotConfig":
     # Convert data to json-encodable form (strips out NamedTuple and numpy
     # array). This is a lossy conversion.
     dict_data = json.loads(
         json.dumps(named_tuple_to_dict(data), cls=utils.PlotlyJSONEncoder))
     # pyre-fixme[7]: Expected `AxPlotConfig` but got `NamedTuple`.
     return super(AxPlotConfig, cls).__new__(cls, dict_data, plot_type)