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

Support for getters? #21

Open
qria opened this issue Jun 15, 2017 · 1 comment
Open

Support for getters? #21

qria opened this issue Jun 15, 2017 · 1 comment

Comments

@qria
Copy link

qria commented Jun 15, 2017

Common pattern for optional property is to use .get:

class MyConfig(Configuration):
    my_name = config_property('my_name', str)

config = MyConfig({'my_name': 'myname'})

print('hello %s' % (config.my_name or 'anonymous'))  # hello myname
print('hello %s' % config.get('my_name', 'anonymous'))  # hello myname

Unfortunately currently this fails silently in some cases.

class MyConfig(Configuration):
    my_name = config_property('my_name', str, default='defaultname')

config = MyConfig()

print('hello %s' % (config.my_name or 'anonymous'))  # hello defaultname
print('hello %s' % config.get('my_name', 'anonymous'))  # hello anonymous
class MyConfig(Configuration):
    my_name = config_property('myinfo.name', str)

config = MyConfig({'myinfo': {'name': 'myname', 'age': 10, 'gender': 'male'}})

print('hello %s' % (config.my_name or 'anonymous'))  # hello myname
print('hello %s' % config.get('my_name', 'anonymous'))  # hello anonymous

I am not sure whether or not settei intends to support getters,

but I think it would be nice if settei supported getters or explicitly raise an error.

@dahlia
Copy link
Contributor

dahlia commented Jun 15, 2017

If you don't set any default/default_func for a config_property it will raise KeyError on lack of value.

.get() is a part of collections.Mapping protocol (dictionary-like interface).

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

2 participants