Closed ant5 closed 6 years ago
>>> import trafaret as t
>>> must_be_a_list = lambda trafaret: t.List(trafaret) | trafaret & (lambda x: [x])
>>> must_be_a_list(t.String)('z')
['z']
>>> must_be_a_list(t.String)(['z', 'f'])
['z', 'f']
Thank you for suggestion. But it seem that in my case the solution does not fit perfectly. I'm reading data specification defined as trafaret from config file and evaluating it to get real trafaret object.
Example config : a = trafaret instance String(min_length=3) b = trafaret instance List(String()) c = trafaret instance Regexp("^\d{15}$")
I get things after words 'trafaret instance' and evaluate it with environment containing 'trafaret' module. Evaluation give me a trafaret object wich I use for data validation.
I still can't understand what to write after words 'trafaret instance' to get a trafaret which convert individual value to List.
Trafaret can not contain all trafarets that you will ever need, so it seems correct way is to create custom one and populate it to your environment to evaluation.
Probably you are right.
Sorry for disturbing again.
I wonder is it possible to convet individually supplied value to a list.
I want to do something like: t = trafaret.List(trafaret.String()) t(['a','b'])
It will be amazingly cosily to pass FieldStorage.getvalue() directly to trafaret.