Skip to content

Serializers

ImageSerializer

Bases: SerializerBase

Pillow image serializer.

Source code in lume_services/files/serializers/image.py
 5
 6
 7
 8
 9
10
11
12
13
14
class ImageSerializer(SerializerBase):
    """Pillow image serializer."""

    def serialize(self, filename, image: Image):
        image.save(filename)

    @classmethod
    def deserialize(cls, filename) -> Image:

        return Image.open(filename)

YAMLSerializer

Bases: SerializerBase

Serializer sublass handling YAML files.

Source code in lume_services/files/serializers/yaml.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class YAMLSerializer(SerializerBase):
    """Serializer sublass handling YAML files."""

    def serialize(self, filename: str, object: List[dict]) -> None:
        """Serialize an object to a YAML.


        Args:
            filename (str): Name of file to write.
            object (List[dict]): Object to serialize.

        """

        with open(filename, "w") as f:
            yaml.dump(object, f)

    @classmethod
    def deserialize(cls, filename: str) -> List[dict]:
        """Deserializes a given YAML file.

        Args:
            filename (str): Name of file to deserialize.

        Returns:
            List[dict]: Loaded YAML representation.

        """
        yaml_rep = None

        with open(filename, "r") as f:

            try:
                yaml_rep = yaml.safe_load(f)

            except yaml.YAMLError as exc:
                logger.exception(exc)

        return yaml_rep

deserialize(filename) classmethod

Deserializes a given YAML file.

Parameters:

Name Type Description Default
filename str

Name of file to deserialize.

required

Returns:

Type Description
List[dict]

List[dict]: Loaded YAML representation.

Source code in lume_services/files/serializers/yaml.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@classmethod
def deserialize(cls, filename: str) -> List[dict]:
    """Deserializes a given YAML file.

    Args:
        filename (str): Name of file to deserialize.

    Returns:
        List[dict]: Loaded YAML representation.

    """
    yaml_rep = None

    with open(filename, "r") as f:

        try:
            yaml_rep = yaml.safe_load(f)

        except yaml.YAMLError as exc:
            logger.exception(exc)

    return yaml_rep

serialize(filename, object)

Serialize an object to a YAML.

Parameters:

Name Type Description Default
filename str

Name of file to write.

required
object List[dict]

Object to serialize.

required
Source code in lume_services/files/serializers/yaml.py
12
13
14
15
16
17
18
19
20
21
22
23
def serialize(self, filename: str, object: List[dict]) -> None:
    """Serialize an object to a YAML.


    Args:
        filename (str): Name of file to write.
        object (List[dict]): Object to serialize.

    """

    with open(filename, "w") as f:
        yaml.dump(object, f)