|
1 |
| -from typing import Optional |
| 1 | +from typing import Optional, List, Dict |
2 | 2 | from decimal import Decimal
|
3 | 3 |
|
4 | 4 | from validator_collection import validators, checkers
|
@@ -237,10 +237,71 @@ def from_array(cls, value):
|
237 | 237 | f'be a DataBase Data Point or be '
|
238 | 238 | f'coercable to one. Could not coerce: '
|
239 | 239 | f'{item}')
|
| 240 | + if checkers.is_string(as_obj.x) and not as_obj.name: |
| 241 | + as_obj.name = as_obj.x |
| 242 | + as_obj.x = None |
| 243 | + |
240 | 244 | collection.append(as_obj)
|
241 | 245 |
|
242 | 246 | return collection
|
243 | 247 |
|
| 248 | + def _get_props_from_array(self) -> List[str]: |
| 249 | + """Returns a list of the property names that can be set using the |
| 250 | + :meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>` |
| 251 | + method. |
| 252 | + |
| 253 | + :rtype: :class:`list <python:list>` of :class:`str <python:str>` |
| 254 | + """ |
| 255 | + return ['x', 'high', 'low', 'close', 'name'] |
| 256 | + |
| 257 | + def to_array(self, force_object = False) -> List | Dict: |
| 258 | + """Generate the array representation of the data point (the inversion |
| 259 | + of |
| 260 | + :meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`). |
| 261 | + |
| 262 | + .. warning:: |
| 263 | + |
| 264 | + If the data point *cannot* be serialized to a JavaScript array, |
| 265 | + this method will instead return the untrimmed :class:`dict <python:dict>` |
| 266 | + representation of the data point as a fallback. |
| 267 | +
|
| 268 | + :param force_object: if ``True``, forces the return of the instance's |
| 269 | + untrimmed :class:`dict <python:dict>` representation. Defaults to ``False``. |
| 270 | + :type force_object: :class:`bool <python:bool>` |
| 271 | +
|
| 272 | + :returns: The array representation of the data point. |
| 273 | + :rtype: :class:`list <python:list>` of values or :class:`dict <python:dict>` |
| 274 | + """ |
| 275 | + if self.requires_js_object or force_object: |
| 276 | + return self._to_untrimmed_dict() |
| 277 | + |
| 278 | + if self.x is not None: |
| 279 | + x = self.x |
| 280 | + elif self.name is not None: |
| 281 | + x = self.name |
| 282 | + else: |
| 283 | + x = constants.EnforcedNull |
| 284 | + |
| 285 | + if self.high is not None: |
| 286 | + high = self.high |
| 287 | + else: |
| 288 | + high = constants.EnforcedNull |
| 289 | + |
| 290 | + if self.low is not None: |
| 291 | + low = self.low |
| 292 | + else: |
| 293 | + low = constants.EnforcedNull |
| 294 | + |
| 295 | + if self.close is not None: |
| 296 | + close = self.close |
| 297 | + else: |
| 298 | + close = constants.EnforcedNull |
| 299 | + |
| 300 | + if self.x is None and self.name is None: |
| 301 | + return [high, low, close] |
| 302 | + |
| 303 | + return [x, high, low, close] |
| 304 | + |
244 | 305 |
|
245 | 306 | class OHLCData(HLCData):
|
246 | 307 | """Data point that can be visualized in a
|
@@ -371,6 +432,72 @@ def from_array(cls, value):
|
371 | 432 | f'be a DataBase Data Point or be '
|
372 | 433 | f'coercable to one. Could not coerce: '
|
373 | 434 | f'{item}')
|
| 435 | + if checkers.is_string(as_obj.x) and not as_obj.name: |
| 436 | + as_obj.name = as_obj.x |
| 437 | + as_obj.x = None |
| 438 | + |
374 | 439 | collection.append(as_obj)
|
375 | 440 |
|
376 | 441 | return collection
|
| 442 | + |
| 443 | + def _get_props_from_array(self) -> List[str]: |
| 444 | + """Returns a list of the property names that can be set using the |
| 445 | + :meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>` |
| 446 | + method. |
| 447 | + |
| 448 | + :rtype: :class:`list <python:list>` of :class:`str <python:str>` |
| 449 | + """ |
| 450 | + return ['x', 'open', 'high', 'low', 'close', 'name'] |
| 451 | + |
| 452 | + def to_array(self, force_object = False) -> List | Dict: |
| 453 | + """Generate the array representation of the data point (the inversion |
| 454 | + of |
| 455 | + :meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`). |
| 456 | + |
| 457 | + .. warning:: |
| 458 | + |
| 459 | + If the data point *cannot* be serialized to a JavaScript array, |
| 460 | + this method will instead return the untrimmed :class:`dict <python:dict>` |
| 461 | + representation of the data point as a fallback. |
| 462 | +
|
| 463 | + :param force_object: if ``True``, forces the return of the instance's |
| 464 | + untrimmed :class:`dict <python:dict>` representation. Defaults to ``False``. |
| 465 | + :type force_object: :class:`bool <python:bool>` |
| 466 | +
|
| 467 | + :returns: The array representation of the data point. |
| 468 | + :rtype: :class:`list <python:list>` of values or :class:`dict <python:dict>` |
| 469 | + """ |
| 470 | + if self.requires_js_object or force_object: |
| 471 | + return self._to_untrimmed_dict() |
| 472 | + |
| 473 | + if self.x is not None: |
| 474 | + x = self.x |
| 475 | + elif self.name is not None: |
| 476 | + x = self.name |
| 477 | + else: |
| 478 | + x = constants.EnforcedNull |
| 479 | + |
| 480 | + if self.open is not None: |
| 481 | + open_ = self.open |
| 482 | + else: |
| 483 | + open_ = constants.EnforcedNull |
| 484 | + |
| 485 | + if self.high is not None: |
| 486 | + high = self.high |
| 487 | + else: |
| 488 | + high = constants.EnforcedNull |
| 489 | + |
| 490 | + if self.low is not None: |
| 491 | + low = self.low |
| 492 | + else: |
| 493 | + low = constants.EnforcedNull |
| 494 | + |
| 495 | + if self.close is not None: |
| 496 | + close = self.close |
| 497 | + else: |
| 498 | + close = constants.EnforcedNull |
| 499 | + |
| 500 | + if self.x is None and self.name is None: |
| 501 | + return [open, high, low, close] |
| 502 | + |
| 503 | + return [x, open, high, low, close] |
0 commit comments