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

Caching a box resuts in read error #260

Open
srikiran-cpn opened this issue Sep 27, 2023 · 1 comment
Open

Caching a box resuts in read error #260

srikiran-cpn opened this issue Sep 27, 2023 · 1 comment
Labels

Comments

@srikiran-cpn
Copy link

I have a use case where I want to cache boxes and read them back again. Below is a MWE using joblib for caching but I also found it doesn't work Flask caching. I think some metadata is lost when caching the box, so the retrieved box somehow is not a box with box_dots = True.

Code


import box
print(box.__version__)
from box import Box
import joblib
print(joblib.__version__)
b2 = Box(
    {
        "l1": {
            "time_range_selected_utc": {
                "left": "2023-03-01 10:00:00",
                "right": "2023-06-01 10:00:00",
            }
        }
    },
    box_dots=True,
    conversion_box=False,
    frozen_box=False,
)
print(b2.l1.time_range_selected_utc.left)
print(b2["l1.time_range_selected_utc.right"])
print(b2["l1.time_range_selected_utc.left"])

joblib.dump(b2, "boxdump.joblib")

b3 = joblib.load("boxdump.joblib")
print(b3.l1.time_range_selected_utc.left)
print(b3["l1.time_range_selected_utc.right"]) # this will fail
print(b3["l1.time_range_selected_utc.left"])

Output

7.1.1
1.3.2
2023-03-01 10:00:00
2023-06-01 10:00:00
2023-03-01 10:00:00
2023-03-01 10:00:00
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File [~/opt/miniconda3/envs/om3/lib/python3.9/site-packages/box/box.py:592](https://file+.vscode-resource.vscode-cdn.net/Users/sriki/Documents/cpnet/OmegaMuller/~/opt/miniconda3/envs/om3/lib/python3.9/site-packages/box/box.py:592), in box.box.Box.__getitem__()

KeyError: 'l1.time_range_selected_utc.right'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
File [~/opt/miniconda3/envs/om3/lib/python3.9/site-packages/box/box.py:592](https://file+.vscode-resource.vscode-cdn.net/Users/sriki/Documents/cpnet/OmegaMuller/~/opt/miniconda3/envs/om3/lib/python3.9/site-packages/box/box.py:592), in box.box.Box.__getitem__()

KeyError: 'time_range_selected_utc.right'

...
@cdgriffith cdgriffith added the bug label Sep 29, 2023
@cdgriffith
Copy link
Owner

It seems that when pickling the box, only the outer most box is set to box_dots=True for some reason.

from box import Box
import pickle

b2 = Box(
    {
        "l1": {
            "time_range_selected_utc": {
                "left": "2023-03-01 10:00:00",
                "right": "2023-06-01 10:00:00",
            }
        },
    },
    **{'box_dots': True, 'conversion_box': False, 'frozen_box': False}
)

pickle.dump(b2, open("testpickle", "wb"))
loaded = pickle.load(open("testpickle", "rb"))

print(loaded._box_config)
print(loaded["l1"])  # Works

print(loaded["l1"]._box_config)
print(loaded["l1.time_range_selected_utc"])  # Works

print(loaded["l1.time_range_selected_utc"]._box_config)
print(loaded["l1.time_range_selected_utc.left"])  # Breaks

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

No branches or pull requests

2 participants