
    ii8                    z   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZmZ e	rd dlmZ  ed	eee          Z G d
 dee                   Zddd:dZd;dZd<dZd=dZ	 	 	 	 	 	 d>d?d(Z 	 	 	 	 	 	 	 d@dAd+Z!dBd.Z"	 	 	 	 	 dCdDd0Z#	 	 	 	 	 dCdEd2Z$dFd5Z%dGd8Z&	 	 	 	 	 	 	 d@dHd9Z'dS )I    )annotations)
ROUND_DOWNDecimallocalcontext)
float_info)TYPE_CHECKINGAnyProtocolTypeVarcast)ParameterException)check_for_deprecated_parameters)create_random_decimalcreate_random_floatcreate_random_integer)RandomTc                      e Zd ZdZddd
ZdS )NumberGeneratorProtocolz?Protocol for custom callables used to generate numerical valuesNrandom'Random'minimumT | Nonemaximumreturnr   c                    dS )zSignature of the callable.

        :param random: An instance of random.
        :param minimum: A minimum value.
        :param maximum: A maximum value.
        :return: The generated numeric value.
        N )selfr   r   r   s       /var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/polyfactory/value_generators/constrained_numbers.py__call__z NumberGeneratorProtocol.__call__   s	     	    )NN)r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r    r   r!   r   r   r      s3        II      r!   r   g:0yE>)deltavalue_1floatvalue_2r&   r   boolc               .    t          | |z
            |k    S )a  Return True if two floats are almost equal

    :param value_1: A float value.
    :param value_2: A float value.
    :param delta: A minimal delta.

    :returns: Boolean dictating whether the floats can be considered equal - given python's problematic comparison of floats.
    )abs)r'   r)   r&   s      r   almost_equal_floatsr-      s     w !!U**r!   r   r   multiple_ofc                   t          | t                    r:t          |t                    r%| |z  t          d          t          d          fv rdS t          | |z            }|dk    rdnd}||z  ||z   |z  fD ]^}t          |          }t          |t          |                     s-t          |t          |                    s| |cxk     r|k     rn [ dS _dS )ao  Determine if at least one multiply of `multiple_of` lies in the given range.

    :param minimum: T: A minimum value.
    :param maximum: T: A maximum value.
    :param multiple_of: T: A value to use as a base for multiplication.

    :returns: Boolean dictating whether at least one multiply of `multiple_of` lies in the given range between minimum and maximum.
    z+infz-infFr      T)
isinstancer(   roundr-   )r   r   r.   
multiplierstepmultiplymultiply_floats          r   #is_multiply_of_multiple_of_in_ranger8   +   s
   ( 	7E""{E** k!eFmmU6]]%CCCuw,--Ja11RD*  +-
T0A[/PQ  xg??	">5>>BB	 ++++G+++++44 , 5r!   valuec                    |dk    rdS t          |           t          |          z  dz  }t          |d          pt          |d          S )zDetermine whether a given value passes the pydantic multiple_of validation.

    :param value: A numeric value.
    :param multiple_of: Another numeric value.

    :returns: Boolean dictating whether value is a multiple of value.

    r   Tr0           g      ?)r(   r-   )r9   r.   mods      r   "passes_pydantic_multiple_validatorr=   g   sQ     at
,,{++
+a
/CsC((I,?S,I,IIr!   t_typetype[T]c                    t           dt          t          j        t          t	          d          i}t          d||                    S )zGet a small increment base to add to constrained values, i.e. lt/gt entries.

    :param t_type: A value of type T.

    :returns: An increment T.
    r0   z0.001r   )intr(   r   epsilonr   r   )r>   valuess     r   get_incrementrD   v   s;     	Qz!!!F
 VF^$$$r!   Nltr   legtge
max_digits
int | Nonedecimal_placestuple[T | None, T | None]c                   ||}n||t          |           z   }nd}||}n||t          |           z
  }nd}|d}	|||z
  n|}
