1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
3

X'ícX§ã@s<dZddlZddlZddlmZmZddlZddlm	Z	m
Z
mZyeWne
k
r`eZYnXe	dƒZGdd„deƒZdQdd	„ZdRd
d„ZdSdd
„ZdTdd„Zdefdd„Zefdd„Zdd„Zdefdd„ZdUdd„Zefdd„Zefdd„Zefd d!„Zefd"d#„Zefd$d%„Zefd&d'„Z d(d)„Z!d*d+„Z"deddfd,d-„Z#d.edd/œd0d1„Z$dVd2d3„Z%Gd4d5„d5e&ƒZ'dedd6ddfd7d8„Z(dedd6ddfd9d:„Z)deddfd;d<„Z*dedfd=d>„Z+Gd?d@„d@e,ƒZ-efdAdB„Z.edfdCdD„Z/dEZ0dFZ1dGe0Z2dHe1Z3dIZ4ej5dJe2e3e4fƒZ6dKdL„Z7dMdN„Z8GdOdP„dPƒZ9dS)Wa‡
    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-2022 by the Babel Team.
    :license: BSD, see LICENSE for more details.
éN)ÚdateÚdatetime)Údefault_localeÚLocaleÚ
get_globalÚ
LC_NUMERICc@seZdZdZdd„ZdS)Ú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)Úselfr©r
ú./tmp/pip-build-gk9425m9/babel/babel/numbers.pyr
*szUnknownCurrencyError.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
r
r
r
rr&srcCs&|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)rÚparseÚ
currenciesÚkeysrÚset)Úlocalerr
r
rÚlist_currencies4srcCs|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)Úcurrencyrr
r
rÚvalidate_currencyFsrcCs>|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)Ú
isinstanceÚstrrr)rrr
r
rÚis_currencyRsrcCs$t|tƒr|jƒ}t||ƒs dS|S)zöReturns 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)rrr
r
rÚnormalize_currency`s


rcCsdtj|ƒ}|dk	rV|j|ƒ}|jd}||krV||}||krF||Sd|krV|dS|jj||ƒS)a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.
    NZcurrency_names_pluralÚother)rrÚplural_formÚ_datarÚget)rÚcountrÚlocr!Zplural_namesZcurrency_plural_namesr
r
rÚget_currency_nameos


r&cCstj|ƒjj||ƒS)zè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.
    )rrZcurrency_symbolsr#)rrr
r
rÚget_currency_symbol‰s	r'cCstdƒ}|j||dƒdS)zÔReturn 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_fractionsÚDEFAULTr)rr#)rZ
precisionsr
r
rÚget_currency_precision•s
r)cCsNtj|ƒ}|dk	r@|j|ƒ}y|jd|Stk
r>YnX|jddS)aV
    Return the unit pattern used for long display of a currency value
    for a given locale.
    This is a string containing ``{0}`` where the numeric part
    should be substituted and ``{1}`` where the currency long display
    name should be substituted.

    >>> get_currency_unit_pattern('USD', locale='en_US', count=10)
    u'{0} {1}'

    .. versionadded:: 2.7.0

    :param currency: the currency code.
    :param count: the optional count.  If provided the unit
                  pattern for that number will be returned.
    :param locale: the `Locale` object or locale identifier.
    NZcurrency_unit_patternsr )rrr!r"ÚLookupError)rr$rr%r!r
r
rÚget_currency_unit_pattern£s

r+TFcsàtdƒ}ˆdkrtjƒ‰ntˆtƒr,ˆjƒ‰ˆdkr:ˆ‰ntˆtƒrLˆjƒ‰|j|jƒfƒ}‡‡fdd„}g}	xl|D]d\}
}}}
|rŒt|Ž}|r˜t|Ž}|
r |sª|
rt|rt|||ƒrt|rÎ|	j|
|||
dœƒqt|	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
)ÚstartÚend)Úend_dateÚ
start_dater
rÚ
_is_activesz,get_territory_currencies.<locals>._is_active)rÚfromZtoÚtender)	rÚdate_ÚtodayrÚ	datetime_rr#rÚappend)Ú	territoryr/r.r2Z
non_tenderZinclude_detailsrZcursr0ÚresultZ
currency_coder,r-Z	is_tenderr
)r.r/rÚget_territory_currenciesÁs64


