On Fri, Aug 7, 2020 at 5:22 PM Kazantcev Andrey <hec...@yandex.ru> wrote:
>
> Maybe use context as a context manager.
>
> For example
>
> ```
> with json.Context(ensure_ascii=False):
>     json.dumps(...)
> ```
>
> Implementation can be done via contextlib.

If all you want is a way to parameterize naive calls to json.dumps(),
you could monkeypatch it.

import json
import contextlib

@contextlib.contextmanager
def monkeypatch_json(**defaults):
    orig = json.dumps
    try:
        def dumps(*a, **kw):
            return orig(*a, **{**defaults, **kw})
        json.dumps = dumps
        yield
    finally:
        json.dumps = orig

Not gonna be 100% reliable and I don't think it belongs in the stdlib,
but might be useful.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WDANEN56SJPWHAHZKEBI7B4CQSO4L6II/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to