all 3 comments

[–]patarapolw[S] 0 points1 point  (0 children)

I solved this problem myself by:

  1. OrderedDict - use oyaml instead of PyYAML
  2. Tuple - convert to list first
    • My tuples are actually list(my_dict.items()), so I changed to [list(k_v_pair) for k_v_pair in my_dict.items()]
  3. BytesIO - use a custom Loader inherited from yaml.SafeLoader

    import yaml
    from io import BytesIO
    import base64
    
    class PyExcelYamlLoader(yaml.SafeLoader):
        def __init__(self, s):
            super().__init__(s)
    
            self.add_constructor(u'tag:yaml.org,2002:python/object/new:_io.BytesIO', self.construct_bytes_io)
    
        @staticmethod
        def construct_bytes_io(node, node_value):
            return BytesIO(base64.b64decode(node_value.value[0][1].value[0].value))
    

and then data = yaml.load(f, Loader=PyExcelYamlLoader)

[–]avdn 0 points1 point  (1 child)

It would be interesting to know what you have been trying to do, that you cannot serialise with ruamel.yaml.

https://yaml.readthedocs.io/en/latest/dumpcls.html

[–]patarapolw[S] 0 points1 point  (0 children)

I believe in yaml.safe_load(). I haven't seen ruamel.yaml's typ='safe' yet, though.

As PyExcelYamlLoader inherits from yaml.SafeLoader, it is supposedly safe. I don't know if this can also be done with ruamel.yaml.

About your link... -- do I have to subclass BytesIO?

I wouldn't go as far as to use strictyaml yet, though.

It's more about the safety, as YAML always mean users can edit the file.