ó ÒðQc@s¶dZddlmZmZddlZddlZddlmZmZddl m Z edƒZ de d„Z e d„Ze d „Ze d „Ze d „Ze d „Ze d „Ze d„Zde d„Zde d„Zde d„Zde d„Zdefd„ƒYZe d„Ze d„ZdZdZdeZdeZ dZ!ej"dee e!fƒZ#d„Z$dd„Z%d „Z&d!„Z'd"e(fd#„ƒYZ)dS($s‚ babel.numbers ~~~~~~~~~~~~~ Locale 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`` :copyright: (c) 2013 by the Babel Team. :license: BSD, see LICENSE for more details. iÿÿÿÿ(tDecimaltInvalidOperationN(tdefault_localetLocale(t range_typet LC_NUMERICcCsetj|ƒ}|dk rR|j|ƒ}|jd}||krR|||Sn|jj||ƒS(s‘Return the name used by the locale for the specified currency. >>> get_currency_name('USD', locale='en_US') u'US Dollar' .. versionadded:: 0.9.4 :param currency: the currency code :param count: the optional count. If provided the currency name will be pluralized to that number if possible. :param locale: the `Locale` object or locale identifier tcurrency_names_pluralN(RtparsetNonet plural_formt_datat currenciestget(tcurrencytcounttlocaletlocR t plural_names((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_currency_name s    cCstj|ƒjj||ƒS(sæReturn the symbol used by the locale for the specified currency. >>> get_currency_symbol('USD', locale='en_US') u'$' :param currency: the currency code :param locale: the `Locale` object or locale identifier (RRtcurrency_symbolsR (R R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_currency_symbol6s cCstj|ƒjjddƒS(s³Return the symbol used by the locale to separate decimal fractions. >>> get_decimal_symbol('en_US') u'.' :param locale: the `Locale` object or locale identifier tdecimalu.(RRtnumber_symbolsR (R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_decimal_symbolBscCstj|ƒjjddƒS(s©Return the plus sign symbol used by the current locale. >>> get_plus_sign_symbol('en_US') u'+' :param locale: the `Locale` object or locale identifier tplusSignu+(RRRR (R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_plus_sign_symbolMscCstj|ƒjjddƒS(sªReturn the plus sign symbol used by the current locale. >>> get_minus_sign_symbol('en_US') u'-' :param locale: the `Locale` object or locale identifier t minusSignu-(RRRR (R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_minus_sign_symbolXscCstj|ƒjjddƒS(s»Return 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 t exponentialuE(RRRR (R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_exponential_symbolcscCstj|ƒjjddƒS(s³Return 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 tgroupu,(RRRR (R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytget_group_symbolnscCst|d|ƒS(uReturn the given number formatted for a specific locale. >>> format_number(1099, locale='en_US') u'1,099' >>> format_number(1099, locale='de_DE') u'1.099' :param number: the number to format :param locale: the `Locale` object or locale identifier R(tformat_decimal(tnumberR((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt format_numberys cCsFtj|ƒ}|s*|jj|ƒ}nt|ƒ}|j||ƒS(uŠReturn 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(1.2345, locale='de') u'1,234' 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 (RRtdecimal_formatsR t parse_patterntapply(R!tformatRtpattern((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyR ‰s  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'1.099,98\xa0US$' >>> format_currency(1099.98, 'EUR', locale='de_DE') u'1.099,98\xa0\u20ac' The pattern can also be specified explicitly. The currency is placed with the '¤' sign. As the sign gets repeated the format expands (¤ being the symbol, ¤¤ is the currency abbreviation and ¤¤¤ is the full name of the currency): >>> format_currency(1099.98, 'EUR', u'¤¤ #,##0.00', locale='en_US') u'EUR 1,099.98' >>> format_currency(1099.98, 'EUR', u'#,##0.00 ¤¤¤', locale='en_US') u'1,099.98 euros' :param number: the number to format :param currency: the currency code :param locale: the `Locale` object or locale identifier R (RRtcurrency_formatsR R$R%(R!R R&RR'((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytformat_currency¨s  cCsFtj|ƒ}|s*|jj|ƒ}nt|ƒ}|j||ƒS(sReturn 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 (RRtpercent_formatsR R$R%(R!R&RR'((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytformat_percentÇs  cCsFtj|ƒ}|s*|jj|ƒ}nt|ƒ}|j||ƒS(s†Return 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 (RRtscientific_formatsR R$R%(R!R&RR'((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytformat_scientificás  tNumberFormatErrorcBseZdZRS(s>Exception raised when a string cannot be parsed into a number.(t__name__t __module__t__doc__(((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyR.÷scCsHy t|jt|ƒdƒƒSWn!tk rCtd|ƒ‚nXdS(sSParse localized number string into an integer. >>> parse_number('1,099', locale='en_US') 1099 >>> parse_number('1.099', locale='de_DE') 1099 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 :raise `NumberFormatError`: if the string can not be converted to a number ts%r is not a valid numberN(tinttreplaceRt ValueErrorR.(tstringR((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt parse_numberûs  cCsitj|ƒ}y2t|jt|ƒdƒjt|ƒdƒƒSWn!tk rdtd|ƒ‚nXdS(s„Parse localized decimal string into a decimal. >>> parse_decimal('1,099.98', locale='en_US') Decimal('1099.98') >>> parse_decimal('1.099,98', locale='de') Decimal('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 :raise NumberFormatError: if the string can not be converted to a decimal number R2t.s %r is not a valid decimal numberN(RRRR4RRRR.(R6R((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt parse_decimals  s [^0-9@#.,]s [0-9@#.\-,E+]s(?P(?:'[^']*'|%s)*)s(?P%s+)s(?P.*)s%s%s%sc Cs‰t|tƒr#|jƒ\}}}g}g}ttt|ƒƒ}x>t| ƒD]/}|ru|j|jƒƒqS|jdƒqSWx!t|ƒD]}|jdƒq“Wx|rÉ|j|jƒƒq­Wt |ƒdkråd}n|rû|jdƒndj t |ƒƒdj t |ƒƒfSd|j dƒ}d|krr|j ddƒ\}} | dkrd} qn |d}} || fS( s=Convert a number into a (intasstring, fractionasstring) tuplet0it-R2s%.9fR8i(R:(t isinstanceRtas_tupletlisttmaptstrtrangetappendtpoptlentjointreversedtrstriptsplit( tvaluetsigntdigitstexptint_partt frac_parttittexttatb((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt split_number;s2  (    ic Csqt|dkƒrdpd}t|ƒ}t|ƒ\}}||}d}t|ƒ|}|dks|t|ƒkr}n—||dkr–d}n~||dkrÃ||ddkrÃd}nQ||dkrx>t|dt|ƒƒD] }||dkríd}PqíqíWnd|} t|tƒrMtt|| |ƒƒ| |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 iiÿÿÿÿit5t13579R:i N(R3tabsRSRDRR<Rtfloat( RItndigitsRJRQRRRKtaddROtjtscale((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt bankersroundks*   $     cCsƒt|ƒ}|jdƒ}|dkr+dS||d}|| d jdƒ}|dkrg||fS|||d}||fS(s¹Parse primary and secondary digit grouping >>> parse_grouping('##') (1000, 1000) >>> parse_grouping('#,###') (3, 3) >>> parse_grouping('#,####,###') (3, 4) t,iÿÿÿÿièii(ièiè(RDtrfind(tptwidthtg1tg2((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytparse_grouping”s    c CsÏt|tƒr|Sd„}d|krm|jddƒ\}}||ƒ\}}}||ƒ\}}}n%||ƒ\}}}d|}|}d|kr¹|jddƒ\}} nd } d|krõd|krõd|krõtd ƒ‚qõnd|kr|jddƒ\} } n |} d } d „} | | ƒ} | | ƒ}| rŒ| | | ƒ}| jd ƒ}| jd ƒ} | | ƒ}n d }d }t| ƒ}t|||f||f|| |||ƒS(sParse number format patternscSs8tj|ƒ}|dkr.td|ƒ‚n|jƒS(NsInvalid number pattern %r(t number_retsearchRR5tgroups(R'trv((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt _match_number¯s t;iR;tEt@R8R:s5Significant digit patterns can not contain "@" or "0"R2cSstd}}x]|D]U}|dkr:|d7}|d7}q|dkrS|d7}q|dkreqqPqW||fS(s(Calculate the min and max allowed digitsis@0it#R]((R_tmintmaxtc((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytparse_precisionÌs        t+N( R<t NumberPatternRHRR5trsplitt startswithtlstripRc(R'Rht neg_patternt pos_prefixR!t pos_suffixt neg_prefixt_t neg_suffixRLtintegertfractionRptint_prect frac_prectexp_plustexp_prectgrouping((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyR$ªsF           RrcBsAeZd„Zd„Zdd„Zd„Zd„Zd„ZRS(c Cs«||_||_||_||_||_||_||_||_ddj|j|jƒkrsd|_ n4ddj|j|jƒkržd|_ n d|_ dS(Nt%R2idu‰ièi( R'tprefixtsuffixR‚R~RRR€RER[( tselfR'R„R…R‚R~RRR€((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt__init__îs          cCsdt|ƒj|jfS(Ns<%s %r>(ttypeR/R'(R†((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyt__repr__ÿsc CsŠt|tƒr$tt|ƒƒ}n||j9}t|dkƒ}|jrÝt|ƒ}|r‚ttj tj |dƒƒƒ}nd}|j d|j dkrº||j dd8}n2|j drìt||j dƒ|j d}nt|tƒs t|ƒ}n|dkr(|d| }n|d|}d}|dkrWt |ƒ}n|j rot|ƒ}nt|ƒ}d|j||jd|jdƒt|ƒ||jt|ƒ|jd|jd|ƒf}n(d|jkr|jt|ƒ|j d|j dƒ}d|krt|jdƒ\} } |j| dd|ƒ} | rgt|ƒ| } n| | }q|j|dd|ƒ}nvttt|ƒ|jdƒƒ\} } | pÀd } |j| |j d|j d|ƒ} |j| |ƒ} | | }d |j|||j|f} d | kr†| jd t|||ƒƒ} | jd |jƒƒ} | jd t||ƒƒ} n| S(Nii iR2u%s%s%s%sRkR8ièR:u%s%s%su¤u¤¤¤u¤¤(R<RWRR@R[R3RRVtmathtfloortlogR~RR€Rt_format_sigdigRRt _format_intR'RHRRSR\t _format_fracR„R…R4RtupperR( R†RIRR t is_negativeRLtexp_signR!RPRQRRtretval((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyR%sp   $ %             cCs t|ƒ\}}t|ƒ}|dkri|dkrid}x*|jdƒre|d}|d8}q?Wntt|||ƒƒ\}}t||jdƒƒ}|s°d}nt|ƒ|krÒ|dkrÒ|S||kró|d||7}n|rd||fS|S(s‡Convert value to a string. The resulting string will contain between (min, max) number of significant digits. R:R2iis%s.%s(RSRDRtR\Ru(R†RIRmRnRQRRt ndecimalsRK((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyRBs$    c Cs˜t|ƒ}||kr-d|||}n|jd}d}t|ƒ}xAt|ƒ|kr||| |}|| }|jd}qOW||S(NR:iR2i(RDR‚R( R†RIRmRnRR`tgsizetrettsymbol((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyRŽ\s     cCs·|j\}}t|ƒ|kr<|d|t|ƒ7}n|dksf|dkrjt|ƒdkrjdSt|ƒ}x0t|ƒ|kr¨|ddkr¨|d }qyWt|ƒ|S(NR:iR2iÿÿÿÿ(RRDR3R(R†RIRRmRnR`((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyRis* %N( R/R0R‡R‰RR%RRŽR(((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pyRrìs    @  (*R1RRRRŠtret babel.coreRRt babel._compatRRRRRRRRRRR"R R)R+R-R5R.R7R9t PREFIX_ENDt NUMBER_TOKENtPREFIX_PATTERNtNUMBER_PATTERNtSUFFIX_PATTERNtcompileRdRSR\RcR$tobjectRr(((s7/usr/local/lib/python2.7/site-packages/babel/numbers.pytsB          0 )  B