r9cCstj|ƒjjddƒS)z³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
    ÚdecimalÚ.)rrÚnumber_symbolsr#)rr
r
rÚget_decimal_symbolsr=cCstj|ƒjjddƒS)z©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
    ZplusSignú+)rrr<r#)rr
r
rÚget_plus_sign_symbol&sr?cCstj|ƒjjddƒS)zª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
    Z	minusSignú-)rrr<r#)rr
r
rÚget_minus_sign_symbol1srAcCstj|ƒjjddƒS)z»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
    ZexponentialÚE)rrr<r#)rr
r
rÚget_exponential_symbol<srCcCstj|ƒjjddƒS)z³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
    Úgroupú,)rrr<r#)rr
r
rÚget_group_symbolGsrFcCstjdtƒt||dS)aŠReturn the given number formatted for a specific locale.

    >>> format_number(1099, locale='en_US')  # doctest: +SKIP
    u'1,099'
    >>> format_number(1099, locale='de_DE')  # doctest: +SKIP
    u'1.099'

    .. deprecated:: 2.6.0

       Use babel.numbers.format_decimal() instead.

    :param number: the number to format
    :param locale: the `Locale` object or locale identifier


    z+Use babel.numbers.format_decimal() instead.)r)ÚwarningsÚwarnÚDeprecationWarningÚformat_decimal)Únumberrr
r
rÚ
format_numberRsrLcCs4t|tjƒst‚|jƒjƒ}|jdkr*dSt|jƒS)zReturn maximum precision of a decimal instance's fractional part.

    Precision is extracted from the fractional part only.
    r)rr:ÚDecimalÚAssertionErrorÚ	normalizeÚas_tupleÚexponentÚabs)rKZ
decimal_tupler
r
rÚget_decimal_precisiongs

rScCs&t|tttjfƒst‚tjdƒ|S)z<Return minimal quantum of a number, as defined by precision.é
)rÚintÚlongr:rMrN)Z	precisionr
r
rÚget_decimal_quantumtsrWcCs4tj|ƒ}|s|jj|ƒ}t|ƒ}|j||||dS)a¾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'

    By default the locale is allowed to truncate and round a high-precision
    number by forcing its format pattern onto the decimal part. You can bypass
    this behavior with the `decimal_quantization` parameter:

    >>> format_decimal(1.2346, locale='en_US')
    u'1.235'
    >>> format_decimal(1.2346, locale='en_US', decimal_quantization=False)
    u'1.2346'
    >>> format_decimal(12345.67, locale='fr_CA', group_separator=False)
    u'12345,67'
    >>> format_decimal(12345.67, locale='en_US', group_separator=True)
    u'12,345.67'

    :param number: the number to format
    :param format:
    :param locale: the `Locale` object or locale identifier
    :param decimal_quantization: Truncate and round high-precision numbers to
                                 the format pattern. Defaults to `True`.
    :param group_separator: Boolean to switch group separator on/off in a locale's
                            number format.
    )Údecimal_quantizationÚgroup_separator)rrÚdecimal_formatsr#Ú
