
    wiG;              #       8   d dl Z d dlZd dlZd dlZd dlmZmZ d dlm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlmZ d dlmZmZ d dlmZ eeeee         f                  Z edd	           G d
 d                      Z edd	           G d d                      Zdedeeef         dee         dee         deeeef                  dee         dee         dedeee e	f                  dee          dee         dee         dee         deee         eee         eee e	f         f         fdZ!deeef         dee         dee         fdZ"	 d'de	de d eed!f         d"ed#ee         ddfd$Z#deee e	f                  d%ee         dee          dee e	f         fd&Z$dS )(    N)	dataclassfield)
AnyCallableDictIterableListOptionalSizedTupleTypeUnion)TqdmKeyError)DEFAULT_START_METHODRUNNING_MACOS)get_tqdmTF)initfrozenc                      e Zd ZU dZee         ed<    edd          Zeed<   ee	         ed<    edd          Z
e	ed<   dZeed	<   d
Zeed<   dZeed<   dZeed<   eZeed<   dZeed<   dZeed<   dZeed<   dZeed<   edee         fd            Zej        dee         dd
fd            Zede	fd            Zej        dee	         dd
fd            Zdee	         de	fdZd
S )WorkerPoolParamsz@
    Data class for all :obj:`mpire.WorkerPool` parameters.
    n_jobsF)r   repr_n_jobscpu_ids_cpu_idsTdaemonNshared_objectspass_worker_iduse_worker_statestart_method
keep_aliveuse_dillenable_insightsorder_tasksreturnc                     | j         S N)r   selfs    d/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/mpire/params.pyr   zWorkerPoolParams.n_jobs$   s
    |    c                 :    |pt          j                    | _        d S r'   )mp	cpu_countr   )r)   r   s     r*   r   zWorkerPoolParams.n_jobs(   s    /r+   c                     | j         S r'   )r   r(   s    r*   r   zWorkerPoolParams.cpu_ids,   s
    }r+   c                 :    |                      |          | _        d S r'   )_check_cpu_idsr   )r)   r   s     r*   r   zWorkerPoolParams.cpu_ids0   s    ++G44r+   c                    g }|rt           rt          j        dt                     t	          |          dk    r>t	          |          | j        k    r&t          dt	          |          | j        fz            d}d}|D ]}t          |t                    rd|	                    |           t          |t          d |D                                 }t          |t          d |D                                 }{t          |t                    r7|	                    |g           t          ||          }t          ||          }t          d          |t          j                    k    r(t          d|t          j                    dz
  fz            |dk     rt          d	          t	          |          dk    r-t          t!          j        |d         | j                            }|S )
ai  
        Checks the cpu_ids parameter for correctness

        :param cpu_ids: List of CPU IDs to use for pinning child processes to specific CPUs. The list must be as long as
            the number of jobs used (if ``n_jobs`` equals ``None`` it must be equal to ``mpire.cpu_count()``), or the
            list must have exactly one element. In the former case, element x specifies the CPU ID(s) to use for child
            process x. In the latter case the single element specifies the CPU ID(s) for all child  processes to use. A
            single element can be either a single integer specifying a single CPU ID, or a list of integers specifying
            that a single child process can make use of multiple CPU IDs. If ``None``, CPU pinning will be disabled.
            Note that CPU pinning may only work on Linux based systems
        :return: cpu_ids
        zJSetting CPU affinity is not supported on MacOS. Ignoring cpu_ids parameter   z9Number of CPU IDs (%d) does not match number of jobs (%d)r   c              3      K   | ]}|V  d S r'    .0cpus     r*   	<genexpr>z2WorkerPoolParams._check_cpu_ids.<locals>.<genexpr>S   "      4K4KSS4K4K4K4K4K4Kr+   c              3      K   | ]}|V  d S r'   r5   r6   s     r*   r9   z2WorkerPoolParams._check_cpu_ids.<locals>.<genexpr>T   r:   r+   z3CPU ID(s) must be either a list or a single integerzACPU ID %d exceeds the maximum CPU ID available on your system: %dzCPU IDs cannot be negative)r   warningswarnRuntimeWarninglenr   
