<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import datetime <EOL> import math <EOL> import six <EOL> from pytz import UTC <EOL> from . import text <EOL> from . datetime import ensure_datetime <EOL> class BoolFormatter : <EOL> def __init__ ( self , true = '<STR_LIT:True>' , false = '<STR_LIT:False>' , size = None , pad_left = False ) : <EOL> if size is None : <EOL> size = max ( len ( true ) , len ( false ) ) <EOL> self . __true = true <EOL> self . __false = false <EOL> self . __size = size <EOL> self . __pad_left = pad_left <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __size <EOL> @ property <EOL> def size ( self ) : <EOL> return self . __size <EOL> @ property <EOL> def pad_left ( self ) : <EOL> return self . __pad_left <EOL> def changing ( self , ** kw_args ) : <EOL> args = dict ( <EOL> size = self . __size , <EOL> pad_left = self . __pad_left , <EOL> ) <EOL> args . update ( kw_args ) <EOL> return self . __class__ ( self . __true , self . __false , ** args ) <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return text . palide ( <EOL> self . __true if value else self . __false , self . __size , <EOL> ellipsis = "<STR_LIT>" , <EOL> pad = "<STR_LIT:U+0020>" , <EOL> position = <NUM_LIT:1.0> , <EOL> left = self . __pad_left ) <EOL> __call__ = format <EOL> class IntFormatter : <EOL> def __init__ ( self , size , pad = "<STR_LIT:U+0020>" , sign = "<STR_LIT:->" ) : <EOL> """<STR_LIT>""" <EOL> assert size >= <NUM_LIT:0> <EOL> assert pad in ( "<STR_LIT:0>" , "<STR_LIT:U+0020>" ) <EOL> assert sign in ( None , "<STR_LIT:->" , "<STR_LIT:+>" ) <EOL> width = size <EOL> if sign in ( "<STR_LIT:->" , "<STR_LIT:+>" ) : <EOL> width += <NUM_LIT:1> <EOL> self . __size = size <EOL> self . __pad = pad <EOL> self . __sign = sign <EOL> self . __width = width <EOL> @ property <EOL> def size ( self ) : <EOL> return self . __size <EOL> @ property <EOL> def pad ( self ) : <EOL> return self . __pad <EOL> @ property <EOL> def sign ( self ) : <EOL> return self . __sign <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __width <EOL> def changing ( self , ** kw_args ) : <EOL> args = dict ( <EOL> size = self . __size , <EOL> pad = self . __pad , <EOL> sign = self . __sign ) <EOL> args . update ( kw_args ) <EOL> return self . __class__ ( ** args ) <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> sign = ( <EOL> "<STR_LIT>" if self . __sign is None <EOL> else "<STR_LIT:->" if value < <NUM_LIT:0> <EOL> else "<STR_LIT:+>" if self . __sign == "<STR_LIT:+>" <EOL> else "<STR_LIT:U+0020>" ) <EOL> result = str ( abs ( value ) ) <EOL> if len ( result ) > self . __size or value < <NUM_LIT:0> and self . __sign is None : <EOL> result = "<STR_LIT:#>" * self . __width <EOL> elif self . __pad == "<STR_LIT:U+0020>" : <EOL> result = text . pad ( <EOL> sign + result , <EOL> self . __size + len ( sign ) , pad = self . __pad , left = True ) <EOL> else : <EOL> result = sign + text . pad ( <EOL> result , self . __size , pad = self . __pad , left = True ) <EOL> return result <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , six . integer_types ) : <EOL> try : <EOL> value = round ( value ) <EOL> except TypeError : <EOL> pass <EOL> value = int ( value ) <EOL> return self . format ( value ) <EOL> class FloatFormatter : <EOL> def __init__ ( self , size , precision , pad = "<STR_LIT:U+0020>" , sign = "<STR_LIT:->" , point = "<STR_LIT:.>" , <EOL> nan_str = "<STR_LIT>" , inf_str = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> assert size >= <NUM_LIT:0> <EOL> assert precision is None or precision >= <NUM_LIT:0> <EOL> assert pad in ( "<STR_LIT:0>" , "<STR_LIT:U+0020>" ) <EOL> assert sign in ( None , "<STR_LIT:->" , "<STR_LIT:+>" ) <EOL> width = size <EOL> if precision is not None : <EOL> width += len ( point ) + precision <EOL> nan_str = nan_str [ : width ] <EOL> inf_str = inf_str [ : width ] <EOL> if sign in ( "<STR_LIT:->" , "<STR_LIT:+>" ) : <EOL> width += <NUM_LIT:1> <EOL> self . __size = size <EOL> self . __precision = precision <EOL> self . __pad = pad <EOL> self . __sign = sign <EOL> self . __point = point <EOL> self . __nan_str = nan_str <EOL> self . __inf_str = inf_str <EOL> self . __multiplier = None if precision is None else <NUM_LIT:10> ** precision <EOL> self . __width = width <EOL> @ property <EOL> def size ( self ) : <EOL> return self . __size <EOL> @ property <EOL> def precision ( self ) : <EOL> return self . __precision <EOL> @ property <EOL> def pad ( self ) : <EOL> return self . __pad <EOL> @ property <EOL> def sign ( self ) : <EOL> return self . __sign <EOL> @ property <EOL> def point ( self ) : <EOL> return self . __point <EOL> @ property <EOL> def nan_str ( self ) : <EOL> return self . __nan_str <EOL> @ property <EOL> def inf_str ( self ) : <EOL> return self . __inf_str <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __width <EOL> def changing ( self , ** kw_args ) : <EOL> args = dict ( <EOL> size = self . __size , <EOL> precision = self . __precision , <EOL> pad = self . __pad , <EOL> sign = self . __sign , <EOL> point = self . __point , <EOL> nan_str = self . __nan_str , <EOL> inf_str = self . __inf_str ) <EOL> args . update ( kw_args ) <EOL> return self . __class__ ( ** args ) <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> sign = ( <EOL> "<STR_LIT>" if self . __sign is None <EOL> else "<STR_LIT:->" if value < <NUM_LIT:0> <EOL> else "<STR_LIT:+>" if self . __sign == "<STR_LIT:+>" <EOL> else "<STR_LIT:U+0020>" ) <EOL> if math . isnan ( value ) : <EOL> result = text . pad ( self . __nan_str , self . __width , pad = "<STR_LIT:U+0020>" , left = True ) <EOL> elif value < <NUM_LIT:0> and self . __sign is None : <EOL> result = "<STR_LIT:#>" * self . __width <EOL> elif math . isinf ( value ) : <EOL> result = text . pad ( <EOL> sign + self . __inf_str , self . __width , pad = "<STR_LIT:U+0020>" , left = True ) <EOL> else : <EOL> precision = <NUM_LIT:0> if self . __precision is None else self . __precision <EOL> rnd_value = round ( value , precision ) <EOL> abs_value = abs ( rnd_value ) <EOL> int_value = int ( abs_value ) <EOL> result = str ( int_value ) <EOL> if len ( result ) > self . __size : <EOL> return "<STR_LIT:#>" * self . __width <EOL> if self . __pad == "<STR_LIT:U+0020>" : <EOL> result = text . pad ( <EOL> sign + result , <EOL> self . __size + len ( sign ) , pad = self . __pad , left = True ) <EOL> else : <EOL> result = sign + text . pad ( <EOL> result , self . __size , pad = self . __pad , left = True ) <EOL> if self . __precision is None : <EOL> pass <EOL> elif self . __precision == <NUM_LIT:0> : <EOL> result += self . __point <EOL> else : <EOL> frac = int ( round ( ( abs_value - int_value ) * self . __multiplier ) ) <EOL> frac = str ( frac ) <EOL> assert len ( frac ) <= precision <EOL> frac = text . pad ( frac , self . __precision , pad = "<STR_LIT:0>" , left = True ) <EOL> result += self . __point + frac <EOL> return result <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . format ( float ( value ) ) <EOL> class EFloatFormatter : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , size , precision , sign = "<STR_LIT:->" , point = "<STR_LIT:.>" , exp = "<STR_LIT:E>" , <EOL> nan_str = "<STR_LIT>" , inf_str = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> assert size > <NUM_LIT:0> <EOL> assert precision is None or precision >= <NUM_LIT:0> <EOL> assert sign in ( None , "<STR_LIT:->" , "<STR_LIT:+>" ) <EOL> width = ( <EOL> <NUM_LIT:1> + ( <NUM_LIT:0> if precision is None else len ( point ) + precision ) <EOL> + len ( exp ) + <NUM_LIT:1> + size ) <EOL> nan_str = nan_str [ : width ] <EOL> inf_str = inf_str [ : width ] <EOL> if sign in ( "<STR_LIT:->" , "<STR_LIT:+>" ) : <EOL> width += <NUM_LIT:1> <EOL> self . __size = size <EOL> self . __precision = precision <EOL> self . __sign = sign <EOL> self . __point = point <EOL> self . __exp = exp <EOL> self . __nan_str = nan_str <EOL> self . __inf_str = inf_str <EOL> self . __multiplier = None if precision is None else <NUM_LIT:10> ** precision <EOL> self . __width = width <EOL> @ property <EOL> def size ( self ) : <EOL> return self . __size <EOL> @ property <EOL> def precision ( self ) : <EOL> return self . __precision <EOL> @ property <EOL> def sign ( self ) : <EOL> return self . __sign <EOL> @ property <EOL> def point ( self ) : <EOL> return self . __point <EOL> @ property <EOL> def exp ( self ) : <EOL> return self . __exp <EOL> @ property <EOL> def nan_str ( self ) : <EOL> return self . __nan_str <EOL> @ property <EOL> def inf_str ( self ) : <EOL> return self . __inf_str <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __width <EOL> def changing ( self , ** kw_args ) : <EOL> args = dict ( <EOL> precision = self . __precision , <EOL> size = self . __size , <EOL> sign = self . __sign , <EOL> point = self . __point , <EOL> exp = self . __exp , <EOL> nan_str = self . __nan_str , <EOL> inf_str = self . __inf_str ) <EOL> args . update ( kw_args ) <EOL> return self . __class__ ( ** args ) <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> sign = ( <EOL> "<STR_LIT>" if self . __sign is None <EOL> else "<STR_LIT:->" if value < <NUM_LIT:0> <EOL> else "<STR_LIT:+>" if self . __sign == "<STR_LIT:+>" <EOL> else "<STR_LIT:U+0020>" ) <EOL> if math . isnan ( value ) : <EOL> result = text . pad ( self . __nan_str , self . __width , pad = "<STR_LIT:U+0020>" , left = True ) <EOL> elif value < <NUM_LIT:0> and self . __sign is None : <EOL> result = "<STR_LIT:#>" * self . __width <EOL> elif math . isinf ( value ) : <EOL> result = text . pad ( <EOL> sign + self . __inf_str , self . __width , pad = "<STR_LIT:U+0020>" , left = True ) <EOL> else : <EOL> if value == <NUM_LIT:0> : <EOL> exp = <NUM_LIT:0> <EOL> mantissa = <NUM_LIT:0> <EOL> else : <EOL> abs_value = abs ( value ) <EOL> precision = <NUM_LIT:0> if self . __precision is None else self . __precision <EOL> rnd_value = round ( <EOL> abs_value , <EOL> int ( math . ceil ( precision - math . log10 ( abs_value ) ) ) ) <EOL> exp = int ( math . floor ( math . log10 ( rnd_value ) ) ) <EOL> mantissa = rnd_value / <NUM_LIT:10> ** exp <EOL> int_value = int ( mantissa ) <EOL> result = sign + str ( int_value ) <EOL> if self . __precision is None : <EOL> pass <EOL> elif self . __precision == <NUM_LIT:0> : <EOL> result += self . __point <EOL> else : <EOL> frac = int ( round ( ( mantissa - int_value ) * <NUM_LIT:10> ** self . __precision ) ) <EOL> frac = text . pad ( str ( frac ) , self . __precision , pad = "<STR_LIT:0>" , left = True ) <EOL> result += self . __point + frac <EOL> result += self . __exp <EOL> result += "<STR_LIT:+>" if exp >= <NUM_LIT:0> else "<STR_LIT:->" <EOL> exp = text . pad ( str ( abs ( exp ) ) , self . __size , pad = "<STR_LIT:0>" , left = True ) <EOL> if len ( exp ) > self . __size : <EOL> result = "<STR_LIT:#>" * self . __width <EOL> else : <EOL> result += exp <EOL> return result <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . format ( float ( value ) ) <EOL> class StrFormatter : <EOL> def __init__ ( self , size , ellipsis = "<STR_LIT>" , position = <NUM_LIT:1.0> , pad = "<STR_LIT:U+0020>" , <EOL> pad_left = False ) : <EOL> """<STR_LIT>""" <EOL> self . __size = size <EOL> self . __ellipsis = ellipsis <EOL> self . __pad = pad <EOL> self . __position = position <EOL> self . __pad_left = pad_left <EOL> @ property <EOL> def size ( self ) : <EOL> return self . __size <EOL> @ property <EOL> def ellipsis ( self ) : <EOL> return self . __ellipsis <EOL> @ property <EOL> def pad ( self ) : <EOL> return self . __pad <EOL> @ property <EOL> def position ( self ) : <EOL> return self . __position <EOL> @ property <EOL> def pad_left ( self ) : <EOL> return self . __pad_left <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __size <EOL> def changing ( self , ** kw_args ) : <EOL> args = dict ( <EOL> size = self . __size , <EOL> ellipsis = self . __ellipsis , <EOL> pad = self . __pad , <EOL> position = self . __position , <EOL> pad_left = self . __pad_left ) <EOL> args . update ( kw_args ) <EOL> return self . __class__ ( ** args ) <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return text . palide ( <EOL> str ( value ) , self . __size , <EOL> ellipsis = self . __ellipsis [ : self . __size ] , <EOL> pad = self . __pad , <EOL> position = self . __position , <EOL> left = self . __pad_left ) <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . format ( str ( value ) ) <EOL> DATETIME_FORMATS = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> class DatetimeFormatter : <EOL> def __init__ ( self , spec = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> dt = datetime . datetime ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:7> , <NUM_LIT> , <NUM_LIT> , UTC ) <EOL> spec = DATETIME_FORMATS . get ( spec , spec ) <EOL> width = len ( format ( dt , spec ) ) <EOL> self . __spec = spec <EOL> self . __width = width <EOL> @ property <EOL> def width ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __width <EOL> def format ( self , value ) : <EOL> """<STR_LIT>""" <EOL> result = format ( value , self . __spec ) <EOL> return result <EOL> def __call__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return self . format ( ensure_datetime ( value ) ) </s>