parse_patternÚapply)rKÚformatrrXrYÚpatternr
r
rrJzs*
rJÚshort)Úformat_typerÚfraction_digitscCs4tj|ƒ}t||||ƒ\}}t|ƒ}|j||ddS)u—Return the given decimal number formatted for a specific locale in compact form.

    >>> format_compact_decimal(12345, format_type="short", locale='en_US')
    u'12K'
    >>> format_compact_decimal(12345, format_type="long", locale='en_US')
    u'12 thousand'
    >>> format_compact_decimal(12345, format_type="short", locale='en_US', fraction_digits=2)
    u'12.35K'
    >>> format_compact_decimal(1234567, format_type="short", locale="ja_JP")
    u'123万'
    >>> format_compact_decimal(2345678, format_type="long", locale="mk")
    u'2 милиони'
    >>> format_compact_decimal(21098765, format_type="long", locale="mk")
    u'21 милион'

    :param number: the number to format
    :param format_type: Compact format to use ("short" or "long")
    :param locale: the `Locale` object or locale identifier
    :param fraction_digits: Number of digits after the decimal point to use. Defaults to `0`.
    F)rX)rrÚ_get_compact_formatr[r\)rKr`rrar]r^r
r
rÚformat_compact_decimal¬s
rcc	CsÒd}|j|}x¦tdd„|dDƒddD]ˆ}t|ƒ|kr*|dt|ƒ}t|ƒj}|dkr^P||d|jdƒd	}t||ƒ}|jt|ƒƒ}||krœ|nd}||t|ƒ}Pq*W|dkrÊ|j	j
dƒ}||fS)
zÑReturns the number after dividing by the unit and the format pattern to use.
    The algorithm is described here:
    https://www.unicode.org/reports/tr35/tr35-45/tr35-numbers.html#Compact_Number_Formats.
    NcSsg|]}t|ƒ‘qSr
)rU)Ú.0Úmr
r
rú
<listcomp>Îsz'_get_compact_format.<locals>.<listcomp>r T)ÚreverseÚ0rTé)Zcompact_decimal_formatsÚsortedrRrr[r^r$Úroundr!rZr#)	rKr`rrar]Zcompact_formatZ	magnituder^r!r
r
rrbÇs"
 

rbc@seZdZdZdS)ÚUnknownCurrencyFormatErrorz>Exception raised when an unknown currency format is requested.N)rrrrr
r
r
rrlåsrlÚstandardc	Cs||dkrt|||||||dStj|ƒ}|r6t|ƒ}n0y|j|}Wn tk
rdtd|ƒ‚YnX|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 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.099,98'

    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"

    >>> format_currency(101299.98, 'USD', locale='en_US', group_separator=False)
    u'$101299.98'

    >>> format_currency(101299.98, 'USD', locale='en_US', group_separator=True)
    u'$101,299.98'

    You can also pass format_type='name' to use long display names. The order of
    the number and currency name, along with the correct localized plural form
    of the currency name, is chosen according to locale:

    >>> format_currency(1, 'USD', locale='en_US', format_type='name')
    u'1.00 US dollar'
    >>> format_currency(1099.98, 'USD', locale='en_US', format_type='name')
    u'1,099.98 US dollars'
    >>> format_currency(1099.98, 'USD', locale='ee', format_type='name')
    u'us ga dollar 1,099.98'

    By default the locale is allowed to truncate and round a high-precision
    number by forcing its format pattern onto the decimal part. You can bypass
    this behavior with the `decimal_quantization` parameter:

    >>> format_currency(1099.9876, 'USD', locale='en_US')
    u'$1,099.99'
    >>> format_currency(1099.9876, 'USD', locale='en_US', decimal_quantization=False)
    u'$1,099.9876'

    :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 natural number of decimal digits
    :param format_type: the currency format type to use
    :param decimal_quantization: Truncate and round high-precision numbers to
                                 the format pattern. Defaults to `True`.
    :param group_separator: Boolean to switch group separator on/off in a locale's
                            number format.

    Úname)r]rÚcurrency_digitsrXrYz&%r is not a known currency format type)rrorXrY)Ú_format_currency_long_namerrr[Zcurrency_formatsÚKeyErrorrlr\)	rKrr]rror`rXrYr^r
r
rÚformat_currencyésZ


rrc
Csxtj|ƒ}t|tƒrt|ƒ}n|}t|||d}	t|||d}
|sN|jj|ƒ}t	|ƒ}|j
||||||d}|	j||
ƒS)N)r$r)rrorXrY)rrrrÚfloatr+r&rZr#r[r\r])
rKrr]rror`rXrYZnumber_nZunit_patternZdisplay_namer^Znumber_partr
r
rrpVs



rpcCs4tj|ƒ}|s|jj|ƒ}t|ƒ}|j||||dS)uDReturn 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‰'

    By default the locale is allowed to truncate and round a high-precision
    number by forcing its format pattern onto the decimal part. You can bypass
    this behavior with the `decimal_quantization` parameter:

    >>> format_percent(23.9876, locale='en_US')
    u'2,399%'
    >>> format_percent(23.9876, locale='en_US', decimal_quantization=False)
    u'2,398.76%'

    >>> format_percent(229291.1234, locale='pt_BR', group_separator=False)
    u'22929112%'

    >>> format_percent(229291.1234, locale='pt_BR', group_separator=True)
    u'22.929.112%'

    :param number: the percent number to format
    :param format:
    :param locale: the `Locale` object or locale identifier
    :param decimal_quantization: Truncate and round high-precision numbers to
                                 the format pattern. Defaults to `True`.
    :param group_separator: Boolean to switch group separator on/off in a locale's
                            number format.
    )rXrY)rrZpercent_formatsr#r[r\)rKr]rrXrYr^r
