Well, it's a quite night on the forum tonight, so I may as well share something. I think it's smart idea, but I have been proven wrong many times before :( What I am posting is not finished but its enough to see the intent.
The intent I have in mind is to provide some simple data structures /classes for parameters to lib functions such as dialogs. I know, the param list is simple enough, but in practice you end up writing a lot of code to support the params. And not in a structured way.
Look, I accept if I am wrong. But it seems to me that when you write a lib, if you could support it with a type of generic data/param interface class without much work it would be a big bonus.
Just an idea
# coding: utf-8
import ui
import dialogs
_fld_attrs = {
'type': None,
'key' : None,
'value' : '',
'title' : None,
'tint_color': None,
'icon' : None,
'placeholder': '',
'autocorrection' : False,
'autocapitalization' : False,
}
class DialogFormField (object):
def __init__(self, **kwargs):
'''
DialogFormField
Just start of an idea to create an easy way to give more comlicated params to functions/methods some form of structured params. i could be off on the wrong track. But while the params look easy enough to a lot of libs, in practice they make for a lot code. also dot notation not avalible etc...
Again, is not finished... just exploring
'''
# create the class attributes from a dict
for attr in _fld_attrs:
setattr(self, attr, _fld_attrs[attr])
'''
# fields as per definition of the
# dialog.form_dialog()
self.type = None
self.key = None
self.value = ''
self.title = None
self.tint_color = None
self.icon = None
self.placeholder = 'Enter your text...'
self.autocorrection = False
self.autocapitalization = False
'''
# maybe this needs to be stronger. In my testing
# it has been ok. but maybe some tricky stuff,
# i dont know about.
self.flds = [attr for attr in dir(self) if not attr.startswith('__') and not callable(getattr(self, attr))]
for k in kwargs:
if hasattr(self, k):
setattr(self, k, kwargs[k])
# calling this because can not declare any more
# attributes in this method. we want to get a
# list of the field attrs. So can declare more
# class attrs in the init2 method.
self.init2()
def init2(self):
'''
just some test values to prove these attrs are not getting in the way
'''
self.another_item = 'init2'
self.xxx = 'init2'
def get_dict(self):
return {attr : getattr(self, attr) for attr in self.flds}
if __name__ == '__main__':
flds = []
fld = DialogFormField(title = 'test', type = 'text', tint_color = 'blue', placeholder = 'Enter your text...')
fld.icon = 'typb:Anchor'
fld.key = 'TextVal'
flds.append(fld.get_dict())
x = DialogFormField()
x.type = 'switch'
x.title = 'My switch'
x.value = 'True'
x.key = 'SwitchVal'
flds.append(x.get_dict())
d = dialogs.form_dialog(title = 'Test', fields = flds)
print d