#NEWLINE# def coalesce_value( value , storage_type , empty_values = None , strip = False ) : #NEWLINE# #INDENT# 'Coalesces `value` to given storage `type`. `empty_values` is a dictionary\n    where keys are storage type names and values are values to be used\n    as empty value replacements.' #NEWLINE# if ( empty_values is None ) : #NEWLINE# #INDENT# empty_values = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( storage_type in [ 'string' , 'text' ] ) : #NEWLINE# #INDENT# if strip : #NEWLINE# #INDENT# value = value . strip ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if value : #NEWLINE# #INDENT# value = unicode ( value ) #NEWLINE# #UNINDENT# #UNINDENT# if ( ( value == '' ) or ( value is None ) ) : #NEWLINE# #INDENT# value = empty_values . get ( 'string' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( storage_type == 'integer' ) : #NEWLINE# #INDENT# if strip : #NEWLINE# #INDENT# value = re . sub ( '\\s' , '' , value . strip ( ) ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# value = int ( value ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# value = empty_values . get ( 'integer' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( storage_type == 'float' ) : #NEWLINE# #INDENT# if strip : #NEWLINE# #INDENT# value = re . sub ( '\\s' , '' , value . strip ( ) ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# value = float ( value ) #NEWLINE# #UNINDENT# except ValueError : #NEWLINE# #INDENT# value = empty_values . get ( 'float' ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( storage_type == 'list' ) : #NEWLINE# #INDENT# value = value . split ( ',' ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return value