r
rÚformat_percentzs'
rtcCs2tj|ƒ}|s|jj|ƒ}t|ƒ}|j|||dS)a·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'##0.##E00', locale='en_US')
    u'1.23E06'

    By default the locale is allowed to truncate and round a high-precision
    number by forcing its format pattern onto the decimal part. You can bypass
    this behavior with the `decimal_quantization` parameter:

    >>> format_scientific(1234.9876, u'#.##E0', locale='en_US')
    u'1.23E3'
    >>> format_scientific(1234.9876, u'#.##E0', locale='en_US', decimal_quantization=False)
    u'1.2349876E3'

    :param number: the number to format
    :param format:
    :param locale: the `Locale` object or locale identifier
    :param decimal_quantization: Truncate and round high-precision numbers to
                                 the format pattern. Defaults to `True`.
    )rX)rrZscientific_formatsr#r[r\)rKr]rrXr^r
r
rÚformat_scientific©s
rucs"eZdZdZd‡fdd„	Z‡ZS)ÚNumberFormatErrorz>Exception raised when a string cannot be parsed into a number.Ncstƒj|ƒ||_dS)N)Úsuperr
Úsuggestions)rÚmessagerx)Ú	__class__r
rr
ÏszNumberFormatError.__init__)N)rrrrr
Ú
__classcell__r
r
)rzrrvÌsrvcCs:yt|jt|ƒdƒƒStk
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)rUÚreplacerFÚ
ValueErrorrv)Ústringrr
r
rÚparse_numberÕsr€c	CsZtj|ƒ}t|ƒ}t|ƒ}|rD|dkrD||krDd|krD|jd|ƒ}ytj|j|dƒj|dƒƒ}Wn"tjk
r„td|ƒ‚YnX|o||krVt	||dd}||koº|j
dƒ||krVytj|j|dƒj|dƒƒ}Wn.tjk
r
td	||f|gd
‚YnLXt	||dd}||kr<td	||f|gd
‚ntd|||f||gd
‚|S)akParse 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')
    >>> parse_decimal('12 345,123', locale='ru')
    Decimal('12345.123')

    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

    If `strict` is set to `True` and the given string contains a number
    formatted in an irregular way, an exception is raised:

    >>> parse_decimal('30.00', locale='de', strict=True)
    Traceback (most recent call last):
        ...
    NumberFormatError: '30.00' is not a properly formatted decimal number. Did you mean '3.000'? Or maybe '30,00'?

    >>> parse_decimal('0.00', locale='de', strict=True)
    Traceback (most recent call last):
        ...
    NumberFormatError: '0.00' is not a properly formatted decimal number. Did you mean '0'?

    :param string: the string to parse
    :param locale: the `Locale` object or locale identifier
    :param strict: controls whether numbers formatted in a weird way are
                   accepted or rejected
    :raise NumberFormatError: if the string can not be converted to a
                              decimal number
    õ ú r|r;z %r is not a valid decimal numberF)rrXrhz?%r is not a properly formatted decimal number. Did you mean %r?)rxzL%r is not a properly formatted decimal number. Did you mean %r? Or maybe %r?)rrrFr=r}r:rMÚInvalidOperationrvrJÚrstrip)	rrÚstrictZgroup_symbolZdecimal_symbolÚparsedZproperZ
parsed_altZ
proper_altr
r
rÚ
parse_decimalïsF%


