dict2css.serializer

Serializer for cascading style sheets.

New in version 0.2.0.

Classes:

CSSSerializer(*[, indent, …])

Serializes a dictionary to CSS.

class CSSSerializer(*, indent='\t', trailing_semicolon=None, indent_closing_brace=False, minify=False, sort_keys=False, check_circular=True, none_style='none')[source]

Serializes a dictionary to CSS.

This controls the formatting of the style sheet.

Parameters
  • indent (str) – The indent to use, such as a tab (\t), two spaces or four spaces. Default '\t'.

  • trailing_semicolon (Optional[bool]) – Whether to add a semicolon to the end of the final property. Default None.

  • indent_closing_brace (bool) – Default False.

  • minify (bool) – Minify the CSS. Overrides all other options. Default False.

  • sort_keys (bool) – Sort dictionary keys alphabetically. Default False.

  • check_circular (bool) – Check for circular references. Default True.

  • none_style (Union[Literal['none'], Literal['None']]) – Whether to represent None as None or none. Default 'none'.

Changed in version 0.5.0: New implementation. Output may differ slightly from previous css-parser based one.

Changed in version 0.6.0: Added none_style option.

Methods:

default(obj)

Override this method in a subclass to implement custom serialization for objects that are not serializable by default.

encode(obj)

Return a CSS representation of a Python dictionary.

iterencode(obj)

Encode the given dictionary and yield each part of the CSS string representation.

reset_style()

Reset the serializer to its default style.

use()

No-op.

default(obj)[source]

Override this method in a subclass to implement custom serialization for objects that are not serializable by default.

This method should return a serializable object. If this method is not overridden, the encoder will raise a ValueError when trying to encode an unsupported object.

Parameters

obj (Any) – The object to be serialized that is not supported by default.

Return type

Union[dict, list, tuple, int, float, str, bool, None]

Returns

A serializable object

Raises

ValueError: If the object cannot be serialized

encode(obj)[source]

Return a CSS representation of a Python dictionary.

Parameters

obj (Mapping) – The Python dictionary to be serialized

Return type

str

Returns

The CSS string representation of the Python dictionary

iterencode(obj)[source]

Encode the given dictionary and yield each part of the CSS string representation.

Parameters

obj (Mapping) – The Python dictionary to be serialized

Return type

Iterable[str]

Returns

An iterable of strings representing the CSS serialization of the Python dictionary.

reset_style()[source]

Reset the serializer to its default style.

use()[source]

No-op. Deprecated.

Return type

Iterator