|
|	k     rt          d|
z  dz
            nt          d|	z  dz
            }|dz  }|||k     r | |          }n||k    rd| }t          |          |||k    r! | |dk    r|nt          d                    }n||k     rd| }t          |          ||fS )zReturn an optional value.

    :param equal_value: An GE/LE value.
    :param constrained: An GT/LT value.
    :param increment: increment

    :returns: Optional T.
    N
   r0   r1   z minimum value must be less than r   z#maximum value must be greater than )rD   r   r   )r>   rE   rF   rG   rH   rI   rK   minimum_valuemaximum_valuemax_whole_digitswhole_digitsr   r   msgs                 r   get_value_or_nonerT      sm   " 
~	]6222	~	]62226D6PzN22V`-9<L-L-LGB$q()))RYZ\^nZnqrZrRsRs 	 R. MG$;$;"F7OOMMW$$>W>>C$S))) MG$;$;"Fgkk77wqzzJJMMW$$AAAC$S)))-''r!   r   r   c	           	     t    | |                                 dz            }	t          ||||| ||          \  }
}|
|||
k     rd}t          |          |!|
||
dk    r|
|	fn|
|
|	z   fS |	|
||	z
  |fS n>|dk    rd}t          |          |
%|#t          |
||          sd	}t          |          |
|fS )
a#  Return the minimum and maximum values given a field_meta's constraints.

    :param t_type: A primitive constructor - int, float or Decimal.
    :param random: An instance of Random.
    :param lt: Less than value.
    :param le: Less than or equal value.
    :param gt: Greater than value.
    :param ge: Greater than or equal value.
    :param multiple_of: Multiple of value.
    :param decimal_places: Number of decimal places.
    :param max_digits: Maximal number of digits.

    :returns: a tuple of optional minimum and maximum values.
    rN   )rE   rF   rG   rH   r>   rI   rK   Nz0maximum value must be greater than minimum valuer   r;   zmultiple_of can not be zero)r   r   r.   z?given range should include at least one multiply of multiple_of)r   rT   r   r8   )r>   r   rE   rF   rG   rH   r.   rI   rK   seedr   r   rS   s                r   get_constrained_number_rangerW      s    2 6&--//B&''D("6jYg  GW w2w7H7H@ %%%7?&-llGT??'D.8QQ7?T>7**#/C$S)))#7QXfqrrr $ TC$S)))Gr!   method'NumberGeneratorProtocol[T]'c                    ||||n ||           S | || ||          S ||k    r|S |}t          ||          s1t           || ||          |z            |z  }t          ||          1|S )aR  Generate a constrained number, output depends on the passed in callbacks.

    :param random: An instance of random.
    :param minimum: A minimum value.
    :param maximum: A maximum value.
    :param multiple_of: A multiple of value.
    :param method: A function that generates numbers of type T.

    :returns: A value of type T.
    N)r   )r   r   r   )r=   r3   )r   r   r   r.   rX   results         r   generate_constrained_numberr\      s    " '/)5{{66;P;P;PPvVWgFFFFgF0EE lvvVWgNNNQ\\]]`kk 1EE lMr!   rA   c           	     p    t          ||||t          ||           \  }}t          | |||t                    S )a+  Handle constrained integers.

    :param random: An instance of Random.
    :param lt: Less than value.
    :param le: Less than or equal value.
    :param gt: Greater than value.
    :param ge: Greater than or equal value.
    :param multiple_of: Multiple of value.

    :returns: An integer.

    rG   rH   rE   rF   r>   r.   r   r   r   r   r.   rX   )rW   rA   r\   r   r   r.   rG   rH   rE   rF   r   r   s           r   handle_constrained_intra     sZ    * 4  GW '$   r!   float | Nonec           	     p    t          ||||t          ||           \  }}t          | |||t                    S )a%  Handle constrained floats.

    :param random: An instance of Random.
    :param lt: Less than value.
    :param le: Less than or equal value.
    :param gt: Greater than value.
    :param ge: Greater than or equal value.
    :param multiple_of: Multiple of value.

    :returns: A float.
    r^   r_   )rW   r(   r\   r   r`   s           r   handle_constrained_floatrd   4  sZ    ( 4  GW '"   r!   Decimal | NoneNonec                    t          dd|ff           | dk    rd}t          |          || |k     rd}t          |          dS dS )zValidate that max digits is greater than minimum and decimal places.

    :param max_digits: The maximal number of digits for the decimal.
    :param minimum: Minimal value.
    :param decimal_places: Number of decimal places

    :returns: 'None'

    z2.19.1r   )
parametersr   z!max_digits must be greater than 0Nz7max_digits must be greater or equal than decimal places)r   r   )rI   r   rK   rS   s       r   validate_max_digitsri   [  sl     $H9g:N9PQQQQQ1 %%%!j>&A&AG %%% "!&A&Ar!   generated_decimalr   c                   t                      5 }t          |_        t          |                               d                              d          }d}t          |          |k    r|\  }}|/t          |          |k    rt          | |          cddd           S |Qt          |          t          |          z   |k    r.|t          |          z
  }t          | |          cddd           S | cddd           S # 1 swxY w Y   dS )zHandle the length of the decimal.

    :param generated_decimal: A decimal value.
    :param decimal_places: Number of decimal places.
    :param max_digits: Maximal number of digits.

    z-0.   N)r   r   roundingstrstripsplitlenr3   )	rj   rK   rI   ctxlist_decimaldecimal_partswholedecimalsmax_decimalss	            r   handle_decimal_lengthry   t  s{    
 !3!,--33D99??DD|--*OE8)c(mmn.L.L.??! ! ! ! ! ! ! ! %#e**s8}}*Dz*Q*Q)CJJ6.==! ! ! ! ! ! ! ! !! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s   B C=AC=/C==DDc                    t          |||||||t          | 	  	        \  }}	|t          |d|           t          | ||	|t                    }
t          |
||          S )a  Handle a constrained decimal.

    :param random: An instance of Random.
    :param multiple_of: Multiple of value.
    :param decimal_places: Number of decimal places.
    :param max_digits: Maximal number of digits.
    :param lt: Less than value.
    :param le: Less than or equal value.
    :param gt: Greater than value.
    :param ge: Greater than or equal value.

    :returns: A decimal.

    )	rG   rH   rE   rF   r.   rI   rK   r>   r   N)rI   r   rK   r_   )rj   rI   rK   )rW   r   ri   r\   r   ry   )r   r.   rK   rI   rG   rH   rE   rF   r   r   rj   s              r   handle_constrained_decimalr{     s    0 4%
 
 
GW z4P^____3$   !+%   r!   )r'   r(   r)   r(   r&   r(   r   r*   )r   r   r   r   r.   r   r   r*   )r9   r   r.   r   r   r*   )r>   r?   r   r   )NNNNNN)r>   r?   rE   r   rF   r   rG   r   rH   r   rI   rJ   rK   rJ   r   rL   )NNNNNNN)r>   r?   r   r   rE   r   rF   r   rG   r   rH   r   r.   r   rI   rJ   rK   rJ   r   rL   )r   r   r   r   r   r   r.   r   rX   rY   r   r   )NNNNN)r   r   r.   rJ   rG   rJ   rH   rJ   rE   rJ   rF   rJ   r   rA   )r   r   r.   rb   rG   rb   rH   rb   rE   rb   rF   rb   r   r(   )rI   rA   r   re   rK   rJ   r   rf   )rj   r   rK   rJ   rI   rJ   r   r   )r   r   r.   re   rK   rJ   rI   rJ   rG   re   rH   re   rE   re   rF   re   r   r   )(
__future__r   decimalr   r   r   sysr   typingr   r	   r
   r   r   polyfactory.exceptionsr   polyfactory.utils.deprecationr   'polyfactory.value_generators.primitivesr   r   r   r   r   rA   r(   r   r   r-   r8   r=   rD   rT   rW   r\   ra   rd   ri   ry   r{   r   r!   r   <module>r      s   " " " " " " 5 5 5 5 5 5 5 5 5 5       > > > > > > > > > > > > > > 5 5 5 5 5 5 I I I I I I u u u u u u u u u u GC#u%%    hqk    KO 	+ 	+ 	+ 	+ 	+ 	+9 9 9 9xJ J J J% % % %" !!%2( 2( 2( 2( 2(p  !!%3 3 3 3 3l   > #$ $ $ $ $R !%$ $ $ $ $N& & & &2! ! ! !8 #'!%!3 3 3 3 3 3 3r!   