Skip to content

Commit 0e64792

Browse files
authored
Merge pull request #26 from highcharts-for-python/develop
PR for v.1.3.0
2 parents 4176e85 + e3b2f75 commit 0e64792

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+420
-222
lines changed

CHANGES.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
Release 1.3.0
2+
=========================================
3+
4+
* **ENHANCEMENT:** Modified the way that data points are serialized to JavaScript literal objects. Now, they are serialized to a JavaScript array if their configured properties are those that Highcharts (JS) supports in JavaScript array notation. Otherwise, the code falls back to serialize the data point as a JavaScript object literal. This change is intended to improve performance and reduce the size of the serialized data. (#25)
5+
* **ENHANCEMENT:** Added ``__repr__()`` method for Highcharts Core for Python classes.
6+
* **ENHANCEMENT:** Added ``__str__()`` method with special handling for difficult-to-read classes.
7+
* **ENHANCEMENT:** Added ``Chart.get_script_tags()`` to retrieve Javascript ``<script>`` tags.
8+
* **ENHANCEMENT:** Added ``utility_functions.to_snake_case()`` function.
9+
* **BUGFIX:** Fixed incorrect serialization of datetime and Pandas ``Timestamp`` objects in ``.to_dict()`` and ``.to_json()``.
10+
* **BUGFIX:** Fixed incorrect serialization of ``EnforcedNull`` in ``.to_dict()`` and ``.to_json()``.
11+
12+
13+
---------------------
14+
115
Release 1.2.0
216
=========================================
317

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Before you install, please be aware of the following "hard" dependencies:
8585
* Python 3.10 or higher
8686
* Highcharts Maps (JS) v.10.2 or higher (not technically a Python dependency, but
8787
it won't work with earlier versions of Highcharts)
88-
* `Highcharts Core for Python <https://core-docs.highchartspython.com/en/latest/>`__ v.1.0 or higher
88+
* `Highcharts Core for Python <https://core-docs.highchartspython.com/en/latest/>`__ v.1.3 or higher
8989
* `esprima-python <https://github.com/Kronuz/esprima-python>`__ v.4.0 or higher
9090
* `requests <https://requests.readthedocs.io/en/latest/>`__ v.2.31 or higher
9191
* `validator-collection <https://validator-collection.readthedocs.io/en/latest/>`__

docs/_dependencies.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
Not technically a Python dependency, but obviously **Highcharts Maps for Python**
3333
will not work properly if your rendering layer does not leverage Highcharts Maps.
3434

35-
* `highcharts-core <https://core-docs.highchartspython.com>`_ v.1.0.0 or higher
35+
* `highcharts-core <https://core-docs.highchartspython.com>`_ v.1.3.0 or higher
3636
* `esprima-python <https://github.com/Kronuz/esprima-python>`_ v.4.0 or higher
3737
* `requests <https://requests.readthedocs.io/en/latest/>`_ v.2.31 or higher
3838
* `validator-collection <https://validator-collection.readthedocs.io/en/latest/>`_

highcharts_maps/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.2.0'
1+
__version__ = '1.3.0'

highcharts_maps/options/series/data/connections.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,17 @@ def from_array(cls, value):
322322

323323
return collection
324324

325+
def _get_props_from_array(self) -> List[str]:
326+
"""Returns a list of the property names that can be set using the
327+
:meth:`.from_array() <highcharts_core.options.series.data.base.DataBase.from_array>`
328+
method.
329+
330+
:rtype: :class:`list <python:list>` of :class:`str <python:str>`
331+
"""
332+
return ['from_',
333+
'to',
334+
'weight']
335+
325336
@classmethod
326337
def _get_kwargs_from_dict(cls, as_dict):
327338
"""Convenience method which returns the keyword arguments used to initialize the

highcharts_maps/options/series/data/geometric.py

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, List
1+
from typing import Optional, List, Dict
22
from decimal import Decimal
33

44
from validator_collection import validators, checkers
@@ -305,6 +305,62 @@ def from_array(cls, value):
305305

306306
return collection
307307

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+
308364
@classmethod
309365
def _get_kwargs_from_dict(cls, as_dict):
310366
"""Convenience method which returns the keyword arguments used to initialize the
@@ -437,6 +493,15 @@ def from_array(cls, value):
437493

438494
return collection
439495

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+
440505
@classmethod
441506
def _get_kwargs_from_dict(cls, as_dict):
442507
"""Convenience method which returns the keyword arguments used to initialize the
@@ -631,6 +696,15 @@ def from_array(cls, value):
631696

632697
return collection
633698

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+
634708
@classmethod
635709
def _get_kwargs_from_dict(cls, as_dict):
636710
"""Convenience method which returns the keyword arguments used to initialize the

highcharts_maps/options/series/data/map_data.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,42 @@ def __init__(self, **kwargs):
3636
self.force_geojson = kwargs.get('force_geojsons', None)
3737
self.topology = kwargs.get('topology', None)
3838

39+
def __str__(self):
40+
"""Return a human-readable :class:`str <python:str>` representation of the map
41+
data.
42+
43+
.. warning::
44+
45+
To ensure that the result is human-readable, the result will be rendered
46+
*without* its
47+
:meth:`.topology <highcharts_maps.options.series.data.map_data.MapData.topology>`
48+
property.
49+
50+
.. tip::
51+
52+
If you would like a *complete* and *unambiguous* :class:`str <python:str>`
53+
representation, then you can:
54+
55+
* use the
56+
:meth:`__repr__() <highcharts_maps.options.series.data.map_data.MapData.__repr__>` method,
57+
* call ``repr(my_map_data)``, or
58+
* serialize the chart to TopoJSON using
59+
:meth:`.to_topojson() <highcharts_maps.options.series.data.map_data.MapData.to_topojson>`
60+
* serialize the chart to GeoJSON using
61+
:meth:`.to_geojson() <highcharts_maps.options.series.data.map_data.MapData.to_geojson>`
62+
63+
:returns: A :class:`str <python:str>` representation of the map data.
64+
:rtype: :class:`str <python:str>`
65+
"""
66+
as_dict = self.to_dict()
67+
68+
kwargs = {utility_functions.to_snake_case(key): as_dict[key]
69+
for key in as_dict if key not in ['topology']}
70+
kwargs_as_str = ', '.join([f'{key} = {repr(kwargs[key])}'
71+
for key in kwargs])
72+
73+
return f'{self.__class__.__name__}({kwargs_as_str})'
74+
3975
@property
4076
def force_geojson(self) -> Optional[bool]:
4177
"""If ``True``, will serialize as :term:`GeoJSON`. If ``False``, will serialize

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ classifiers = [
5858

5959
requires-python = ">= 3.10"
6060
dependencies = [
61-
"highcharts-core>=1.0.0-rc1",
61+
"highcharts-core>=1.3.0",
6262
"esprima>=4.0.1",
6363
"validator-collection>=1.5.0",
6464
"requests>=2.31.0",

requirements.dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ requests==2.31.0
1818
tox==4.0.0
1919
urllib3==1.26.9
2020
validator-collection==1.5.0
21-
highcharts-core>=1.0.0-rc1
21+
highcharts-core>=1.3.0

requirements.travis.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ requests==2.31.0
1818
tox==4.0.0
1919
urllib3==1.26.9
2020
validator-collection==1.5.0
21-
highcharts-core>=1.0.0-rc1
21+
highcharts-core>=1.3.0

0 commit comments

Comments
 (0)