-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow serialization of a subset of params #338
Conversation
Hi, thanks for your contribution. You're on the right track, but there there are a few details:
So here are my thoughts:
Here's some code I quickly put together that should get you started: import warnings
from copy import copy
from ploomber.env.expand import iterate_nested_dict
def is_json_serializable(o):
# per:
# https://github.com/python/cpython/blob/dea59cf88adf5d20812edda330e085a4695baba4/Lib/json/encoder.py#L73
return isinstance(o,
(dict, list, tuple, str, int, float, bool)) or o is None
d = {"a": 1, "b": 2}
d = {"foo": ["bar", object()], "stuff": object(), "number": 1}
d = {"foo": ["bar", {"another": object()}]}
d = {"foo": [1, 2, object(), object()]}
def remove_non_serializable_top_keys(d):
out = copy(d)
# this iterates over every value in a possibly nested dictionary
for (_, _, current_val, preffix) in iterate_nested_dict(d):
if not is_json_serializable(current_val):
top_key = preffix[0]
if top_key in out:
del out[top_key]
warnings.warn(f'Top-key {top_key!r} contains unserializable '
f'object: {current_val!r}. The key will be '
'ignored.')
return out
remove_non_serializable_top_keys(d) Let me know if you're ok with these changes. Feel free to edit this PR or open a new one. |
@edublancas Thanks for the feedback. There are a couple of things I'd like to ask you about.
Is this correct? Thanks for taking the time to help with this |
We're on the same page now :) |
@edublancas I've made the recommended changes and tested everything. I put the |
Thanks a lot for your contribution! Feel free to contribute/ask questions on any other open issues! |
* Allow serialization of a subset of params * Allow serialization of a subset of params #337 * parameter names * changes based on suggestions * formatting
Addresses #337
This is my first time contributing to a project. I'd be happy to hear any critiques