ó
    6lj‚  ã                  óV   • S r SSKJr  SSKrSSKJrJr  SSKJr  S	S jr	S
S jr
SS jrg)a  Reading a printed figure as a number. The only place that does it.

A figure printed in a filing carries a currency sign, thousands separators, a
percent sign, a trailing multiple, or parentheses standing in for a minus. Every
consumer of an answer wants the number underneath: a spreadsheet column, a
comparison against a gold value, an arithmetic check. Each of those wanting its
own conversion is how the same figure comes out with two different values, and
the way they differ is by sign.

So there is one function, and everything that needs a number calls it. It is not
a model's job either: the conversion is deterministic, and a model asked for it
alongside the figure returns the figure reliably and the number only most of the
time, which is a hole in a column no one can predict.
é    )ÚannotationsN)ÚDecimalÚInvalidOperation)ÚOptionalc                ó`   • [         R                  " SSU =(       d    SR                  5       5      $ )z@Trim and collapse whitespace, for comparing two printed figures.z\s+Ú Ú )ÚreÚsubÚstrip©Úss    Ú>/home/mande/repo/quber/src/quber/playground/answers/figures.pyÚnorm_printedr      s    € ä�6Š6�&˜# § RŸ™Ó0Ó1Ð1ó    c                ó²  • U (       d  g[         R                  " SS[        U 5      5      nUR                  S5      =(       a    UR	                  S5      nUR                  S5      n[         R                  " SSU5      n[         R                  " SSU5      n[         R                  " S	U5      (       d  g [        U5      nU(       a  U* $ U$ ! [         a     gf = f)
a=  The numeric value of a printed figure, or None if it is not numeric.

Parentheses mean negative, as financial statements print them. Currency
signs, thousands separators, percent signs and a trailing multiple 'x' are
dropped; a figure carrying a scale word keeps only its digits, so
'$1.2 million' reads as 1.2 and is compared against a gold that prints the
same way.

A percentage keeps the magnitude it was printed with: '92.6%' is 92.6, not
0.926. Nothing here rescales a figure, because a reader checking a row
against the filing is comparing it to what the page says.
Nz[$,%\s]r	   Ú(Ú)z()z[xX]$z+(?i)(million|billion|thousand|mm|bn|[mbk])$z^-?\d*\.?\d+$)	r
   r   r   Ú
startswithÚendswithr   Úmatchr   r   )r   ÚtÚnegÚvs       r   Ú	as_numberr      s·   € ö Øô 	�Šˆz˜2œ|¨A›Ó/€AØ
�,‰,�sÓ
×
/ §
¡
¨3£€CØ	�‰�‹€AÜ
�Šˆx˜˜QÓ€AÜ
�ŠÐ=¸rÀ1ÓE€AÜ�8Š8Ð$ a×(Ñ(ØðÜ�A‹Jˆö ˆAˆ2Ð˜!Ðøô ó Ùðús   Â2C	 Ã	
CÃCc                ó6   • [        U 5      R                  S5      $ )uœ  Whether a printed figure is a percentage.

A spreadsheet given '92.6%' stores the fraction and formats the cell as a
percentage, so the figure both computes as a rate and displays as the
document printed it. Given a bare 92.6 it stores ninety-two point six, which
is not the rate. So an export has to know which figures keep their sign, and
this is the test â€” a question about notation, never about magnitude.
Ú%)r   r   r   s    r   Ú
is_percentr   <   s   € ô ˜‹?×#Ñ# CÓ(Ð(r   )r   úOptional[str]ÚreturnÚstr)r   r   r    zOptional[Decimal])r   r   r    Úbool)Ú__doc__Ú
__future__r   r
   Údecimalr   r   Útypingr   r   r   r   © r   r   Ú<module>r(      s(   ðñõ #ã 	ß -Ý ô2ô
õ@	)r   