Skip to content

Commit 8e18139

Browse files
committed
Implemented Data.column_types property.
1 parent 31caf35 commit 8e18139

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

highcharts_core/constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,4 +953,12 @@ def __repr__(self):
953953

954954
EMPTY_STRING_CONTEXTS = [
955955
'Annotation.draggable',
956+
]
957+
958+
959+
DATA_COLUMN_TYPES = [
960+
'string',
961+
'number',
962+
'float',
963+
'date',
956964
]

highcharts_core/options/data.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self, **kwargs):
3131
self._before_parse = None
3232
self._columns = None
3333
self._columns_url = None
34+
self._column_types = None
3435
self._complete = None
3536
self._csv = None
3637
self._csv_url = None
@@ -59,6 +60,7 @@ def __init__(self, **kwargs):
5960
self.before_parse = kwargs.get('before_parse', None)
6061
self.columns = kwargs.get('columns', None)
6162
self.columns_url = kwargs.get('columns_url', None)
63+
self.column_types = kwargs.get('column_types', None)
6264
self.complete = kwargs.get('complete', None)
6365
self.csv = kwargs.get('csv', None)
6466
self.csv_url = kwargs.get('csv_url', None)
@@ -185,6 +187,46 @@ def columns_url(self, value):
185187
except ValueError:
186188
raise error
187189

190+
@property
191+
def column_types(self) -> Optional[List[str]]:
192+
"""The data types for each column in the related CSV data.
193+
194+
This property expects an iterable of :class:`str <python:str>` values,
195+
where each item in the iterable corresponds to a column in the underlying
196+
CSV data. The type specified for each column may be one of the following values:
197+
198+
* ``'string'``
199+
* ``'number'``
200+
* ``'float'``
201+
* ``'date'``
202+
203+
Defaults to :obj:`None <python:None>`.
204+
205+
:returns: The data types for each column in the related CSV data.
206+
:rtype: :class:`list <python:list>` of :class:`str <python:str>` or
207+
:obj:`None <python:None>`
208+
"""
209+
return self._column_types
210+
211+
@column_types.setter
212+
def column_types(self, value):
213+
if not value:
214+
self._column_types = None
215+
else:
216+
values = []
217+
value = validators.iterable(value,
218+
forbid_literals = (str, bytes))
219+
for item in value:
220+
item = str(item).lower()
221+
if item not in constants.DATA_COLUMN_TYPES:
222+
raise errors.HighchartsValueError(
223+
f'column_types expects one of {constants.DATA_COLUMN_TYPES}. '
224+
f'Received: {item}'
225+
)
226+
values.append(item)
227+
228+
self._column_types = values
229+
188230
@property
189231
def complete(self) -> Optional[CallbackFunction]:
190232
"""The JavaScript callback function that is evaluated when the data has finished
@@ -716,6 +758,7 @@ def _get_kwargs_from_dict(cls, as_dict):
716758
'before_parse': as_dict.get('beforeParse', None),
717759
'columns': as_dict.get('columns', None),
718760
'columns_url': as_dict.get('columnsURL', None),
761+
'column_types': as_dict.get('columnTypes', None),
719762
'complete': as_dict.get('complete', None),
720763
'csv': as_dict.get('csv', None),
721764
'csv_url': as_dict.get('csvURL', None),
@@ -749,6 +792,7 @@ def _to_untrimmed_dict(self, in_cls = None) -> dict:
749792
'beforeParse': self.before_parse,
750793
'columns': self.columns,
751794
'columnsURL': self.columns_url,
795+
'columnTypes': self.column_types,
752796
'complete': self.complete,
753797
'csv': self.csv,
754798
'csvURL': self.csv_url,

0 commit comments

Comments
 (0)