r‡z
[^0-9@#.,]z[0-9@#.,E+]z(?P<prefix>(?:'[^']*'|%s)*)z(?P<number>%s*)z(?P<suffix>.*)z%s%s%scCsjt|ƒ}|jdƒ}|dkrdS||d}|d|d…jdƒ}|dkrR||fS|||d}||fS)	z¹Parse primary and secondary digit grouping

    >>> parse_grouping('##')
    (1000, 1000)
    >>> parse_grouping('#,###')
    (3, 3)
    >>> parse_grouping('#,####,###')
    (3, 4)
    rEriéèNééÿÿÿÿ)rˆrˆrŠ)ÚlenÚrfind)ÚpÚwidthZg1Zg2r
r
rÚparse_groupingJs

rc	Cs4t|tƒr|Sdd„}|}d|krP|jddƒ\}}||ƒ\}}}||ƒ\}}}	n||ƒ\}}}d|}|}	d|kr„|jddƒ\}}
nd}
d|kr¨d	|kr¨d
|kr¨tdƒ‚d	|krÂ|jd	dƒ\}}n|}d}d
d„}
|
|ƒ}|
|ƒ}|
r|
jdƒ}|
jdƒ}
|
|
ƒ}nd}d}t|ƒ}t|||f||	f|||||ƒS)zParse number format patternscSs&tj|ƒ}|dkrtd|ƒ‚|jƒS)NzInvalid number pattern %r)Ú	number_reÚsearchr~Úgroups)r^Úrvr
r
rÚ
_match_numberes
z$parse_pattern.<locals>._match_numberú;rir@rBNú@r;rhz5Significant digit patterns can not contain "@" or "0"r|cSsXd}}xF|D]>}|dkr,|d7}|d7}q|dkr>|d7}q|dkrJqqPqW||fS)z(Calculate the min and max allowed digitsrz@0riú#rEr
)rÚminÚmaxÚcr
r
rÚparse_precision„s


z&parse_pattern.<locals>.parse_precisionr>)rÚ
NumberPatternÚsplitr~ÚrsplitÚ
startswithÚlstripr)r^r”Zpos_patternZneg_patternZ
pos_prefixrKZ
pos_suffixZ
neg_prefixÚ_Z
neg_suffixÚexpÚintegerÚfractionr›Úint_precÚ	frac_precÚexp_plusÚexp_precÚgroupingr
r
rr[`sF




r[c@sXeZdZdd„Zdd„Zdd„Zdd„Zddd„Zd
d„Zdd„Z	dd„Z
ddd„Zd	S)rœc		Cs>||_||_||_||_||_||_||_||_|jƒ|_	dS)N)
r^ÚprefixÚsuffixr©r¥r¦r¨r§Ú
compute_scaleÚscale)	rr^rªr«r©r¥r¦r¨r§r
r
rr
¥szNumberPattern.__init__cCsdt|ƒj|jfS)Nz<%s %r>)Útyperr^)rr
r
rÚ__repr__²szNumberPattern.__repr__cCs>d}ddj|j|jƒkr d}nddj|j|jƒkr:d}|S)uReturn the scaling factor to apply to the number before rendering.

        Auto-set to a factor of 2 or 3 if presence of a ``%`` or ``‰`` sign is
        detected in the prefix or suffix of the pattern. Default is to not mess
        with the scale at all and keep it to 0.
        rú%r|r‰u‰é)Újoinrªr«)rr­r
r
rr¬µszNumberPattern.compute_scalecCs†|jƒ}|t|ƒ}|jƒdks$t‚tdt|jƒgƒd}||}|t|ƒ}d}|dkrft|ƒ}n|jrtt|ƒ}t	|ƒ}|||fS)zG Returns normalized scientific notation components of a value.
        rrir|)
ÚadjustedrWrNr™r˜r¥rAr§r?rR)rÚvaluerr¢Z
lead_shiftÚexp_signr
r
rÚscientific_notation_elementsÃs
z*NumberPattern.scientific_notation_elementsNTc	CsÐt|tjƒstjt|ƒƒ}|j|jƒ}t|jƒƒ}t|ƒj	ƒ}|j
rV|j||ƒ\}}	}
|rltj
dtƒ|}n|r„|r„t|ƒfd}n|j}|sž|j
rº|dkrº|dt|dt|ƒgƒf}|j
rdj|j||||ƒt|ƒ|
|jt|	ƒ|j
d|j
d|ƒgƒ}nnd|jkr`|j||jd|jdƒ}
|
jdƒ\}}}|j|dd|ƒ}|rp|t|ƒ|7}n|j||||ƒ}dj|j|||j|gƒ}d	|krÌ|jd
t|||ƒƒ}|jd|j ƒƒ}|jd	t!||ƒƒ}|S)
aRenders into a string a number following the defined pattern.

        Forced decimal quantization is active by default so we'll produce a
        number string that is strictly following CLDR pattern definitions.

        :param value: The value to format. If this is not a Decimal object,
                      it will be cast to one.
        :type value: decimal.Decimal|float|int
        :param locale: The locale to use for formatting.
        :type locale: str|babel.core.Locale
        :param currency: Which currency, if any, to format as.
        :type currency: str|None
        :param currency_digits: Whether or not to use the currency's precision.
                                If false, the pattern's precision is used.
        :type currency_digits: bool
        :param decimal_quantization: Whether decimal numbers should be forcibly
                                     quantized to produce a formatted output
                                     strictly matching the CLDR definition for
                                     the locale.
        :type decimal_quantization: bool
        :param force_frac: DEPRECATED - a forced override for `self.frac_prec`
                           for a single formatting invocation.
        :return: Formatted decimal string.
        :rtype: str
        z@The force_frac parameter to NumberPattern.apply() is deprecated.r‰rrir|r–r;ièõ¤u¤¤¤u¤¤)rr)"rr:rMrÚscalebr­rUÚ	is_signedrRrOr¨r¶rGrHrIr)r¦r™rSr²Ú_quantize_valuerCÚ_format_intr^Ú_format_significantr¥Ú	partitionr=rªr«r}r&rr')rr´rrrorXÚ
