You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would suggest a version of the transform method, where there is no change if a field is missing.
My motivation is, that I like the idea of pyrsistent's tranformation, but I use document databases with optional fields and subfields and I would like to keep the original state where a field or parent field is missing. (Also pydantic can use Optional fields.) Coming up with some weird lambda function instead of ny would mess up the code.
m(
composer=m(birth=m(place="SALZBURG", year=1777), name="Felix"), # The only place that changes is place here.lyrics=m(name="John"),
arranged_by=m(name="Jack", birth=m(year=1977))
)
doesn't create birth/place in lyrics and arranged_by. And I don't need to use a more difficult function instead of str.upper to handle the missing fields.
Maybe pyrsistent could use the same transform method with an additional mode keyword argument, that is "strict" by default with the current behavior, and maybe "loose" is the behavior I presented here. (I'm not a native English speaker, so I couldn't find better word than loose.)
I think that discard could work with the same way, so using discard instead of str.upper in the second example would just remove place="Salzburg" from the composer field.
The text was updated successfully, but these errors were encountered:
horvatha
changed the title
Transformation: not strict version
Transformation that not strict with missing fields
Jun 27, 2022
Thanks for opening this issue. The use case seems reasonable to me. Could you please create a PR with a suggested implementation for me to review (the gist looks fine in principle but I need to have a look at it in a pyrsistent-context) and eventually merge if you want it included in pyrsistent?
Using a new keyword arg to transform seems like the most discoverable way of doing I think.
My main concern is: the transform function's current version works for every pyrsistant data structure. As I would like to use JSON-like structures, I don't need any other than PVector and PMap. Is is ok if for the other data structures it just raises an exception if I don't use the current strict mode?
I'm not sure I can create any PR soon.
I would suggest a version of the
transform
method, where there is no change if a field is missing.My motivation is, that I like the idea of pyrsistent's tranformation, but I use document databases with optional fields and subfields and I would like to keep the original state where a field or parent field is missing. (Also
pydantic
can use Optional fields.) Coming up with some weird lambda function instead ofny
would mess up the code.I give you some examples:
would give back
doesn't do anything with the second element.
A second example:
gives back
doesn't create birth/place in lyrics and arranged_by. And I don't need to use a more difficult function instead of
str.upper
to handle the missing fields.I have a
loose_transform
implementation and unit tests as gist.Maybe
pyrsistent
could use the sametransform
method with an additionalmode
keyword argument, that is"strict"
by default with the current behavior, and maybe"loose"
is the behavior I presented here. (I'm not a native English speaker, so I couldn't find better word than loose.)I think that
discard
could work with the same way, so usingdiscard
instead ofstr.upper
in the second example would just removeplace="Salzburg"
from the composer field.The text was updated successfully, but these errors were encountered: