Why don't you manage None values in int_to_hms function?

Il giorno giovedì 25 ottobre 2012 21:56:17 UTC+2, Joe Barnhart ha scritto:
>
> Actually Paolo, I have a custom Validator which contains the formatter 
> function.  It is supplied automatically when I use the validator, so I do 
> not get a chance to change its calling sequence as you show in the case of 
> using "represents".  Here is the validator class:
>
> class IS_ELAPSED_TIME(object):
>     def __init__(self,error_message='Must be MM:SS.hh or MMSShh (with no 
> punctuation)'):
>         self.error_message=error_message
>     def __call__(self,value):
>         try:
>             if value and value.upper() != 'NT':
>                 res = hms_to_int(value)
>             else:
>                 res = None
>             return (res,None)
>         except:
>             return (value,self.error_message)
>     def formatter(self,value):
>         if value:
>             rtn = int_to_hms(value)
>         else:
>             rtn = 'NT'
>         return rtn
>
> Since the Field is declared to use this validator, I have no explicit 
> "formatter=" statement I cannot easily employ your solution.  I do not 
> understand why "None" is not allowed to be formatted in the first place.
>
> -- Joe B.
>
> On Thursday, October 25, 2012 6:07:09 AM UTC-7, Paolo Caruccio wrote:
>>
>> or better
>>
>> def format_function (value)
>>      formatted_value = .....
>>      return formatted_value
>>
>> db.tablename.fieldname.represent= lambda value,row: format_function(value
>> ) if value else "Not Standard Time"
>>
>>
>>
>>
>> Il giorno giovedì 25 ottobre 2012 13:31:43 UTC+2, Paolo Caruccio ha 
>> scritto:
>>>
>>> Did you try:
>>>
>>> db.tablename.fieldname.represent= lambda value: value if value else 'NT"
>>>
>>> ?
>>> web2py book reference 
>>> http://web2py.com/books/default/chapter/29/06?search=represent#Record-representation
>>>
>>>
>>> Il giorno giovedì 25 ottobre 2012 03:20:29 UTC+2, Joe Barnhart ha 
>>> scritto:
>>>>
>>>> I have an application where I expect "None" items in my database and I 
>>>> want to format them to "NT".  It is an app that uses time standards, and 
>>>> if 
>>>> there is no standard present I expect a "None" in the database which 
>>>> translates to a field of "No Time" or "NT".
>>>>
>>>> The problem is that the current implementation of formatter in the 
>>>> Field class tests the value for "None" and escapes before the formatter is 
>>>> called.
>>>>
>>>> I can see why this behavior might be expected in a lot of cases, but it 
>>>> seems extreme to deny the ability to format "None" into a more pleasing 
>>>> form for those applications that could benefit from it.  Here is the 
>>>> offending part of formatter (located in gluon/dal.py):
>>>>
>>>>     def formatter(self, value):
>>>>         requires = self.requires
>>>>         if value is None or not requires:
>>>>             return value
>>>>
>>>> If I change the above to:
>>>>
>>>>     def formatter(self, value):
>>>>         requires = self.requires
>>>>         if not requires:
>>>>             return value
>>>>
>>>> I get my desired behavior, which is to pass "None" to my formatter 
>>>> which is implemented as part of a custom Validator object.  I realize the 
>>>> code now has to go "further" for cases where the value is None, but is it 
>>>> really safe to assume nobody will ever want to "format" None into another 
>>>> string?  Not in my case, at least!
>>>>
>>>> Joe B.
>>>>
>>>>
>>>>

-- 



Reply via email to