|
1 | | -from typing import Optional, List |
| 1 | +from typing import Optional, List, Dict |
2 | 2 | from decimal import Decimal |
3 | 3 |
|
4 | 4 | from validator_collection import validators, checkers |
@@ -305,6 +305,62 @@ def from_array(cls, value): |
305 | 305 |
|
306 | 306 | return collection |
307 | 307 |
|
| 308 | + def _get_props_from_array(self) -> List[str]: |
| 309 | + """Returns a list of the property names that can be set using the |
| 310 | + :meth:`.from_array() <highcharts_maps.options.series.data.geometric.GeometricData.from_array>` |
| 311 | + method. |
| 312 | + |
| 313 | + :rtype: :class:`list <python:list>` of :class:`str <python:str>` |
| 314 | + """ |
| 315 | + return ['name', 'value'] |
| 316 | + |
| 317 | + @property |
| 318 | + def requires_js_object(self) -> bool: |
| 319 | + """Indicates whether or not the data point *must* be serialized to a JS literal |
| 320 | + object or whether it can be serialized to a primitive array. |
| 321 | + |
| 322 | + :returns: ``True`` if the data point *must* be serialized to a JS literal object. |
| 323 | + ``False`` if it can be serialized to an array. |
| 324 | + :rtype: :class:`bool <python:bool>` |
| 325 | + """ |
| 326 | + from_array_props = [utility_functions.to_camelCase(x) |
| 327 | + for x in self._get_props_from_array()] |
| 328 | + |
| 329 | + as_dict = self.to_dict() |
| 330 | + trimmed_dict = self.trim_dict(as_dict) |
| 331 | + for prop in from_array_props: |
| 332 | + if prop in trimmed_dict: |
| 333 | + del trimmed_dict[prop] |
| 334 | + |
| 335 | + if trimmed_dict: |
| 336 | + return True |
| 337 | + |
| 338 | + return False |
| 339 | + |
| 340 | + def to_array(self, force_object = False) -> List | Dict: |
| 341 | + """Generate the array representation of the data point (the inversion |
| 342 | + of |
| 343 | + :meth:`.from_array() <highcharts_maps.options.series.data.geometric.GeometricData.from_array>`). |
| 344 | + |
| 345 | + .. warning:: |
| 346 | + |
| 347 | + If the data point *cannot* be serialized to a JavaScript array, |
| 348 | + this method will instead return the untrimmed :class:`dict <python:dict>` |
| 349 | + representation of the data point as a fallback. |
| 350 | +
|
| 351 | + :param force_object: if ``True``, forces the return of the instance's |
| 352 | + untrimmed :class:`dict <python:dict>` representation. Defaults to ``False``. |
| 353 | + :type force_object: :class:`bool <python:bool>` |
| 354 | +
|
| 355 | + :returns: The array representation of the data point. |
| 356 | + :rtype: :class:`list <python:list>` of values or :class:`dict <python:dict>` |
| 357 | + """ |
| 358 | + if self.requires_js_object or force_object: |
| 359 | + return self._to_untrimmed_dict() |
| 360 | + |
| 361 | + return [getattr(self, x, constants.EnforcedNull) |
| 362 | + for x in self._get_props_from_array()] |
| 363 | + |
308 | 364 | @classmethod |
309 | 365 | def _get_kwargs_from_dict(cls, as_dict): |
310 | 366 | """Convenience method which returns the keyword arguments used to initialize the |
@@ -437,6 +493,15 @@ def from_array(cls, value): |
437 | 493 |
|
438 | 494 | return collection |
439 | 495 |
|
| 496 | + def _get_props_from_array(self) -> List[str]: |
| 497 | + """Returns a list of the property names that can be set using the |
| 498 | + :meth:`.from_array() <highcharts_maps.options.series.data.geometric.GeometricData.from_array>` |
| 499 | + method. |
| 500 | + |
| 501 | + :rtype: :class:`list <python:list>` of :class:`str <python:str>` |
| 502 | + """ |
| 503 | + return ['z'] |
| 504 | + |
440 | 505 | @classmethod |
441 | 506 | def _get_kwargs_from_dict(cls, as_dict): |
442 | 507 | """Convenience method which returns the keyword arguments used to initialize the |
@@ -631,6 +696,15 @@ def from_array(cls, value): |
631 | 696 |
|
632 | 697 | return collection |
633 | 698 |
|
| 699 | + def _get_props_from_array(self) -> List[str]: |
| 700 | + """Returns a list of the property names that can be set using the |
| 701 | + :meth:`.from_array() <highcharts_maps.options.series.data.geometric.GeometricData.from_array>` |
| 702 | + method. |
| 703 | + |
| 704 | + :rtype: :class:`list <python:list>` of :class:`str <python:str>` |
| 705 | + """ |
| 706 | + return ['name', 'y'] |
| 707 | + |
634 | 708 | @classmethod |
635 | 709 | def _get_kwargs_from_dict(cls, as_dict): |
636 | 710 | """Convenience method which returns the keyword arguments used to initialize the |
|
0 commit comments