force_fracrYZis_negativer¢rµr¦rKÚtextÚaÚsepÚbÚretvalr
r
rr\ÞsR#"
zNumberPattern.applycCs²|jƒ}|d|}t|j|ƒjtjdƒƒƒ}|dkrF|d|}nh|d|…}t|ƒ}	|	t||	dƒ}
dj|pxddt	|ddƒ||	|
…||
d…j
dƒdj
dƒ}|S)Nrirrhz-{intpart}.{pad:0<{fill}}{fracpart}{fracextra}r|)ÚintpartÚpadÚfillZfracpartZ	fracextrar;)r³rr¸Úquantizer:rMr‹r™r]r˜r„)rr´ZminimumÚmaximumr¢r­Údigitsr8rÄÚiÚjr
r
rr¼Zs 
z!NumberPattern._format_significantc	Cs~t|ƒ}||kr d|||}|jd}d}t|ƒ}x>t|ƒ|krt|||d…|}|d|…}|jd}q8W||S)Nrhrr|ri)r‹r©rF)	rr´r˜r™rrŽZgsizeÚretÚsymbolr
r
rr»ms
zNumberPattern._format_intcCsjt|dƒ}|j|ƒ}dj|ƒjdƒ\}}}	|}
|rP|j||jd|jd|ƒ}
|
|j|	p\d||ƒ}|S)Nriz{:f}r;rrh)rWrÇr]r½r»r¥Ú_format_frac)rr´rr¦rYZquantumZroundedrÀrÁrÂZinteger_partrKr
r
rrºzs
zNumberPattern._quantize_valuecCs„|p|j\}}t|ƒ|kr.|d|t|ƒ7}|dksJ|dkrNt|ƒdkrNdSx(t|ƒ|krv|ddkrv|dd…}qPWt|ƒ|S)Nrhrr|rirŠrŠ)r¦r‹rUr=)rr´rr¾r˜r™r
r
rr΄szNumberPattern._format_frac)NTTNT)N)rrrr
r¯r¬r¶r\r¼r»rºrÎr
r
r
rrϣs

t

rœ)N)N)N)N)NNTFF)r):rr:Úrerrr3r5rGZ
babel.corerrrrVÚ	NameErrorrUrr	rrrrrr&r'r)r+r9r=r?rArCrFrLrSrWrJrcrbrqrlrrrprtrur~rvr€r‡Z
PREFIX_ENDZNUMBER_TOKENZPREFIX_PATTERNZNUMBER_PATTERNZSUFFIX_PATTERNÚcompilerrr[rœr
r
r
rÚ<module>sj





X
1
l#/"	P

C