ValueError
isinstancelistappendmaxminint	TypeErrorr-   r.   	itertoolsrepeat)r)   r   converted_cpu_ids
max_cpu_id
min_cpu_idcpu_ids         r*   r1   zWorkerPoolParams._check_cpu_ids4   s
      	? .j,. . . 7||q  S\\T[%@%@ !\"%g,,!<"= > > > JJ! 
[ 
[fd++ 	[%,,V444!$Z4K4KF4K4K4K1K1K!L!LJ!$Z4K4KF4K4K4K1K1K!L!LJJ,, [%,,fX666!$Z!8!8J!$Z!8!8JJ#$YZZZ R\^^++ !d",blnnq.@!A"B C C C A~~ !=>>>  !!Q&& $Y%56G6JDK%X%X Y Y  r+   )__name__
__module____qualname____doc__r
   rF   __annotations__r   r   CPUListr   r   boolr   r   r   r   r   r    strr!   r"   r#   r$   propertyr   setterr   r1   r5   r+   r*   r   r      s          SM5e%000GS000g5u555Hg555FDNC ND   "d""",L#,,,JHd!OT!!!K    X ]0Xc] 0t 0 0 0 ]0     X ^5x0 5T 5 5 5 ^55!hw&7 5!G 5! 5! 5! 5! 5! 5!r+   r   c                       e Zd ZU dZeed<   dZee         ed<   dZee         ed<   dZ	ee
         ed<   dZeed<   dZee         ed	<   dZee         ed
<   dZee         ed<   dd defdZdS )WorkerMapParamszk
    Data class for all :meth:`mpire.WorkerPool.map` parameters that need to be passed on to a worker.
    funcNworker_initworker_exitworker_lifespanFprogress_bartask_timeoutworker_init_timeoutworker_exit_timeoutotherr%   c                 z   |j         | j         k    s|j        | j        k    rt          j        dt          d           |j        | j        k    oo|j         | j         k    o_|j        | j        k    oO|j        | j        k    o?|j        | j        k    o/|j        | j        k    o|j	        | j	        k    o|j
        | j
        k    S )zg
        :param other: Other WorkerMapConfig
        :return: Whether the configs are the same
        a  You're changing either the worker_init and/or worker_exit function while keep_alive is enabled. Be aware this can have undesired side-effects as worker_init functions are only executed when a worker is started and worker_exit functions when a worker is terminated.   
stacklevel)r[   r\   r<   r=   r>   rZ   r]   r^   r_   r`   ra   )r)   rb   s     r*   __eq__zWorkerMapParams.__eq__   s    
  000E4EIY4Y4YM u )Q8 8 8 8
 
di' F!T%55F!T%55F %)==F "d&77	F
 "d&77F )T-EEF )T-EE	Gr+   )rN   rO   rP   rQ   r   rR   r[   r
   r\   r]   rF   r^   rT   r_   floatr`   ra   rg   r5   r+   r*   rY   rY   l   s           NNN&*K(#***&*K(#*** &*OXc]))) L$ %)L(5/(((+/%///+/%///G- G$ G G G G G Gr+   rY   pool_paramsiterable_of_argsiterable_lenmax_tasks_active
chunk_sizen_splitsr]   r^   progress_bar_optionsprogress_bar_styler_   r`   ra   r%   c                 b   t          ||          }t          |dt          t          fdd           t          |dt          fdd           |8||||z  }n.|t	          j        dt          d	           d
}n|| j        dz  z  }|-| j        t          t          j	        |                    z  dz  }nt          |dt          fdd           t          |dt          fdd           t          |||	          }|
df|df|dffD ]%\  }}t          ||t          t          fdd           &|||||fS )aV  
    Check the parameters provided to any (i)map function. Also extracts the number of tasks and can modify the
    ``chunk_size`` and ``progress_bar`` parameters.

    :param pool_params: WorkerPool config
    :param iterable_of_args: A numpy array or an iterable containing tuples of arguments to pass to a worker
    :param iterable_len: Number of elements in the ``iterable_of_args``
    :param max_tasks_active: Maximum number of active tasks in the queue. Use ``None`` to not limit the queue
    :param chunk_size: Number of simultaneous tasks to give to a worker. If ``None`` it will generate ``n_jobs * 4``
        number of chunks
    :param n_splits: Number of splits to use when ``chunk_size`` is ``None``
    :param worker_lifespan: Number of chunks a worker can handle before it is restarted. If ``None``, workers will
        stay alive the entire time. Use this when workers use up too much memory over the course of time
    :param progress_bar: When ``True`` it will display a progress bar
    :param progress_bar_options: Dictionary containing keyword arguments to pass to the ``tqdm`` progress bar. See
         ``tqdm.tqdm()`` for details. The arguments ``total`` and ``leave`` will be overwritten by MPIRE.
    :param progress_bar_style: Style to use for the progress bar
    :param task_timeout: Timeout in seconds for a single task
    :param worker_init_timeout: Timeout in seconds for the ``worker_init`` function
    :param worker_exit_timeout: Timeout in seconds for the ``worker_exit`` function
    :return: Number of tasks, max tasks active, chunk size, progress bar, progress bar options
    rm   Tr3   )allowed_typesnone_allowedmin_rn   NzFailed to obtain length of iterable when chunk size or number of splits is None. Chunk size is set to 4. Remedy: either provide an iterable with a len() function or specify iterable_len in the function callrd   re      @   rl   r]   r_   r`   ra   g:0yE>)get_number_of_taskscheck_numberrF   rh   r<   r=   r>   r   mathceilcheck_progress_bar_options)ri   rj   rk   rl   rm   rn   r]   r^   ro   rp   r_   r`   ra   n_taskstimeout_vartimeout_var_names                   r*   check_map_parametersr      s   > ""2LAAG\#uTX_`aaaa:cV$UVWWWW G$7 8+JJ BCQ^_a a a a 

$(:R(?@
 &-DIj4I4I0J0JJQN%'9#^bijkkkk "3C6X\cdeeee 66JGUghh ,8*H+>@U*V+>@U*V*X n n%% 	["23,]ahlmmmmm$j,@TTTr+   c                 L    ||S t          | d          rt          |           S dS )a  
    Get the number of tasks to process. If iterable_len is provided, it will be used. Otherwise, if iterable_of_args
    is a Sized object, len(iterable_of_args) will be used. Otherwise, None will be returned.

    :param iterable_of_args: A numpy array or an iterable containing tuples of arguments to pass to a worker
    :param iterable_len: Number of elements in the ``iterable_of_args``
    :return: Number of tasks to process
    N__len__)hasattrr?   )rj   rk   s     r*   rw   rw      s7     ++ %#$$$4r+   varvar_namerr   .rs   rt   c                     |r| dS t          | |          st          | d|           || |k     rt          | d|           dS dS )aq  
    Check that a variable is of the correct type and within the allowed range

    :param var: Variable to check
    :param var_name: Name of the variable
    :param allowed_types: Allowed types for the variable
    :param none_allowed: Whether None is allowed for the variable
    :param min_: Minimum value for the variable. If None, no minimum value is checked
    Nz should be of type z should be >= )rA   rG   r@   )r   r   rr   rs   rt   s        r*   rx   rx      sz      c=)) I8GGGGHHHC$JJH::D::;;; JJr+   r|   c                    | pi } t          | t                    st          d          d| v rt          j        dt
          d           d| v rt          j        dt
          d           |dk    rd	| v rt          d
          ddddd| |dd} t          |          }	 |                    |            n)# t          t          f$ r}|t          d          d}~ww xY w| S )a  
    Check that the progress bar options are properly formatted and set some defaults

    :param progress_bar_options: Dictionary containing keyword arguments to pass to the ``tqdm`` progress bar. See
         ``tqdm.tqdm()`` for details. The arguments ``total`` and ``leave`` will be overwritten by MPIRE.
    :param n_tasks: Number of tasks to process
    :param progress_bar_style: Progress bar style to use
    :return: Dictionary containing the progress bar options
    z+progress_bar_options should be a dictionarytotalzThe 'total' keyword argument is overwritten by MPIRE. Set the total number of tasks to process using the iterable_len parameterrd   re   leavez9The 'leave' keyword argument will be overwritten by MPIRErichpositionzJThe 'position' parameter is currently not supported for rich progress barsr   Tg?g      ?)r   dynamic_ncolsminintervalmaxinterval)r   r   zThere's an error in progress_bar_options. Either one of the parameters doesn't exist or it's not properly formatted. See tqdm.tqdm() for details.N)rA   dictrG   r<   r=   r>   NotImplementedErrorr   check_optionsr   r@   )ro   r|   rp   tqdmes        r*   r{   r{      sj    052*D11 GEFFF&&& 9:HUV	X 	X 	X 	X&&&QSanopppp V##
6J(J(J!"nooo 
 
 
 
 
 
 &''D
`/0000)$ ` ` `Z !_ ` ` 	``  s    B6 6CCCr'   )%rH   ry   multiprocessingr-   r<   dataclassesr   r   typingr   r   r   r   r	   r
   r   r   r   r   r   r   mpire.contextr   r   mpire.tqdm_utilsr   rF   rS   r   rY   rh   rT   rU   r   rw   rx   r{   r5   r+   r*   <module>r      s             ( ( ( ( ( ( ( ( [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [ [       = = = = = = = = % % % % % % uS$s)^$
% U###W! W! W! W! W! W! W! $#W!t T"""&G &G &G &G &G &G &G #"&GRBU&6 BU%PUW_P_J` BU'/}BUHPQTBU%-eCJ.?%@BULTUXMBU +33-BU HLBU 08S#X/G	BU ^ffi]j	BU
 (0BU
 NVV[_BU /7uoBU #Xc]D$sCx.HIBU BU BU BUJ%x*@ PXY\P] bjknbo    & *.< <c <S <tSy9I <Y] <<26< < < <&5 Xd38n5M 5 X`adXe 5 3;C=5 EI#s(^5  5  5  5  5  5 r+   