Mc@sdZddlZddlZyddlmZeZWnek rQeZnXddl m Z m Z ddl m Z dddd d d d d gZdZe dZedZedZedZedZedZedZedZedZdedZdedZdedZdedZd efdYZedZ edZ!dZ"d Z#d!e"Z$d"e#Z%d#Z&ej'd$e$e%e&fZ(d%Z)d&d'Z*d(Z+d)e,fd*YZ-dS(+sLocale dependent formatting and parsing of numeric data. The default locale for the functions in this module is determined by the following environment variables, in that order: * ``LC_NUMERIC``, * ``LC_ALL``, and * ``LANG`` iN(tDecimal(tdefault_localetLocale(trsplitt format_numbertformat_decimaltformat_currencytformat_percenttformat_scientifict parse_numbert parse_decimaltNumberFormatErrorsrestructuredtext ent LC_NUMERICcCstj|jj||S(s<Return the name used by the locale for the specified currency. >>> get_currency_name('USD', 'en_US') u'US Dollar' :param currency: the currency code :param locale: the `Locale` object or locale identifier :return: the currency symbol :rtype: `unicode` :since: version 0.9.4 (Rtparset currenciestget(tcurrencytlocale((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_currency_name,s cCstj|jj||S(sReturn the symbol used by the locale for the specified currency. >>> get_currency_symbol('USD', 'en_US') u'$' :param currency: the currency code :param locale: the `Locale` object or locale identifier :return: the currency symbol :rtype: `unicode` (RR tcurrency_symbolsR(RR((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_currency_symbol:s cCstj|jjddS(sReturn the symbol used by the locale to separate decimal fractions. >>> get_decimal_symbol('en_US') u'.' :param locale: the `Locale` object or locale identifier :return: the decimal symbol :rtype: `unicode` tdecimalu.(RR tnumber_symbolsR(R((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_decimal_symbolGs cCstj|jjddS(sReturn the plus sign symbol used by the current locale. >>> get_plus_sign_symbol('en_US') u'+' :param locale: the `Locale` object or locale identifier :return: the plus sign symbol :rtype: `unicode` tplusSignu+(RR RR(R((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_plus_sign_symbolSs cCstj|jjddS(sReturn the plus sign symbol used by the current locale. >>> get_minus_sign_symbol('en_US') u'-' :param locale: the `Locale` object or locale identifier :return: the plus sign symbol :rtype: `unicode` t minusSignu-(RR RR(R((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_minus_sign_symbol_s cCstj|jjddS(sReturn the symbol used by the locale to separate mantissa and exponent. >>> get_exponential_symbol('en_US') u'E' :param locale: the `Locale` object or locale identifier :return: the exponential symbol :rtype: `unicode` t exponentialuE(RR RR(R((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_exponential_symbolks cCstj|jjddS(sReturn the symbol used by the locale to separate groups of thousands. >>> get_group_symbol('en_US') u',' :param locale: the `Locale` object or locale identifier :return: the group symbol :rtype: `unicode` tgroupu,(RR RR(R((s1/usr/lib/python2.7/site-packages/babel/numbers.pytget_group_symbolws cCst|d|S(sReturn the given number formatted for a specific locale. >>> format_number(1099, locale='en_US') u'1,099' :param number: the number to format :param locale: the `Locale` object or locale identifier :return: the formatted number :rtype: `unicode` R(R(tnumberR((s1/usr/lib/python2.7/site-packages/babel/numbers.pyRs cCsFtj|}|s*|jj|}nt|}|j||S(sReturn the given decimal number formatted for a specific locale. >>> format_decimal(1.2345, locale='en_US') u'1.234' >>> format_decimal(1.2346, locale='en_US') u'1.235' >>> format_decimal(-1.2346, locale='en_US') u'-1.235' >>> format_decimal(1.2345, locale='sv_SE') u'1,234' >>> format_decimal(12345, locale='de') u'12.345' The appropriate thousands grouping and the decimal separator are used for each locale: >>> format_decimal(12345.5, locale='en_US') u'12,345.5' :param number: the number to format :param format: :param locale: the `Locale` object or locale identifier :return: the formatted decimal number :rtype: `unicode` (RR tdecimal_formatsRt parse_patterntapply(R tformatRtpattern((s1/usr/lib/python2.7/site-packages/babel/numbers.pyRs  cCsLtj|}|s*|jj|}nt|}|j||d|S(u~Return formatted currency value. >>> format_currency(1099.98, 'USD', locale='en_US') u'$1,099.98' >>> format_currency(1099.98, 'USD', locale='es_CO') u'US$\xa01.099,98' >>> format_currency(1099.98, 'EUR', locale='de_DE') u'1.099,98\xa0\u20ac' The pattern can also be specified explicitly: >>> format_currency(1099.98, 'EUR', u'¤¤ #,##0.00', locale='en_US') u'EUR 1,099.98' :param number: the number to format :param currency: the currency code :param locale: the `Locale` object or locale identifier :return: the formatted currency value :rtype: `unicode` R(RR tcurrency_formatsRR"R#(R RR$RR%((s1/usr/lib/python2.7/site-packages/babel/numbers.pyRs  cCsFtj|}|s*|jj|}nt|}|j||S(sNReturn formatted percent value for a specific locale. >>> format_percent(0.34, locale='en_US') u'34%' >>> format_percent(25.1234, locale='en_US') u'2,512%' >>> format_percent(25.1234, locale='sv_SE') u'2\xa0512\xa0%' The format pattern can also be specified explicitly: >>> format_percent(25.1234, u'#,##0\u2030', locale='en_US') u'25,123\u2030' :param number: the percent number to format :param format: :param locale: the `Locale` object or locale identifier :return: the formatted percent number :rtype: `unicode` (RR tpercent_formatsRR"R#(R R$RR%((s1/usr/lib/python2.7/site-packages/babel/numbers.pyRs  cCsFtj|}|s*|jj|}nt|}|j||S(sReturn value formatted in scientific notation for a specific locale. >>> format_scientific(10000, locale='en_US') u'1E4' The format pattern can also be specified explicitly: >>> format_scientific(1234567, u'##0E00', locale='en_US') u'1.23E06' :param number: the number to format :param format: :param locale: the `Locale` object or locale identifier :return: value formatted in scientific notation. :rtype: `unicode` (RR tscientific_formatsRR"R#(R R$RR%((s1/usr/lib/python2.7/site-packages/babel/numbers.pyRs  cBseZdZRS(s>Exception raised when a string cannot be parsed into a number.(t__name__t __module__t__doc__(((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR scCsHy t|jt|dSWn!tk rCtd|nXdS(s|Parse localized number string into a long integer. >>> parse_number('1,099', locale='en_US') 1099L >>> parse_number('1.099', locale='de_DE') 1099L When the given string cannot be parsed, an exception is raised: >>> parse_number('1.099,98', locale='de') Traceback (most recent call last): ... NumberFormatError: '1.099,98' is not a valid number :param string: the string to parse :param locale: the `Locale` object or locale identifier :return: the parsed number :rtype: `long` :raise `NumberFormatError`: if the string can not be converted to a number ts%r is not a valid numberN(tlongtreplaceRt ValueErrorR (tstringR((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR s  cCsitj|}y2t|jt|djt|dSWn!tk rdtd|nXdS(sParse localized decimal string into a float. >>> parse_decimal('1,099.98', locale='en_US') 1099.98 >>> parse_decimal('1.099,98', locale='de') 1099.98 When the given string cannot be parsed, an exception is raised: >>> parse_decimal('2,109,998', locale='de') Traceback (most recent call last): ... NumberFormatError: '2,109,998' is not a valid decimal number :param string: the string to parse :param locale: the `Locale` object or locale identifier :return: the parsed decimal number :rtype: `float` :raise `NumberFormatError`: if the string can not be converted to a decimal number R,t.s %r is not a valid decimal numberN(RR tfloatR.RRR/R (R0R((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR s  s [^0-9@#.,]s [0-9@#.\-,E+]s(?P(?:'[^']*'|%s)*)s(?P%s+)s(?P.*)s%s%s%scCstr$t|tr$t|}nd|jd}d|krs|jdd\}}|dkrd}qn |d}}||fS(s=Convert a number into a (intasstring, fractionasstring) tuples%.9ft0R1iR,(t have_decimalt isinstanceRtstrtrstriptsplit(tvaluettexttatb((s1/usr/lib/python2.7/site-packages/babel/numbers.pyt split_numberEs    ic Cs&t|dkrdpd}t|}t|\}}||}d}t||}|dks|t|kr}nF||dkrd}n-||dkr||ddkrd}nd|}trt|trtt|||||Stt|||||SdS(sCRound a number to a given precision. Works like round() except that the round-half-even (banker's rounding) algorithm is used instead of round-half-up. >>> bankersround(5.5, 0) 6.0 >>> bankersround(6.5, 0) 6.0 >>> bankersround(-6.5, 0) -6.0 >>> bankersround(1234.0, -2) 1200.0 iiit5t13579i N(tinttabsR=tlenR4R5RR2( R9tndigitstsignR;R<tdigitstaddtitscale((s1/usr/lib/python2.7/site-packages/babel/numbers.pyt bankersroundSs    $   c Cst|tr|Sd|krv|jdd\}}tj|j\}}}tj|j\}}}n.tj|j\}}}d|}|}d|kr|jdd\}}nd}d|krd|krd|krtdqnd|kr.t|dd\} } n |} d } d } } d } d }| | }| | }|r| | | }|j d }|j d }| |}n d}d}|| }t|||f||f|||||S(sParse number format patternst;it-tEt@R1R3s5Significant digit patterns can not contain "@" or "0"R,icSstd}}x]|D]U}|dkr:|d7}|d7}q|dkrS|d7}q|dkreqqPqW||fS(s(Calculate the min and max allowed digitsis@0it#t,((tptmintmaxtc((s1/usr/lib/python2.7/site-packages/babel/numbers.pytparse_precisions        cSst|}|jd}|dkr+dS||d}|| d jd}|dkrg||fS|||d}||fS(sParse primary and secondary digit grouping >>> parse_grouping('##') 0, 0 >>> parse_grouping('#,###') 3, 3 >>> parse_grouping('#,####,###') 3, 4 ROiiii(ii(RBtrfind(RPtwidthtg1tg2((s1/usr/lib/python2.7/site-packages/babel/numbers.pytparse_groupings    t+N( R5t NumberPatternR8t number_retsearchtgroupstNoneR/Rt startswithtlstrip(R%t neg_patternt pos_prefixR t pos_suffixt neg_prefixt_t neg_suffixtexptintegertfractiontmin_fractmax_fracRTRYtint_prect frac_prectexp_plustexp_prectgrouping((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR"tsH !           R[cBsAeZdZdZddZdZdZdZRS(c Cs||_||_||_||_||_||_||_||_ddj|j|jkrsd|_ n4ddj|j|jkrd|_ n d|_ dS(Nt%R,idu‰ii( R%tprefixtsuffixRqRmRnRpRotjoinRH( tselfR%RsRtRqRmRnRpRo((s1/usr/lib/python2.7/site-packages/babel/numbers.pyt__init__s          cCsdt|j|jfS(Ns<%s %r>(ttypeR)R%(Rv((s1/usr/lib/python2.7/site-packages/babel/numbers.pyt__repr__sc CsP||j9}t|dk}|jrt|}|r^ttjtj|d}nd}|jd|jdkr||jdd8}n2|jdrt||jd|jd}nt st |t  rt |}n|dkr |d| }n|d|}d}|dkr;t |}n|j rSt|}nt|}d|j||jd|jdt|||jt||jd|jd|f}n(d|jkrs|jt||jd|jd}d|krX|jd\} } |j| dd|} | rKt|| } n| | }q|j|dd|}nvttt||jd\} } | pd } |j| |jd|jd|} |j| |} | | }d |j|||j|f} d | krL| jd |j} | jd t||} n| S( Nii iR,u%s%s%s%sRMR1iR3u%s%s%su¤u¤¤(RHR@RpRAtmathtfloortlogRmR4R5RR2RRoRt_format_sigdigRnRt _format_intR6R%R8RR=RIt _format_fracRsRtR.tupperR( RvR9RRt is_negativeRhtexp_signR R:R;R<tretval((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR#sh   $ %            cCs t|\}}t|}|dkri|dkrid}x*|jdre|d}|d8}q?Wntt|||\}}t||jd}|sd}nt||kr|dkr|S||kr|d||7}n|rd||fS|S(sConvert value to a string. The resulting string will contain between (min, max) number of significant digits. R3R,iis%s.%s(R=RBR`RIRa(RvR9RQRRR;R<t ndecimalsRE((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR}s$    c Cst|}||kr-d|||}n|jd}d}t|}xAt||kr||| |}|| }|jd}qOW||S(NR3iR,i(RBRqR( RvR9RQRRRRVtgsizetrettsymbol((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR~2s     cCs|j\}}t||kr<|d|t|7}n|dksf|dkrjt|dkrjdSt|}x0t||kr|ddkr|d }qyWt||S(NR3iR,i(RnRBR@R(RvR9RRQRRRV((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR?s* %N( R)R*RwRyR_R#R}R~R(((s1/usr/lib/python2.7/site-packages/babel/numbers.pyR[s    <  (.R+RztreRRtTrueR4t ImportErrortFalset babel.coreRRt babel.utilRt__all__t __docformat__R RRRRRRRRR_RRRRR/R R R t PREFIX_ENDt NUMBER_TOKENtPREFIX_PATTERNtNUMBER_PATTERNtSUFFIX_PATTERNtcompileR\R=RIR"tobjectR[(((s1/usr/lib/python2.7/site-packages/babel/numbers.pytsP                   ! R