dbfdg 3 qDYe@sdZddlZddlmZmZddlmZddlm Z m Z m Z ddl m Z mZddlmZe dZGd d d eZdFd d ZdGd dZdHddZdIddZdefddZefddZddZdJddZefddZefdd Zefd!d"Zefd#d$Zefd%d&Z efd'd(Z!defd)d*Z"Gd+d,d,e#Z$dedd-fd.d/Z%defd0d1Z&defd2d3Z'Gd4d5d5e(Z)efd6d7Z*efd8d9Z+d:Z,d;Z-dZ0ej1d?e.e/e0fZ2d@dAZ3dBdCZ4GdDdEdEe5Z6dS)Ka 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. N)datedatetime)chain)default_localeLocale get_global)decimal string_types)locale_identifiers LC_NUMERICc@seZdZdZddZdS)UnknownCurrencyErrorzRException thrown when a currency is requested for which no data is available. cCstj|d|||_dS)zkCreate the exception. :param identifier: the identifier string of the unsupported currency zUnknown currency %r.N) Exception__init__ identifier)selfrr/usr/lib/python3.6/numbers.pyr%szUnknownCurrencyError.__init__N)__name__ __module__ __qualname____doc__rrrrrr !sr cCs&|rtj|jj}ntd}t|S)aM Return a `set` of normalized currency codes. .. versionadded:: 2.5.0 :param locale: filters returned currency codes by the provided locale. Expected to be a locale instance or code. If no locale is provided, returns the list of all currencies from all locales. Zall_currencies)rparse currencieskeysrset)localerrrrlist_currencies/s rcCs|t|krt|dS)a Check the currency code is recognized by Babel. Accepts a ``locale`` parameter for fined-grained validation, working as the one defined above in ``list_currencies()`` method. Raises a `UnknownCurrencyError` exception if the currency is unknown to Babel. N)rr )currencyrrrrvalidate_currencyAs rc Cs>| st|t rdSyt||Wntk r8dSXdS)zy Returns `True` only if a currency is recognized by Babel. This method always return a Boolean and never raise. FT) isinstancer rr )rrrrr is_currencyMsr cCs$t|tr|j}t||s dS|S)zReturns the normalized sting of any currency code. Accepts a ``locale`` parameter for fined-grained validation, working as the one defined above in ``list_currencies()`` method. Returns None if the currency is unknown to Babel. N)rr upperr )rrrrrnormalize_currency[s   r"cCsHtj|}|dk r:|j|}|jd}||kr:|||S|jj||S)aReturn 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. NZcurrency_names_plural)rr plural_form_datarget)rcountrlocr#Z plural_namesrrrget_currency_namejs    r(cCstj|jj||S)zReturn 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. )rrZcurrency_symbolsr%)rrrrrget_currency_symbols r)cCstd}|j||ddS)zReturn currency's precision. Precision is the number of decimals found after the decimal point in the currency's format pattern. .. versionadded:: 2.5.0 :param currency: the currency code. Zcurrency_fractionsZDEFAULTr)rr%)rZ precisionsrrrget_currency_precisions r*TFcstd}dkrtjnttr,jdkr:nttrLj|j|jf}fdd}g} xl|D]d\} } } } | rt| } | rt| } | r|s| rt|rt|| | rt|r| j| | | | dqt| j| qtW| S)asReturns the list of currencies for the given territory that are valid for the given date range. In addition to that the currency database distinguishes between tender and non-tender currencies. By default only tender currencies are returned. The return value is a list of all currencies roughly ordered by the time of when the currency became active. The longer the currency is being in use the more to the left of the list it will be. The start date defaults to today. If no end date is given it will be the same as the start date. Otherwise a range can be defined. For instance this can be used to find the currencies in use in Austria between 1995 and 2011: >>> from datetime import date >>> get_territory_currencies('AT', date(1995, 1, 1), date(2011, 1, 1)) ['ATS', 'EUR'] Likewise it's also possible to find all the currencies in use on a single date: >>> get_territory_currencies('AT', date(1995, 1, 1)) ['ATS'] >>> get_territory_currencies('AT', date(2011, 1, 1)) ['EUR'] By default the return value only includes tender currencies. This however can be changed: >>> get_territory_currencies('US') ['USD'] >>> get_territory_currencies('US', tender=False, non_tender=True, ... start_date=date(2014, 1, 1)) ['USN', 'USS'] .. versionadded:: 2.0 :param territory: the name of the territory to find the currency for. :param start_date: the start date. If not given today is assumed. :param end_date: the end date. If not given the start date is assumed. :param tender: controls whether tender currencies should be included. :param non_tender: controls whether non-tender currencies should be included. :param include_details: if set to `True`, instead of returning currency codes the return value will be dictionaries with detail information. In that case each dictionary will have the keys ``'currency'``, ``'from'``, ``'to'``, and ``'tender'``. Zterritory_currenciesNcs |dks|ko|dkp|kS)Nr)startend)end_date start_daterr _is_activesz,get_territory_currencies.._is_active)rfromZtotender) rdate_Ztodayr datetime_rr%r!append)Z territoryr.r-r1Z non_tenderZinclude_detailsrZcursr/resultZ currency_coder+r,Z is_tenderr)r-r.rget_territory_currenciess64    r6cCstj|jjddS)zReturn the symbol used by the locale to separate decimal fractions. >>> get_decimal_symbol('en_US') u'.' :param locale: the `Locale` object or locale identifier r.)rrnumber_symbolsr%)rrrrget_decimal_symbolsr9cCstj|jjddS)zReturn the plus sign symbol used by the current locale. >>> get_plus_sign_symbol('en_US') u'+' :param locale: the `Locale` object or locale identifier ZplusSign+)rrr8r%)rrrrget_plus_sign_symbolsr;cCstj|jjddS)zReturn the plus sign symbol used by the current locale. >>> get_minus_sign_symbol('en_US') u'-' :param locale: the `Locale` object or locale identifier Z minusSign-)rrr8r%)rrrrget_minus_sign_symbol sr=cCstj|jjddS)zReturn 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 Z exponentialE)rrr8r%)rrrrget_exponential_symbolsr?cCstj|jjddS)zReturn 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 group,)rrr8r%)rrrrget_group_symbol srBcCs t||dS)aReturn 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)format_decimal)numberrrrr format_number+s rEcCs.tj|}|s|jj|}t|}|j||S)aReturn 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 )rrZdecimal_formatsr% parse_patternapply)rDformatrpatternrrrrC;s   rCc@seZdZdZdS)UnknownCurrencyFormatErrorz>Exception raised when an unknown currency format is requested.N)rrrrrrrrrJZsrJZstandardc Csttj|}|rt|}n0y|j|}Wn tk rFtd|YnX|r^t|}||f}nd}|j||||dS)uReturn 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 format 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' Currencies usually have a specific number of decimal digits. This function favours that information over the given format: >>> format_currency(1099.98, 'JPY', locale='en_US') u'\xa51,100' >>> format_currency(1099.98, 'COP', u'#,##0.00', locale='es_ES') u'1.100' However, the number of decimal digits can be overriden from the currency information, by setting the last parameter to ``False``: >>> format_currency(1099.98, 'JPY', locale='en_US', currency_digits=False) u'\xa51,099.98' >>> format_currency(1099.98, 'COP', u'#,##0.00', locale='es_ES', currency_digits=False) u'1.099,98' If a format is not specified the type of currency format to use from the locale can be specified: >>> format_currency(1099.98, 'EUR', locale='en_US', format_type='standard') u'\u20ac1,099.98' When the given currency format type is not available, an exception is raised: >>> format_currency('1099.98', 'EUR', locale='root', format_type='unknown') Traceback (most recent call last): ... UnknownCurrencyFormatError: "'unknown' is not a known currency format type" :param number: the number to format :param currency: the currency code :param format: the format string to use :param locale: the `Locale` object or locale identifier :param currency_digits: use the currency's number of decimal digits :param format_type: the currency format type to use z&%r is not a known currency format typeN)r force_frac)rrrFZcurrency_formatsKeyErrorrJr*rG) rDrrHrZcurrency_digitsZ format_typerI precisionZfracrrrformat_currency^s:   rNcCs.tj|}|s|jj|}t|}|j||S)uReturn 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‰', locale='en_US') u'25,123‰' :param number: the percent number to format :param format: :param locale: the `Locale` object or locale identifier )rrZpercent_formatsr%rFrG)rDrHrrIrrrformat_percents   rOcCs.tj|}|s|jj|}t|}|j||S)aReturn 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 )rrZscientific_formatsr%rFrG)rDrHrrIrrrformat_scientifics   rPc@seZdZdZdS)NumberFormatErrorz>Exception raised when a string cannot be parsed into a number.N)rrrrrrrrrQsrQc Cs:yt|jt|dStk r4td|YnXdS)aSParse 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 z%r is not a valid numberN)intreplacerB ValueErrorrQ)stringrrrr parse_numbersrWc CsTtj|}y"tj|jt|djt|dStjk rNtd|YnXdS)aParse 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 rRr7z %r is not a valid decimal numberN) rrrDecimalrTrBr9ZInvalidOperationrQ)rVrrrr parse_decimals  rYz [^0-9@#.,]z [0-9@#.,E+]z(?P(?:'[^']*'|%s)*)z(?P%s+)z(?P.*)z%s%s%scCsjt|}|jd}|dkrdS||d}|d| djd}|dkrR||fS|||d}||fS) zParse primary and secondary digit grouping >>> parse_grouping('##') (1000, 1000) >>> parse_grouping('#,###') (3, 3) >>> parse_grouping('#,####,###') (3, 4) rAN)r[r[r])lenrfind)pwidthZg1Zg2rrrparse_groupings   rbc Cs@t|tr|Sdd}|}d|krP|jdd\}}||\}}}||\}}} n||\}}}d|}|} d|kr|jdd\}} nd} d|krd |krd |krtd d |kr|jd d\} } n|} d } d d} | | }| | }| r| | | }| jd}| jd} | | }nd}d}t| }t|||f|| f|||||S)zParse number format patternscSs&tj|}|dkrtd||jS)NzInvalid number pattern %r) number_researchrUgroups)rIrvrrr _match_number9s  z$parse_pattern.._match_number;rZr<r>N@r70z5Significant digit patterns can not contain "@" or "0"rRcSsXd}}xF|D]>}|dkr,|d7}|d7}q|dkr>|d7}q|dkrJqqPqW||fS)z(Calculate the min and max allowed digitsrz@0rZ#rAr)r`minmaxcrrrparse_precisionXs   z&parse_pattern..parse_precisionr:)r NumberPatternsplitrUrsplit startswithlstriprb)rIrgZ pos_patternZ neg_patternZ pos_prefixrDZ pos_suffixZ neg_prefix_Z neg_suffixexpZintegerZfractionroint_prec frac_precexp_plusexp_precgroupingrrrrF4sH      rFc@s@eZdZddZddZdddZdd Zd d Zdd d ZdS)rpc Csv||_||_||_||_||_||_||_||_ddj|j|jkrNd|_ n$ddj|j|jkrld|_ nd|_ dS)N%rRr\u‰r) rIprefixsuffixr{rwrxrzryjoinscale) rrIr~rr{rwrxrzryrrrrzszNumberPattern.__init__cCsdt|j|jfS)Nz<%s %r>)typerrI)rrrr__repr__szNumberPattern.__repr__Nc Cs^|p|j}t|tjs$tjt|}|j|j}t|j}|j r6|j }t |}|j d|j dkr|||j dd8}n&|j drt||j d|j d}|dkr|d| }n |d|}d}|dkrt |}n|jrt|}t |}d|j||d|dt|||jt||j d|j d|f} nd|jkr|jt ||j d|j d} | jd\} } } |j| dd|} | r| t|| 7} nhtjd d |d}|j|}tt |jd\} } } |j| |j d|j d||j| pd ||} d |j|| |j|f}d |krZ|jdt|||}|jd|j}|jd t||}|S)NrrZ rRz%s%s%s%srir7iz1.1rjz%s%s%s¤u¤¤¤u¤¤)rxrrrXstrscalebrrSZ is_signedrzadjustedabsrwr=ryr;_format_significantr? _format_intrI partitionr9quantize _format_fracr~rrTr(r!r))rvaluerrrKrxZ is_negativervZexp_signrDtextasepbrMZroundedZretvalrrrrGs`              zNumberPattern.applyc Cs|j}|d|}t|j|jtjd}|dkrF|d| }nh|d| }t|} | t|| d} dj|pxddt |dd || | || dj ddj d}|S)NrZrrjz-{intpart}.{pad:0<{fill}}{fracpart}{fracextra}rR)intpartZpadZfillZfracpartZ fracextrar7) rrrrrrXr^rmrHrlrstrip) rrZminimumZmaximumrvrZdigitsr5rijrrrrs   z!NumberPattern._format_significantc Cs~t|}||kr d|||}|jd}d}t|}x>t||krt||| d|}|d| }|jd}q8W||S)NrjrrRrZ)r^r{rB) rrrlrmrraZgsizeretZsymbolrrrrs zNumberPattern._format_intcCs|p|j\}}t||kr.|d|t|7}|dksJ|dkrNt|dkrNdSx(t||krv|ddkrv|dd}qPWt||S)NrjrrRrZr]r])rxr^rSr9)rrrrKrlrmrrrrs zNumberPattern._format_frac)NN)N) rrrrrrGrrrrrrrrpxs  J rp)N)N)N)N)NNTFF)7rrerrr2r3 itertoolsrZ babel.corerrrZ babel._compatrr Zbabel.localedatar r r r rrr r"r(r)r*r6r9r;r=r?rBrErCrLrJrNrOrPrUrQrWrYZ PREFIX_ENDZ NUMBER_TOKENZPREFIX_PATTERNZNUMBER_PATTERNZSUFFIX_PATTERNcompilercrbrFobjectrprrrrsV       X J    D