Skip to content
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

__repr__ and __str__ suggestion #208

Closed
jrobbins-LiveData opened this issue Aug 22, 2021 · 2 comments
Closed

__repr__ and __str__ suggestion #208

jrobbins-LiveData opened this issue Aug 22, 2021 · 2 comments

Comments

@jrobbins-LiveData
Copy link

The current __repr__ function, while perfectly valid per the Python docs, could be made more informative by indicating that nested dicts are actually also Box objects. With a couple of small changes, as sketched below, that missing info could be made available, along with making the __repr__ output evalable, which is also valid per the Python docs.

from box import Box


def main():
    b1 = Box({'outer': {'k1': 'v1', 'inner': {'k2': 'v2'}}})
    print(f"before new methods: {b1=}")

    Box.__repr__ = lambda self: f"Box({self})"
    Box.__str__ = lambda self: f"{ {k:v for k,v in self.items()} }"

    print(f" after new methods: {b1=}")
    print(f"{eval(repr(b1))=}")


if __name__ == "__main__":
    main()
before new methods: b1=<Box: {'outer': {'k1': 'v1', 'inner': {'k2': 'v2'}}}>
 after new methods: b1=Box({'outer': Box({'k1': 'v1', 'inner': Box({'k2': 'v2'})})})
eval(repr(b1))=Box({'outer': Box({'k1': 'v1', 'inner': Box({'k2': 'v2'})})})

Correct code, instead of my quick and dirty monkey patch, should use self._box_config["box_class"] instead of hardcoding Box in the __repr__ function like I did.

cdgriffith added a commit that referenced this issue Nov 11, 2021
…Robbins)

* Fixing internal `_safe_key` logic to be twice as fast
@cdgriffith
Copy link
Owner

cdgriffith commented Jan 21, 2022

Adding these features in 6.0, currently have a release candidate that can be installed and tested with:

pip install python-box[all]==6.0.0rc2 --upgrade 

6.0 is ushering in Cython speedups on supported platforms, so please let me know if you run into any issues!

cdgriffith added a commit that referenced this issue Mar 15, 2022
* Adding Cython support to greatly speed up normal Box operations on supported systems
* Adding #161 support for access box dots with `get` and checking with `in` (thanks to scott-createplay)
* Adding #183 support for all allowed character sets (thanks to Giulio Malventi)
* Adding #196 support for sliceable boxes (thanks to Dias)
* Adding #164 default_box_create_on_get toggle to disable setting box variable on get request (thanks to ipcoder)
* Changing #208 __repr__ to produce `eval`-able text (thanks to Jeff Robbins)
* Changing #215 support ruamel.yaml new syntax (thanks to Ivan Pepelnjak)
* Changing `update` and `merge_update` to not use a keyword that could cause issues in rare circumstances
* Changing internal `_safe_key` logic to be twice as fast
* Removing support for ruamel.yaml < 0.17
@cdgriffith
Copy link
Owner

Box 6 has been released with this added, thanks for opening the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants