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

disabling implicit branch creation #7

Open
benureau opened this issue Sep 26, 2013 · 2 comments
Open

disabling implicit branch creation #7

benureau opened this issue Sep 26, 2013 · 2 comments

Comments

@benureau
Copy link

Hi,

I use treedict more and more to distribute parameters of scientific simulations. However, I regularly get bitten by implicit branch creation. I misspell the name of a branch I want to retrieve, and rather than raising an exception, it gives me a dandling branch, which creates problems when the value is actually used, which can be in some completely different place in the code. Hard to debug.

Moreover, since dandling branches are invisible, I can't even check that dandling branches exist or were accessed, which makes any kind of automatic error-checking on that aspect (eg. unit test) impossible.

Would it be possible to disable the implicit branch creation, without freezing the structure ? For instance, something like :

t = treedict.TreeDict()
t.disable_implicit()

t.b = 3 # creating node is ok
t.a.b.c = 1 # creating branch implicitly with non dandling leaves is ok
t.b # accessing existing node is ok
t.a # accessing existing branch is ok

t.c # raise Error.

I looked at the code, but did not find the right combination of flag to make this work.

@hoytak
Copy link
Owner

hoytak commented Oct 1, 2014

Hello humm,

I apologize -- I just saw this comment. I've started a new job in the last year, and only recently got back to maintaining my open source projects. Did you figure out the answer?

It should be possible to disable implicit branch creation by calling t.freeze(structure_only = True). This allows existing values to be altered, but no new branches to be created.

-- Hoyt

@benureau
Copy link
Author

Hi Hoyt,

t.a.b # raise Error
t.a.b.c = 1 # ok

is a problem. Because python would have to create or access a and b before figuring out what I want to do with it. So having both things happen is not possible.

t.freeze(structure_only = True does not solve my problem. I want to add things to the tree, and I can't unfreeze the tree.

I ended up cooking up my own solution (pure python), that adds optional typechecks and logging capabilities to the mix, and has an option strict when you can't create a branch or leaf you didn't declare. Not very pythonic (I also exclusively use methods prefixed by underscore), but I can verify that things happened as they should after an experiment finishes. You can have a look at it here. It was largely inspired by your work - except all the crazy things.

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

No branches or pull requests

3 participants
@hoytak @benureau and others