Library Internals
module: .metaclasses
The .metaclasses
module contains - as one might
expect - metaclasses that are used to ensure a consistent interface
throughout the Highcharts Stock for Python library.
class: HighchartsMeta
- class HighchartsMeta(**kwargs)[source]
Metaclass that is used to define the standard interface exposed for serializable objects.
Class Inheritance
- classmethod _copy_dict_key(key, original, other, overwrite=True, **kwargs)[source]
Copies the value of
key
fromoriginal
toother
.
- abstract classmethod _get_kwargs_from_dict(as_dict)[source]
Convenience method which returns the keyword arguments used to initialize the class from a Highcharts Javascript-compatible
dict
object.
- _process_required_modules(scripts=None, include_extension=False) List[str] [source]
Return the list of URLs from which the Highcharts JavaScript modules needed to render the chart can be retrieved.
- abstract _to_untrimmed_dict(in_cls=None) dict [source]
Generate the first-level of the
dict
representation of the object.Note
This method does not traverse the object structure to convert the object into a full
dict
representation - it merely goes “part” of the way there to replace the Highcharts for Python keys with their correpsond Highcharts JS keys.- Return type:
- _untrimmed_mro_ancestors(in_cls=None) dict [source]
Walk through the parent classes and consolidate the results of their
_to_untrimmed_dict()
methods into a singledict
.- Return type:
- classmethod _validate_js_literal(as_str, range=True, _break_loop_on_failure=False)[source]
Parse
as_str
as a valid JavaScript literal object.- Parameters:
- Returns:
The parsed AST representation of
as_str
and the updated string.- Return type:
- copy(other=None, overwrite=True, **kwargs)[source]
Copy the configuration settings from this instance to the
other
instance.- Parameters:
other (
HighchartsMeta
) – The target instance to which the properties of this instance should be copied. IfNone
, will create a new instance and populate it with properties copied fromself
. Defaults toNone
.overwrite (
bool
) – ifTrue
, properties inother
that are already set will be overwritten by their counterparts inself
. Defaults toTrue
.kwargs – Additional keyword arguments. Some special descendents of
HighchartsMeta
may have special implementations of this method which rely on additional keyword arguments.
- Returns:
A mutated version of
other
with new property values
- classmethod from_dict(as_dict: dict, allow_snake_case: bool = True)[source]
Construct an instance of the class from a
dict
object.- Parameters:
- Returns:
A Python object representation of
as_dict
.- Return type:
- classmethod from_js_literal(as_str_or_file, allow_snake_case: bool = True, _break_loop_on_failure: bool = False)[source]
Return a Python object representation of a Highcharts JavaScript object literal.
- Parameters:
as_str_or_file (
str
) – The JavaScript object literal, represented either as astr
or as a filename which contains the JS object literal.allow_snake_case (
bool
) – IfTrue
, interpretssnake_case
keys as equivalent tocamelCase
keys. Defaults toTrue
._break_loop_on_failure (
bool
) – IfTrue
, will break any looping operations in the event of a failure. Otherwise, will attempt to repair the failure. Defaults toFalse
.
- Returns:
A Python object representation of the Highcharts JavaScript object literal.
- Return type:
- classmethod from_json(as_json_or_file, allow_snake_case: bool = True)[source]
Construct an instance of the class from a JSON string.
- Parameters:
as_json_or_file – The JSON string for the object or the filename of a file that contains the JSON string.
allow_snake_case (
bool
) – IfTrue
, interpretssnake_case
keys as equivalent tocamelCase
keys. Defaults toTrue
.
- Returns:
A Python objcet representation of
as_json
.- Return type:
- get_required_modules(include_extension=False) List[str] [source]
Return the list of URLs from which the Highcharts JavaScript modules needed to render the chart can be retrieved.
- to_dict() dict [source]
Generate a
dict
representation of the object compatible with the Highcharts JavaScript library.Note
The
dict
representation has a property structure and naming convention that is intentionally consistent with the Highcharts JavaScript library. This is not Pythonic, but it makes managing the interplay between the two languages much, much simpler.
- to_js_literal(filename=None, encoding='utf-8', careful_validation=False) str | None [source]
Return the object represented as a
str
containing the JavaScript object literal.- Parameters:
along the way using the esprima-python library. Defaults to
False
.Warning
Setting this value to
True
will significantly degrade serialization performance, though it may prove useful for debugging purposes.
- to_json(filename=None, encoding='utf-8')[source]
Generate a JSON string/byte string representation of the object compatible with the Highcharts JavaScript library.
Note
This method will either return a standard
str
or abytes
object depending on the JSON serialization library you are using. For example, if your environment has orjson, the result will be abytes
representation of the string.- Parameters:
- Returns:
A JSON representation of the object compatible with the Highcharts library.
- Return type:
- static trim_dict(untrimmed: dict, to_json: bool = False, context: str = None) dict [source]
Remove keys from
untrimmed
whose values areNone
and convert values that have.to_dict()
methods.- Parameters:
untrimmed (
dict
) – Thedict
whose values may still beNone
or Python objects.to_json (
bool
) – IfTrue
, will remove all keys fromuntrimmed
that are not serializable to JSON. Defaults toFalse
.context (
str
orNone
) – If provided, will inform the method of the context in which it is being run which may inform special handling cases (e.g. where empty strings may be important / allowable). Defaults toNone
.
- Returns:
Trimmed
dict
- Return type:
- static trim_iterable(untrimmed, to_json=False, context: str = None)[source]
Convert any
EnforcedNullType
values inuntrimmed
to'null'
.- Parameters:
untrimmed (iterable) – The iterable whose members may still be
None
or Python objects.to_json (
bool
) – IfTrue
, will remove all members fromuntrimmed
that are not serializable to JSON. Defaults toFalse
.context (
str
orNone
) – If provided, will inform the method of the context in which it is being run which may inform special handling cases (e.g. where empty strings may be important / allowable). Defaults toNone
.
- Return type:
iterable
class: JavaScriptDict
- class JavaScriptDict(dict=None, /, **kwargs)[source]
Special
dict
class which constructs a JavaScript object that can be represented as a string.Keys are validated to be valid variable names, while values are validated to be strings.
When serialized to
str
, keys are not wrapped in double quotes (as they would be in JSON) to ensure that the resulting string can be evaluated as JavaScript code.Class Inheritance
- classmethod _validate_js_literal(as_str, range=True, _break_loop_on_failure=False)[source]
Parse
as_str
as a valid JavaScript literal object.- Parameters:
- Returns:
The parsed AST representation of
as_str
and the updated string.- Return type:
- clear() None. Remove all items from D.
- classmethod from_dict(as_dict: dict, allow_snake_case: bool = True)[source]
Construct an instance of the class from a
dict
object.- Parameters:
- Returns:
A Python object representation of
as_dict
.- Return type:
- classmethod from_js_literal(as_str_or_file, allow_snake_case: bool = True, _break_loop_on_failure: bool = False)[source]
Return a Python object representation of a Highcharts JavaScript object literal.
- Parameters:
as_str_or_file (
str
) – The JavaScript object literal, represented either as astr
or as a filename which contains the JS object literal.allow_snake_case (
bool
) – IfTrue
, interpretssnake_case
keys as equivalent tocamelCase
keys. Defaults toTrue
._break_loop_on_failure (
bool
) – IfTrue
, will break any looping operations in the event of a failure. Otherwise, will attempt to repair the failure. Defaults toFalse
.
- Returns:
A Python object representation of the Highcharts JavaScript object literal.
- Return type:
- classmethod from_json(as_json)[source]
Construct an instance of the class from a JSON string.
- Parameters:
- Returns:
A Python objcet representation of
as_json
.- Return type:
- get(k[, d]) D[k] if k in D, else d. d defaults to None.
- items() a set-like object providing a view on D's items
- keys() a set-like object providing a view on D's keys
- pop(k[, d]) v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair
as a 2-tuple; but raise KeyError if D is empty.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
- to_dict()[source]
Generate a
dict
representation of the object compatible with the Highcharts JavaScript library.Note
The
dict
representation has a property structure and naming convention that is intentionally consistent with the Highcharts JavaScript library. This is not Pythonic, but it makes managing the interplay between the two languages much, much simpler.
- to_js_literal(filename=None, encoding='utf-8', careful_validation=False) str | None [source]
Return the object represented as a
str
containing the JavaScript object literal.- Parameters:
along the way using the esprima-python library. Defaults to
False
.Warning
Setting this value to
True
will significantly degrade serialization performance, though it may prove useful for debugging purposes.
- to_json(filename=None, encoding='utf-8')[source]
Generate a JSON string/byte string representation of the object compatible with the Highcharts JavaScript library.
Note
This method will either return a standard
str
or abytes
object depending on the JSON serialization library you are using. For example, if your environment has orjson, the result will be abytes
representation of the string.- Parameters:
- Returns:
A JSON representation of the object compatible with the Highcharts library.
- Return type:
- static trim_dict(untrimmed: dict, to_json: bool = False, context: str = None) dict [source]
Remove keys from
untrimmed
whose values areNone
and convert values that have.to_dict()
methods.- Parameters:
untrimmed (
dict
) – Thedict
whose values may still beNone
or Python objects.to_json (
bool
) – IfTrue
, will remove all keys fromuntrimmed
that are not serializable to JSON. Defaults toFalse
.context (
str
orNone
) – If provided, will inform the method of the context in which it is being run which may inform special handling cases (e.g. where empty strings may be important / allowable). Defaults toNone
.
- Returns:
Trimmed
dict
- Return type:
- static trim_iterable(untrimmed, to_json=False, context: str = None)[source]
Convert any
EnforcedNullType
values inuntrimmed
to'null'
.- Parameters:
untrimmed (iterable) – The iterable whose members may still be
None
or Python objects.to_json (
bool
) – IfTrue
, will remove all members fromuntrimmed
that are not serializable to JSON. Defaults toFalse
.context (
str
orNone
) – If provided, will inform the method of the context in which it is being run which may inform special handling cases (e.g. where empty strings may be important / allowable). Defaults toNone
.
- Return type:
iterable
- update([E, ]**F) None. Update D from mapping/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values
module: .decorators
The .decorators
module contains decorators and
decorator-assisting functions used throughout the Highcharts Stock for Python library.
decorator: @class_sensitive
- @class_sensitive(types=None, allow_dict=True, allow_json=True, allow_none=True, allow_js_literal=True, force_iterable=False)[source]
Validates that the values passed to a decorated function or method are de-serialized to the appropriate type.
- Parameters:
types –
type
object or iterable oftype
objects used to indicate which types are allowed. The first (or only) item intypes
will indicate the primary type thatvalue
will be returned as.allow_dict (
bool
) – IfTrue
, will accept adict
object asvalue
. Defaults toTrue
.allow_json (
bool
) – IfTrue
, will accept astr
object asvalue
, under the assumption that is is deserializable as JSON. Defaults toTrue
.allow_none (
bool
) – IfTrue
, will accept an empty orNone
object asvalue
. Defaults toTrue
.allow_js_literal (
bool
) – IfTrue
, will accept astr
object asvalue
, under the assumption that it is deserializable as a JavaScript object literal. Defaults toTrue
.force_iterable (
bool
) – IfTrue
, will accept an iterable object asvalue
. Defaults toFalse
(because most attributes are just singletons).
- Types:
Note
To apply the decorator to a property setter method (the most-common use case), place it after the
@<property name>.setter
decorator and directly above the function name like so:@some_property.setter @class_sensitive(...) def some_property(self, value): ...
- Returns:
The result of the decorated function or method having validated the class typing.
- Raises:
HighchartsImplementationError – if
types
is emptyHighchartsValueError – if
types
does not contain atype
or iterable oftype
objectsHighchartsValueError – if the primary type does not conform to the
HighchartsMeta
interface definition
function: validate_types()
- validate_types(value, types=None, allow_dict=True, allow_json=True, allow_none=True, allow_js_literal=True, force_iterable=False, function_name=None)[source]
Validates that
value
is one or more of the allowed types, where the first type passed intypes
is the primary type that it will be returned as.- Parameters:
value (Any) – The value to be validated.
types –
type
object or iterable oftype
objects used to indicate which types are allowed. The first (or only) item intypes
will indicate the primary type thatvalue
will be returned as.allow_dict (
bool
) – IfTrue
, will accept adict
object asvalue
. Defaults toTrue
.allow_json (
bool
) – IfTrue
, will accept astr
object asvalue
, under the assumption that is is deserializable as JSON. Defaults toTrue
.allow_none (
bool
) – IfTrue
, will accept an empty orNone
object asvalue
. Defaults toTrue
.allow_js_literal (
bool
) – IfTrue
, will accept astr
object asvalue
, under the assumption that it is deserializable as a JavaScript object literal. Defaults toTrue
.force_iterable (
bool
) – IfTrue
, will accept an iterable object asvalue
. Defaults toFalse
(because most attributes are just singletons).function_name (
str
) – The optional name of the function that was originally called.
- Types:
- Returns:
value
de-serialized to the primary type (firsttype
intypes
)- Return type:
first
type
intypes
- Raises:
HighchartsImplementationError – if
types
is emptyHighchartsValueError – if
types
does not contain atype
or iterable oftype
objectsHighchartsValueError – if the primary type does not conform to the
HighchartsMeta
interface definition
module:: .js_literal_functions
The .js_literal_functions
module contains
functions that are used to parse, process, de-serialize, and serialize JavaScript literal
notation.
function: serialize_to_js_literal()
- serialize_to_js_literal(item, encoding='utf-8', ignore_to_array=False, careful_validation=False) str | None [source]
Convert
item
to the contents of a JavaScript object literal code snippet.- Parameters:
item – A value that is to be converted into a JS object literal notation value.
encoding (
str
) – The character encoding to apply to the resulting object. Defaults to'utf-8'
.ignore_to_array (
bool
) – IfTrue
, will ignore handling of the.to_array()
method to break recursion. Defaults toFalse
.careful_validation (
bool
) –if
True
, will carefully validate JavaScript values along the way using the esprima-python library. Defaults toFalse
.Warning
Setting this value to
True
will significantly degrade serialization performance, though it may prove useful for debugging purposes.
- Returns:
A JavaScript object literal code snippet, expressed as a string. Or
None
ifitem
is not serializable.- Return type:
function: attempt_variable_declaration()
function: is_js_function_or_class()
- is_js_function_or_class(as_str, careful_validation=False) bool [source]
Determine whether
as_str
is a JavaScript function or not.- Parameters:
as_str (
str
) – The string to evaluate.careful_validation (
bool
) –if
True
, will carefully validate JavaScript values along the way using the esprima-python library. Defaults toFalse
.Warning
Setting this value to
True
will significantly degrade serialization performance, though it may prove useful for debugging purposes.
- Returns:
True
ifas_str
is a JavaScript function.False
if not.- Return type:
function: get_js_literal()
- get_js_literal(item, careful_validation=False) str [source]
Convert the value of
item
into a JavaScript literal string.- Parameters:
careful_validation (
bool
) –if
True
, will carefully validate JavaScript values along the way using the esprima-python library. Defaults toFalse
.Warning
Setting this value to
True
will significantly degrade serialization performance, though it may prove useful for debugging purposes.- Returns:
The JavaScript literal string.
- Return type:
function: assemble_js_literal()
- assemble_js_literal(as_dict, keys_as_strings=False, careful_validation=False) str | None [source]
Convert
as_dict
into a JavaScript object literal string.- Parameters:
as_dict (
dict
) – Adict
representation of the JavaScript object.keys_as_strings (
bool
) – ifTrue
, will return the keys as string values (wrapped in quotation marks). IfFalse
, will return the keys as object literals. Defaults toFalse
.careful_validation (
bool
) –if
True
, will carefully validate JavaScript values along the way using the esprima-python library. Defaults toFalse
.Warning
Setting this value to
True
will significantly degrade serialization performance, though it may prove useful for debugging purposes.
- Returns:
The JavaScript object literal representation of
as_dict
.- Return type:
function: convert_js_literal_to_python()
function: convert_js_property_to_python()
function: convert_js_to_python()
function: get_key_value_pairs()
- get_key_value_pairs(properties, original_str)[source]
Return the key and value pairs for properties defined in
properties
.
.utility_functions
The .utility_functions
module contains a
small number of functions which serve as utilities across the
Highcharts Stock for Python library. Think of it as a function “catch all” module.
function:: mro_to_dict()
function:: get_remaining_mro()
function:: mro__to_untrimmed_dict()
- mro__to_untrimmed_dict(obj, in_cls=None)[source]
Traverse the ancestor classes of
obj
and execute their_to_untrimmed_dict()
methods.- Parameters:
obj (
HighchartsMeta
) – The object to be traversed.in_cls (
type
orNone
) – The class from whichmro__to_untrimmed_dict()
was called.
- Returns:
Collection of untrimmed
dict
representations in the same order as the MRO.- Return type:
for each class in the MRO, execute _to_untrimmed_dict() do not repeat for each class
function:: validate_color()
function:: to_camelCase()
function:: parse_csv()
- parse_csv(csv_data, has_header_row=True, delimiter=',', null_text='None', wrapper_character="'", wrap_all_strings=False, double_wrapper_character_when_nested=False, escape_character='\\', line_terminator='\r\n')[source]
Parse
csv_data
to return a list ofdict
objects, one for each record.- Parameters:
- Returns:
Collection of column names (or numerical keys) and CSV records as
dict
values- Return type: