#NEWLINE# def get_format_fn( self ) : #NEWLINE# #INDENT# NO_DATA_TEXT = '--' #NEWLINE# CANT_CALCULATE_TEXT = '?' #NEWLINE# class NoData ( Exception ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# class BadData ( Exception ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# def trap_errors( fn ) : #NEWLINE# #INDENT# def inner( * args , **kwargs ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return fn ( * args , ** kwargs ) #NEWLINE# #UNINDENT# except BadData : #NEWLINE# #INDENT# return CANT_CALCULATE_TEXT #NEWLINE# #UNINDENT# except NoData : #NEWLINE# #INDENT# return NO_DATA_TEXT #NEWLINE# #UNINDENT# #UNINDENT# return inner #NEWLINE# #UNINDENT# def _raw( data ) : #NEWLINE# #INDENT# if data [ 'denom' ] : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return round ( ( float ( data [ 'num' ] ) / float ( data [ 'denom' ] ) ) , 3 ) #NEWLINE# #UNINDENT# except ( ValueError , TypeError ) : #NEWLINE# #INDENT# raise BadData ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise NoData ( ) #NEWLINE# #UNINDENT# #UNINDENT# def _raw_pct( data , round_type = float ) : #NEWLINE# #INDENT# return round_type ( ( _raw ( data ) * 100 ) ) #NEWLINE# #UNINDENT# @ trap_errors #NEWLINE# def _clean_raw( data ) : #NEWLINE# #INDENT# return _raw ( data ) #NEWLINE# #UNINDENT# @ trap_errors #NEWLINE# def _numeric_pct( data ) : #NEWLINE# #INDENT# return _raw_pct ( data , round_type = int ) #NEWLINE# #UNINDENT# @ trap_errors #NEWLINE# def _pct( data ) : #NEWLINE# #INDENT# return '{0:.0f}%' . format ( _raw_pct ( data ) ) #NEWLINE# #UNINDENT# _fraction = ( lambda data : '{num}/{denom}' . format ( ** data ) ) #NEWLINE# return { #NEWLINE# #INDENT# 'percent' : _pct , #NEWLINE# 'fraction' : _fraction , #NEWLINE# 'both' : ( lambda data : '{} ({})' . format ( _pct ( data ) , _fraction ( data ) ) ) , #NEWLINE# 'numeric_percent' : _numeric_pct , #NEWLINE# 'decimal' : _clean_raw , #NEWLINE# #UNINDENT# } [ self . format ]