B
    eJd!                 @   s   d dl mZ d dlZd dl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mZmZmZmZmZmZ yd dlmZ W n  ek
r   d dlmZ Y nX dd Zdd	 Zd
d Zdd Zdd Zdd Zdd Z dddZ!dddZ"dS )    )absolute_importN)
exceptions)DEFAULT_PLOTLY_COLORSPLOTLY_SCALEScolor_parsercolorscale_to_colorscolorscale_to_scaleconvert_to_RGB_255find_intermediate_color
hex_to_rgb	label_rgbn_colorsunconvert_from_RGB_255unlabel_rgbvalidate_colorsvalidate_colors_dictvalidate_colorscalevalidate_scale_values)Sequencec             C   s   t | tot | t S )N)
isinstancer   str)obj r   M/var/www/html/venv/lib/python3.7/site-packages/plotly/figure_factory/utils.pyis_sequence   s    r   c                sj   ddl m  t| d  r<t fdd| D sftdn*t| d trftdd | D sftddS )z
    Validates if a list contains all numbers or all strings

    :raises: (PlotlyError) If there are any two items in the list whose
        types differ
    r   )Numberc             3   s   | ]}t | V  qd S )N)r   ).0item)r   r   r   	<genexpr>-   s    z!validate_index.<locals>.<genexpr>z^Error in indexing column. Make sure all entries of each column are all numbers or all strings.c             s   s   | ]}t |tV  qd S )N)r   r   )r   r   r   r   r   r   6   s    N)numbersr   r   allr   PlotlyErrorr   )Z
index_valsr   )r   r   validate_index#   s    r"   c                sx   ddl m  xf| D ]^}t|d  rFt fdd|D sptdqt|d trtdd |D stdqW dS )z
    Validates all strings or numbers in each dataframe column

    :raises: (PlotlyError) If there are any two items in any list whose
        types differ
    r   )r   c             3   s   | ]}t | V  qd S )N)r   )r   r   )r   r   r   r   J   s    z%validate_dataframe.<locals>.<genexpr>zWError in dataframe. Make sure all entries of each column are either numbers or strings.c             s   s   | ]}t |tV  qd S )N)r   r   )r   r   r   r   r   r   R   s    N)r   r   r   r    r   r!   r   )arrayZvectorr   )r   r   validate_dataframe?   s    
r$   c                 s0   t | d  t fdd| D r,tddS )z
    Validates that data lists or ndarrays are the same length.

    :raises: (PlotlyError) If any data lists are not the same length.
    r   c             3   s   | ]}t | kV  qd S )N)len)r   lst)lengthr   r   r   b   s    z(validate_equal_length.<locals>.<genexpr>z<Oops! Your data lists or ndarrays should be the same length.N)r%   anyr   r!   )argsr   )r'   r   validate_equal_length[   s    r*   c           
   K   s^   xX|   D ]L\}}y|dkr,td||W q
 tk
rT   td||Y q
X q
W dS )z
    Validates that all values given in key/val pairs are positive.

    Accepts kwargs to improve Exception messages.

    :raises: (PlotlyError) If any value is < 0 or raises.
    r   z{} must be > 0, got {}z{} must be a number, got {}N)items
ValueErrorformat	TypeErrorr   r!   )kwargskeyvalr   r   r   validate_positive_scalarsh   s    r2   c             C   s2   ydd | D S  t k
r,   tdY nX dS )z
    Uses list comprehension to flatten array

    :param (array): An iterable to flatten
    :raises (PlotlyError): If iterable is not nested.
    :rtype (list): The flattened list.
    c             S   s   g | ]}|D ]}|qqS r   r   )r   Zsublistr   r   r   r   
<listcomp>   s    zflatten.<locals>.<listcomp>z\Your data array could not be flattened! Make sure your data is entered as lists or ndarrays!N)r.   r   r!   )r#   r   r   r   flattenx   s
    r4   c             C   s   t | }t| ts&t| ts&tdx | D ]}t|tr,tdq,W xt|d D ]"}| | | |d  krVtdqVW g }|t	d| d g xBt|d D ]2}g }|| |  || |d   || qW || |d  t	dg |S dS )a  
    Returns a list of intervals for categorical colormaps

    Accepts a list or tuple of sequentially increasing numbers and returns
    a list representation of the mathematical intervals with these numbers
    as endpoints. For example, [1, 6] returns [[-inf, 1], [1, 6], [6, inf]]

    :raises: (PlotlyError) If input is not a list or tuple
    :raises: (PlotlyError) If the input contains a string
    :raises: (PlotlyError) If any number does not increase after the
        previous one in the sequence
    zZThe intervals_endpts argument must be a list or tuple of a sequence of increasing numbers.   z-infr   infN)
r%   r   tuplelistr   r!   r   rangeappendfloat)Zendptsr'   r   kZ	intervalsintervalr   r   r   endpts_to_intervals   s*    



r>   colT#0f0f0fc             C   s  d|d |  | }|std}	d}
|dkrJ|d ||  d|  }d}d}q|dkr|d ||  d|  }d}d	}np|dkrd}	d
}
|d ||  d|  }d}d}n>|dkrd}
|d ||  d|  }|rd}d}	nd}d}	d}t ||	|
||ddd| t d|dd
}|S )a#  
    Returns annotation dict for label of n labels of a 1xn or nx1 subplot.

    :param (str) text: the text for a label.
    :param (int) lane: the label number for text. From 1 to n inclusive.
    :param (int) num_of_lanes: the number 'n' of rows or columns in subplot.
    :param (float) subplot_spacing: the value for the horizontal_spacing and
        vertical_spacing params in your plotly.tools.make_subplots() call.
    :param (str) row_col: choose whether labels are placed along rows or
        columns.
    :param (bool) flipped: flips text by 90 degrees. Text is printed
        horizontally if set to True and row_col='row', or if False and
        row_col='col'.
    :param (bool) right_side: only applicable if row_col is set to 'row'.
    :param (str) text_color: color of the text.
    r5   centermiddler?   g      ?g{Gz?r   rowZ   bottomg      ?i  leftg{GzrightFZpaper   )sizecolor)
	textanglexanchoryanchorxyZ	showarrowZxrefZyreftextZfont)dict)rP   ZlaneZnum_of_lanesZsubplot_spacingZrow_colZflippedZ
right_sideZ
text_colorlrL   rM   rN   rO   rK   Zannotation_dictr   r   r   annotation_dict_for_label   sN    rS   andc             C   sD   t | dk rtdt | d d d | d |d  }|j|  S )z
    Returns an English listing of objects seperated by commas ','

    For example, ['foo', 'bar', 'baz'] becomes 'foo, bar and baz'
    if the conjunction 'and' is selected.
       z1Your list or tuple must contain at least 2 items.z{}, z{} z {}.)r%   r   r!   r-   )iterableZconjZperiodtemplater   r   r   list_of_options  s
    $rY   )r?   TTr@   )rT   T)#
__future__r   decimalZplotlyr   Zplotly.colorsr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   collections.abcr   ImportErrorcollectionsr   r"   r$   r*   r2   r4   r>   rS   rY   r   r   r   r   <module>   s&   H:   
?