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-translate keys invalid for property access? #2

Open
techtonik opened this issue Jan 30, 2012 · 3 comments
Open

Auto-translate keys invalid for property access? #2

techtonik opened this issue Jan 30, 2012 · 3 comments

Comments

@techtonik
Copy link

It is not documented what happens if Bunch() is expanded using dictionary notation with an element, which key is invalid for property access?

@dsc
Copy link
Owner

dsc commented Feb 23, 2012

Can you give an example? I don't think I follow.

@luckydonald
Copy link

@dsc
Maybe something like

bunched["3"] = "foo";
bunched.3  # is not valid syntax.

bunched["foo'quotes'bar"] = "hua"
bunched.foo'quotes'bar  # does not work either.

bunched["lol2345"] = "rickrolled."
bunched.lol2345  # works.

Maybe replace all wrong characters with _ and prefix strings starting with a number with int_?

Regex for the former could be (generating only one _ for multible illegal characters:

import re
unallowed_in_variable_name = re.compile('[\W]+')
var_name = unallowed_in_variable_name.sub('_', name)  
#  {'foo-2.4;"':'foo'} becomes bunched.foo_2_4_

pombredanne referenced this issue in pombredanne/bunch Sep 27, 2015
@dsc
Copy link
Owner

dsc commented Oct 19, 2024

Maybe replace all wrong characters with _ and prefix strings starting with a number with int_?
...

I think it's most pythonesque to raise not require. I have code for a Bunch subclass that can validate/transform keys. I'm not sure if this really is all that useful though: almost every usecase is obscure and you could simply write a small wrapper where you insert keys (or even wrap __setitem__ / __getitem__).

@dsc dsc changed the title Bunch with invalid key for property access Auto-translate keys invalid for property access? Oct 19, 2024
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