U
    0-e                     @   sX   d Z ddlZddlmZ ee edddZee ddd	Zdeeeed
ddZdS )aV  
A collection of utilities for comparing `examples/complete_*_example.py` scripts with the capabilities inside of each
`examples/by_feature` example. `compare_against_test` is the main function that should be used when testing, while the
others are used to either get the code that matters, or to preprocess them (such as stripping comments)
    N)List)linesnamec                 C   s   |dkr |dkr t d| dg d }}| D ]d}|sTd| |krTd}|| q.|r.|dkrpd|krp|  S |dkrd	|kr|  S || q.d
S )a.  
    Extracts a function from `lines` of segmented source code with the name `name`.

    Args:
        lines (`List[str]`):
            Source code of a script seperated by line.
        name (`str`):
            The name of the function to extract. Should be either `training_function` or `main`
    training_functionmainz Incorrect function name passed: z-, choose either 'main' or 'training_function'Fzdef Tzdef mainzif __name__N)
ValueErrorappend)r   r   Z
good_linesZfound_startline r
   _/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/accelerate/test_utils/examples.pyget_function_contents_by_name   s    


r   r   c                 C   s   dd | D S )z
    Filters `lines` and removes any entries that start with a comment ('#') or is just a newline ('
')

    Args:
        lines (`List[str]`):
            Source code of a script seperated by line.
    c                 S   s&   g | ]}|  d s|dkr|qS )#
)lstrip
startswith.0r	   r
   r
   r   
<listcomp><   s       zclean_lines.<locals>.<listcomp>r
   r   r
   r
   r   clean_lines4   s    r   )base_filenamefeature_filenameparser_onlysecondary_filenamec              	      s8  t | d}| }W 5 Q R X t tjtjddd}| }W 5 Q R X t |d}| }W 5 Q R X |dk	rt |d}| }W 5 Q R X |rtt|d}	tt|d}
tt|d}|dk	rtt|dnBtt|d}	tt|d}
tt|d}|dk	rtt|dd}g }g }t|}t	t
|d D ]X}||kr>t|}||
kr>| |kr>d	|kr|| || nt|}q>g g }t|	D ]J\}}||kr||
kr| |krd	|kr| || qfd
d|D }|dk	r4fdd|D   fdd|D }|S )a  
    Tests whether the additional code inside of `feature_filename` was implemented in `base_filename`. This should be
    used when testing to see if `complete_*_.py` examples have all of the implementations from each of the
    `examples/by_feature/*` scripts.

    It utilizes `nlp_example.py` to extract out all of the repeated training code, so that only the new additional code
    is examined and checked. If something *other* than `nlp_example.py` should be used, such as `cv_example.py` for the
    `complete_cv_example.py` script, it should be passed in for the `secondary_filename` parameter.

    Args:
        base_filename (`str` or `os.PathLike`):
            The filepath of a single "complete" example script to test, such as `examples/complete_cv_example.py`
        feature_filename (`str` or `os.PathLike`):
            The filepath of a single feature example script. The contents of this script are checked to see if they
            exist in `base_filename`
        parser_only (`bool`):
            Whether to compare only the `main()` sections in both files, or to compare the contents of
            `training_loop()`
        secondary_filename (`str`, *optional*):
            A potential secondary filepath that should be included in the check. This function extracts the base
            functionalities off of "examples/nlp_example.py", so if `base_filename` is a script other than
            `complete_nlp_example.py`, the template script should be included here. Such as `examples/cv_example.py`
    rZexamplesznlp_example.pyNr   r   zMtrain_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)
   ZTESTING_MOCKED_DATALOADERSc                    s   g | ]}| kr|qS r
   r
   r   )new_full_example_partsr
   r   r      s      z(compare_against_test.<locals>.<listcomp>c                    s   g | ]}| kr|qS r
   r
   r   )secondary_file_funcr
   r   r      s      c                    s   g | ]}| kr|qS r
   r
   r   )diff_from_twor
   r   r      s      )open	readlinesospathabspathjoinr   r   iterrangelennextr   r   	enumerate)r   r   r   r   fZbase_file_contentsZfull_file_contentsZfeature_file_contentsZsecondary_file_contentsZbase_file_funcZfull_file_funcZfeature_file_funcZ_dl_lineZnew_feature_codeZpassed_idxsitir	   _Zdiff_from_exampler
   )r   r   r   r   compare_against_test?   s^    







r.   )N)	__doc__r!   typingr   strr   r   boolr.   r
   r
   r
   r   <module>   s
   