|
1 | 1 | from typing import Optional |
2 | 2 | from decimal import Decimal |
| 3 | +from collections import UserDict |
3 | 4 |
|
4 | | -from validator_collection import validators |
| 5 | +from validator_collection import validators, checkers |
5 | 6 |
|
6 | | -from highcharts_core import utility_functions |
| 7 | +from highcharts_core import utility_functions, constants |
7 | 8 | from highcharts_core.options.series.data.base import DataBase |
8 | 9 | from highcharts_core.utility_classes.gradients import Gradient |
9 | 10 | from highcharts_core.utility_classes.patterns import Pattern |
@@ -84,6 +85,47 @@ def weight(self) -> Optional[int | float | Decimal]: |
84 | 85 | def weight(self, value): |
85 | 86 | self._weight = validators.numeric(value, allow_empty = True) |
86 | 87 |
|
| 88 | + @classmethod |
| 89 | + def from_array(cls, value): |
| 90 | + if not value: |
| 91 | + return [] |
| 92 | + elif checkers.is_string(value): |
| 93 | + try: |
| 94 | + value = validators.json(value) |
| 95 | + except (ValueError, TypeError): |
| 96 | + pass |
| 97 | + elif not checkers.is_iterable(value): |
| 98 | + value = [value] |
| 99 | + |
| 100 | + collection = [] |
| 101 | + for item in value: |
| 102 | + if checkers.is_type(item, 'SinglePointData'): |
| 103 | + as_obj = item |
| 104 | + elif checkers.is_dict(item): |
| 105 | + as_obj = cls.from_dict(item) |
| 106 | + elif item is None or isinstance(item, constants.EnforcedNullType): |
| 107 | + as_obj = cls(y = None) |
| 108 | + elif checkers.is_numeric(item): |
| 109 | + as_obj = cls(y = item) |
| 110 | + elif checkers.is_iterable(item, forbid_literals = (str, bytes, dict, UserDict)): |
| 111 | + if len(item) == 3: |
| 112 | + as_obj = cls(from_ = item[0], |
| 113 | + to = item[1], |
| 114 | + weight = item[2]) |
| 115 | + else: |
| 116 | + raise errors.HighchartsValueError(f'each data point supplied must either ' |
| 117 | + f'be an Arc Diagram Data Point or be ' |
| 118 | + f'coercable to one. Could not coerce: ' |
| 119 | + f'{item}') |
| 120 | + else: |
| 121 | + raise errors.HighchartsValueError(f'each data point supplied must either ' |
| 122 | + f'be an Arc Diagram Data Point or be ' |
| 123 | + f'coercable to one. Could not coerce: ' |
| 124 | + f'{item}') |
| 125 | + collection.append(as_obj) |
| 126 | + |
| 127 | + return collection |
| 128 | + |
87 | 129 | @classmethod |
88 | 130 | def _get_kwargs_from_dict(cls, as_dict): |
89 | 131 | """Convenience method which returns the keyword arguments used to initialize the |
|
0 commit comments