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

Auto attributes creation #9

Closed
jhermann opened this issue Mar 31, 2017 · 4 comments
Closed

Auto attributes creation #9

jhermann opened this issue Mar 31, 2017 · 4 comments
Milestone

Comments

@jhermann
Copy link

jhermann commented Mar 31, 2017

>>> b = box.Box()
>>> b.o.x = 42
AttributeError: 'Box' object has no attribute 'o'

IMHO this should work, possibly via a subtype (AutoBox) or __init__ keyword arg (auto_attr=True).

Another variant would be OpenBox combined with a close() method – i.e. allow path construction initially, but then switch to default mode (AttributeError) after construction.

@cdgriffith
Copy link
Owner

I'm personally leaning towards doing keyword arguments, as that would allow for other customization and expansions in the future. I am mulling over how best to name them (so they aren't close to anything someone might have in a dictionary).

Either something like box_auto_attr or _auto_attr. Potentially add a box_auto_default=Box field as well so it's possible to make it more like a true default dict that a user can specify what type of object the default fallback is.

Closing #5 as it was less detailed

@jhermann
Copy link
Author

Sounds like separation of creating Box variants from using them would be valuable (separate namespaces with no awkward avoidance of overlap). So how about this:

@box.closable
@box.autoattr
class MyClosableAutoBox(Box):
    """Customized ``Box``."""

Chances are you want to use such a beast at several places anyway, and then you'd have to curry the Box construction with those keyword args. 😬

Alas, Python3 only, but you could call the decorators explicitly in Python2.

@cdgriffith
Copy link
Owner

It's own namespace would be nice and simple, but for #6 need to have some sort of namespace pollution anyways, so I have been working on a way to make it near invisible. https://github.com/cdgriffith/Box/blob/auto_attr/box.py#L269

my_box = Box({"I call Hax!": "totally"}, default_box=True, conversion_box=True)
my_box.i_call_hax
# Out[8]: 'totally'

my_box.sub_box.another_box.boxes_the_whole_way_down = 'Yet another box'
my_box
# Out[10]: <Box: {'I call Hax!': 'totally', 'sub_box': {'another_box': {'boxes_the_whole_way_down': 'Yet another box'}}}>

Could still make it into it's own subclass if those same options will always be repeated (still mulling over just adding a few of these as well for simplicity):

class DefaultBox(Box):
    def __init__(self, *args, **kwargs):
        super(Box, self).__init__(*args, **kwargs)
        self._box_config['default'] = True 

Thoughts?

@cdgriffith cdgriffith added this to the 3.0 milestone Apr 1, 2017
@cdgriffith
Copy link
Owner

added default_box in 3.0 6d90e42

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