Open M0r13n opened 3 years ago
@smarie Thanks for your suggestions. I already knew about pydantic. But it does't really fit my needs. I did not know about pyfields. It looks quite nice. Maybe I will take a look at it in a future project.
I both cases, I am missing the option to directly parse JSON or YAML files. I can build this of course. But it is nice, if I don't need to take care about that.
I both cases, I am missing the option to directly parse JSON or YAML files.
Indeed, only dict conversion is provided. If I'm not mistaken with marshmallow-pyfields
you can then bridge to the marshmallow ORM which provides json conversion, but this becomes then probably too similar to pydantic, which doesnt suit your needs.
Concerning yaml-ability PyYaml does not provide a super-easy yaml support for objects so I wrote yamlable
, a very thin layer on top for objects. But I never used pyfields + yamlable in combination so I do not know how that would work. related
is definitely easier in terms of packaging :)
Thanks again for your input. :-)
Those are some nice suggestions and ideas. It’s fascinating to see how many libraries already exist. One just has to know, where to search.
However, since I use related in a locked down and restricted area, it is difficult to introduce new packages. Dependencies are always a problem and need to be kept to a minimum. The nice thing about related was, that it is basically based on pyyaml and attrs. Both are useful in other contexts as well, so they can be reused
I may need to to some research and experimenting in order to find a future proof alternative to related.
It’s fascinating to see how many libraries already exist.
I would tend to say that this is a pain :) ... but it reflects the diversity of usages and constant evolution of the python world.
Anyway, for what's worth, I tried to combine the two libs mentioned above and found an issue in python 3.7+ with the ABC inheritance: https://github.com/smarie/python-pyfields/issues/84 . So ... thanks for bringing up this use case ! :) I fixed it, and now at least this tiny example works:
import json
import yaml
from pyfields import autoclass, field
from yamlable import yaml_info, YamlAble
@yaml_info(yaml_tag_ns='com.yamlable.example')
@autoclass
class Foo(YamlAble):
a: int = field(default=0)
b: str
# ----- object -----
f = Foo(b="hello")
print("OBJECT:")
print(f)
# ------ yaml -----
print("\nYAML:")
f_yml_str = f.dumps_yaml()
print(f_yml_str)
f2 = yaml.safe_load(f_yml_str)
assert f2 == f
# ------ json -----
print("JSON:")
f_json_str = json.dumps(f.to_dict())
print(f_json_str)
f3 = json.loads(f_json_str)
assert f3 == f
yields
OBJECT:
Foo(a=0, b='hello')
YAML:
!yamlable/com.yamlable.example.Foo
a: 0
b: hello
JSON:
{"a": 0, "b": "hello"}
I let you go further if needed, with these libs or with others - at least I have shared what I could :).
I'll now stop polluting the related
project with this side-discussion, apologies !
For anyone interested: I created a public fork of this lib which I will keep improving: https://github.com/M0r13n/related
It seems like this project was abandoned. There are quite a few open issues and the last commit was in 2019.
Personally, I like this library. But there are some improvements/fixes that should be merged. I fixed some of them in a private fork, but ultimately, it would be nice, if everyone could benefit from such fixes.