settei
settei copied to clipboard
Support for getters?
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.
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).