
    Hfht                         d Z ddlZddlZddlmZ ddlmZ ddlmZ	 ddlm
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ  G d de          Zd Zd ZddZd Zd ZdS )a  
    pyexcel.internal.sheets.matrix
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Matrix, a data model that accepts any types, spread sheet style
    of lookup.

    :copyright: (c) 2014-2022 by Onni Software Ltd.
    :license: New BSD License, see LICENSE for more details
    N)partial)chain)_compact)	constants)	SheetMeta)Row)Column)	to_format)PyexcelList   )_sharedc                   $   e Zd ZdZd Zd Zd Zd Zd Zd Z	d0d	Z
d
 Zd Zd1dZd Zd Zd Zd Zd1dZd Zd Zd Zd Zd Zd2dZd Zd Zd Zd Zd Zd Zd Zd  Z d! Z!d" Z"d# Z#d$ Z$d% Z%d& Z&d' Z'd( Z(d) Z)d2d*Z*d+ Z+d, Z,d- Z-d. Z.d/ Z/dS )3Matrixz]The internal representation of a sheet data. Each element
    can be of any python types
    c                 b   t          |t          j                  r*t          t	          |                    \  | _        | _        n;	 t          |          \  | _        | _        n# t          $ r t          d          w xY wt          |           | _	        t          |           | _        d| _        dS )zConstructor

        The reason a deep copy was not made here is because
        the data sheet could be huge. It could be costly to
        copy every cell to a new memory area
        :param list array: a list of arrays
        zInvalid two dimensional arraymatrixN)
isinstancetypesGeneratorTypeuniformlist_Matrix__width_Matrix__array	TypeErrorr   rowr	   columnname)selfarrays     `/var/www/ffwdstore.be/apps/py-env/lib/python3.11/site-packages/pyexcel/internal/sheets/matrix.py__init__zMatrix.__init__    s     eU011 	A)0e)=)=&DL$,,A-4U^^*dll A A A ?@@@At99Tll			s   A# #A=c                     | j         S )zpresent internal arrayr   r   s    r   get_internal_arrayzMatrix.get_internal_array3   
    |    c                 *    t          | j                  S )zThe number of rows)lenr   r#   s    r   number_of_rowszMatrix.number_of_rows7   s    4<   r&   c                 D    |                                  dk    r| j        S dS )zThe number of columnsr   )r)   r   r#   s    r   number_of_columnszMatrix.number_of_columns;   s&      1$$<1r&   c                 N    t          j        |                                           S )z3
        Utility function to get row range
        )compactiranger)   r#   s    r   	row_rangezMatrix.row_rangeB   s      ~d1133444r&   c                 N    t          j        |                                           S )z6
        Utility function to get column range
        )r-   r.   r+   r#   s    r   column_rangezMatrix.column_rangeH   s      ~d4466777r&   Nc                 2   ||                                  k     o||                                 k     }|$|r| j        |         |         S t          d          |s.t	          | j        |dz   |dz             \  }}||c| _        | _        || j        |         |<   dS )zRandom access to table cells

        :param int row: row index which starts from 0
        :param int column: column index which starts from 0
        :param any new_value: new value if this is to set the value
        NzIndex out of ranger   )r)   r+   r   
IndexErrorr   r   )r   r   r   	new_valuefitwidthr   s          r   
cell_valuezMatrix.cell_valueN   s     D'')))Oft7M7M7O7O.O 7|C(00 !5666 :&t|S1WfqjIIu-2E*dl(1DLf%%%r&   c                    ||                                  v r,t          t          j        | j        |                             S |dk     rit          j        |          |                                  v rAt          t          j        | j        ||                                 z                                S t          t          j
                  )z4
        Gets the data at the specified row
        r   )r/   r   copydeepcopyr   utilsabsr)   r3   r   MESSAGE_INDEX_OUT_OF_RANGE)r   indexs     r   row_atzMatrix.row_atb   s     DNN$$$$t}T\%-@AABBBQYY59U++t~~/?/???dl543F3F3H3H+HIJJ  
 YABBBr&   c                    |                                  }||k     rT|| j        |<   t          |          |                                 k    r#t	          | j                  \  | _        | _        dS dS t          t          j                  )zUpdate a row data rangeN)	r)   r   r(   r+   r   r   r3   r   r=   )r   	row_index
data_arraynrowss       r   
set_row_atzMatrix.set_row_atq   s~    ##%%u&0DL#:$"8"8":":::-4T\-B-B*dlll ;: YABBBr&   r   c                    |                                  }|                                 }||k     r||k     rt          |          |z   }t          ||          }t	          ||          D ]"}|                     |||||z
                      #||k    r%||z
  }	| j        |         ||	d         z   | j        |<   t          | j                  \  | _        | _        dS t          t          j                  )aH  Update a row data range

        It works like this if the call is: set_row_at(2, ['N', 'N', 'N'], 1)::

            A B C
            1 3 5
            2 N N <- row_index = 2
              ^starting = 1

        This function will not set element outside the current table range

        :param int row_index: which row to be modified
        :param list data_array: one dimensional array
        :param int starting: from which index, the update happens
        :raises IndexError: if row_index exceeds row range or starting
                            exceeds column range
        N)r)   r+   r(   minranger7   r   r   r   r3   r   r=   )
r   rA   rB   startingrC   ncolumnsreal_lenendilefts
             r   _set_row_atzMatrix._set_row_at{   s    $ ##%%))++uH!4!4:1Hh))C8S)) H H	1jX.FGGGG(""(*L+j.?? Y' *1)>)>&DL$,,,YABBBr&   c                     t          j        |          }t          j        |t                    r| xj        |z  c_        d S | j                            |           d S N)r9   r:   r-   is_array_typer   r   append)r   r   r   s      r   _extend_rowzMatrix._extend_row   sV    c"" -- 	'LLE!LLLLL&&&&&r&   c                     t          |t                    r8|                     |           t          | j                  \  | _        | _        dS t          dt          |          z            )z1Inserts two dimensional data after the bottom rowzCannot use %sN)r   r   rS   r   r   r   r   typer   rowss     r   extend_rowszMatrix.extend_rows   s]    dD!! 	:T""")0)>)>&DL$,,,Od4jj8999r&   c                    t          |t                    du rt          t          |          dk    rKt	          |          }t          |d          }|D ]*}||                                 k     r|dk    r| j        |= )dS dS )zDeletes specified row indicesFr   TreverseN)r   r   r3   r(   _uniquesortedr)   r   )r   row_indicesunique_listsorted_listrL   s        r   delete_rowszMatrix.delete_rows   s    k4((E11{a!+..K d;;;K  ( (t**,,,,aQ  ( (r&   c                    t                      }||                                 v rB|                                 D ]+}|                    |                     ||                     ,|S |dk     rt          j        |          |                                 v rY|                                 |z   }|                                 D ]+}|                    |                     ||                     ,|S t          t          j
                  )z7
        Gets the data at the specified column
        r   )r   r1   r/   rR   r7   r;   r<   r+   r3   r   r=   )r   r>   
cell_arrayrL   reverse_indexs        r   	column_atzMatrix.column_at   s     !]]
D%%''''^^%% = =!!$//!U";";<<<<QYY59U++t/@/@/B/BBB 2244u<M^^%% E E!!$//!]"C"CDDDD YABBBr&   c                    |                                  }|                                 }||k     r||k     rt          |          |z   }t          ||          }t	          ||          D ]"}|                     |||||z
                      #||k    r@t	          ||          D ]/}dg|z  |||z
           gz   }	| j                            |	           0t          | j                  \  | _	        | _        dS t          t          j                  )ay  Updates a column data range

        It works like this if the call is:
        set_column_at(2, ['N','N', 'N'], 1)::

                +--> column_index = 2
                |
            A B C
            1 3 N <- starting = 1
            2 4 N

        This function will not set element outside the current table range

        :param int column_index: which column to be modified
        :param list data_array: one dimensional array
        :param int staring: from which index, the update happens
        :raises IndexError: if column_index exceeds column range
                            or starting exceeds row range
         N)r)   r+   r(   rF   rG   r7   r   rR   r   r   r3   r   r=   )
r   column_indexrB   rH   rC   rI   rJ   rK   rL   new_rows
             r   set_column_atzMatrix.set_column_at   s   ( ##%%))++(""x%'7'7:1Hh&&C8S)) K K<AL1IJJJJ%uh// 1 1A!d\1ZH5M4NNGL''0000)0)>)>&DL$,,,YABBBr&   c                     t          |t                    st          t          j                  |}t          j        |t                    s|g}t          |          }|                     |           dS )zInserts two dimensional data after the rightmost column

        This is how it works:

        Given::

            s s s     t t

        Get::

            s s s  +  t t
        N)	r   r   r   r   %MESSAGE_DATA_ERROR_DATA_TYPE_MISMATCHr-   rQ   	transpose_extend_columns_with_rows)r   columnsincoming_datas      r   extend_columnszMatrix.extend_columns   sp     '4(( 	MIKLLL$Wd33 	&$IM!-00&&}55555r&   c                 
   |                                  }|                                 }t          |          }t          ||          }t	          d|          D ]1}t          j        ||                   }| j        |xx         |z  cc<   2||k     rR||z
  }|}	t	          d|          D ]:}t          j	        g|z  }
|
||	|z            z  }
| j        
                    |
           ;t          | j                  \  | _        | _        d S )Nr   )r)   r+   r(   rF   rG   r9   r:   r   r   
DEFAULT_NArR   r   r   )r   rW   current_nrowscurrent_ncolsinsert_column_nrowsarray_lengthrL   r   deltabase	new_arrays              r   rn   z Matrix._extend_columns_with_rows  s   ++--..00!$ii=*=>>q,'' 	% 	%AM$q'**ELOOOu$OOOO...'-7E D1e__ / /&12]B	T$(^+	##I....%,T\%:%:"dlllr&   c                 0    |                      |           dS )aK  Rows were appended to the rightmost side

        example::

            >>> import pyexcel as pe
            >>> data = [
            ...     [1],
            ...     [2],
            ...     [3]
            ... ]
            >>> matrix = pe.Sheet(data)
            >>> matrix
            pyexcel sheet:
            +---+
            | 1 |
            +---+
            | 2 |
            +---+
            | 3 |
            +---+
            >>> rows = [
            ...      [11, 11],
            ...      [22, 22]
            ... ]
            >>> matrix.extend_columns_with_rows(rows)
            >>> matrix
            pyexcel sheet:
            +---+----+----+
            | 1 | 11 | 11 |
            +---+----+----+
            | 2 | 22 | 22 |
            +---+----+----+
            | 3 |    |    |
            +---+----+----+
        N)rn   rV   s     r   extend_columns_with_rowszMatrix.extend_columns_with_rows  s    H 	&&t,,,,,r&   c                    g }t          |d         |                                           }t          |d         |                                           }t          |d         |          D ][}g }t          |d         |          D ]+}|                    |                     ||                     ,|                    |           \|S )zGet a rectangle shaped data out

        :param slice topleft_corner: the top left corner of the rectangle
        :param slice bottomright_corner: the bottom right
                                         corner of the rectangle
        r   r   )rF   r)   r+   rG   rR   r7   )	r   topleft_cornerbottomright_cornerregionmax_rowmax_colr   tmp_rowr   s	            r   r   zMatrix.region9  s     (+T-@-@-B-BCC(+T-C-C-E-EFF*G44 	# 	#CGq 17;; = =tsF;;<<<<MM'""""r&   c                     |                      ||          }t          |d         |d                   D ]B}t          |d         |d                   D ]#}|                     ||t          j                   $C|S )a	  Get a rectangle shaped data out and clear them in position

        :param slice topleft_corner: the top left corner of the rectangle
        :param slice bottomright_corner: the bottom right
                                         corner of the rectangle
        r   r   )r   rG   r7   r   rs   )r   r~   r   r   r   r   s         r   cutz
Matrix.cutJ  s     ^-?@@*,>q,ABB 	C 	CCq 13Ea3HII C CVY-ABBBBCr&   c                     |r|                      ||           dS |r|                     ||           dS t          t          j                  )a  Paste a rectangle shaped data after a position

        :param slice topleft_corner: the top left corner of the rectangle

        example::

            >>> import pyexcel as pe
            >>> data = [
            ...     # 0 1  2  3  4 5   6
            ...     [1, 2, 3, 4, 5, 6, 7], #  0
            ...     [21, 22, 23, 24, 25, 26, 27],
            ...     [31, 32, 33, 34, 35, 36, 37],
            ...     [41, 42, 43, 44, 45, 46, 47],
            ...     [51, 52, 53, 54, 55, 56, 57]  # 4
            ... ]
            >>> s = pe.Sheet(data)
            >>> # cut  1<= row < 4, 1<= column < 5
            >>> data = s.cut([1, 1], [4, 5])
            >>> s.paste([4,6], rows=data)
            >>> s
            pyexcel sheet:
            +----+----+----+----+----+----+----+----+----+----+
            | 1  | 2  | 3  | 4  | 5  | 6  | 7  |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+
            | 21 |    |    |    |    | 26 | 27 |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+
            | 31 |    |    |    |    | 36 | 37 |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+
            | 41 |    |    |    |    | 46 | 47 |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+
            | 51 | 52 | 53 | 54 | 55 | 56 | 22 | 23 | 24 | 25 |
            +----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    | 32 | 33 | 34 | 35 |
            +----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    | 42 | 43 | 44 | 45 |
            +----+----+----+----+----+----+----+----+----+----+
            >>> s.paste([6,9], columns=data)
            >>> s
            pyexcel sheet:
            +----+----+----+----+----+----+----+----+----+----+----+----+
            | 1  | 2  | 3  | 4  | 5  | 6  | 7  |    |    |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            | 21 |    |    |    |    | 26 | 27 |    |    |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            | 31 |    |    |    |    | 36 | 37 |    |    |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            | 41 |    |    |    |    | 46 | 47 |    |    |    |    |    |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            | 51 | 52 | 53 | 54 | 55 | 56 | 22 | 23 | 24 | 25 |    |    |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    | 32 | 33 | 34 | 35 |    |    |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    | 42 | 43 | 44 | 22 | 32 | 42 |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    |    |    |    | 23 | 33 | 43 |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    |    |    |    | 24 | 34 | 44 |
            +----+----+----+----+----+----+----+----+----+----+----+----+
            |    |    |    |    |    |    |    |    |    | 25 | 35 | 45 |
            +----+----+----+----+----+----+----+----+----+----+----+----+

        N)_paste_rows_paste_columns
ValueErrorr    MESSAGE_DATA_ERROR_EMPTY_CONTENT)r   r~   rW   ro   s       r   pastezMatrix.pasteW  s`    ~  	I^T22222 	I88888YGHHHr&   c                 F   |\  }}|                                  }|                                 }||z
  }|dk    rEt          ||          fdt          j        |          D             }|                     |           |                                  }t          |          D ]]\  }	}
||	z   }||k     r|                     ||
|d                    /t          j	        g|d         z  |
z   }|                     |           ^t          | j                  \  | _        | _        d S )Nr   c                 L    g | ] }d  t          j                  D             !S )c                 &    g | ]}t           j        S  )r   rs   ).0_s     r   
<listcomp>z1Matrix._paste_rows.<locals>.<listcomp>.<listcomp>  s    KKK!%KKKr&   )r-   r.   )r   __max_columnss     r   r   z&Matrix._paste_rows.<locals>.<listcomp>  sB        LKw~k/J/JKKK  r&   r   rH   )r)   r+   maxr-   r.   rS   	enumeraterN   r   rs   r   r   r   )r   r~   rW   starting_rowstarting_columnr)   r+   rx   	empty_rowr>   r   	set_indexreal_rowr   s                @r   r   zMatrix._paste_rows  sJ   (6%o,,.. 2244~-199o/@AAK   !.//  I Y''',,..#D// 	+ 	+JE3$u,I>))  C.:K LLLL%01N14EEK  ****%,T\%:%:"dlllr&   c                 `   |d         }|                                  }t          |          D ]`\  }}||z   }||k     r|                     |||d                    /t          j        g|d         z  }||z  }|                     |g           at          | j                  \  | _        | _        d S )Nr   r   r   )	r+   r   rj   r   rs   rq   r   r   r   )	r   r~   ro   r   r+   r>   r   r   real_columns	            r   r   zMatrix._paste_columns  s    (+ 2244&w// 		3 		3ME6'%/I,,,""vq0A #      )34~a7HHv%##[M2222%,T\%:%:"dlllr&   c                    t          |t                    du rt          t          j                  t          |          dk    rt          |          }t          |d          }|                                 D ]3}|D ].}|| 	                                k     r|dk    r| j
        |         |= /4t          | j
                  | _        dS dS )z+Delete columns by specified list of indicesFr   TrZ   N)r   r   r   r   rl   r(   r\   r]   r/   r+   r   longest_row_numberr   )r   column_indicesr_   r`   rL   js         r   delete_columnszMatrix.delete_columns  s    nd++u44IKLLL~""!.11K d;;;K^^%% / /$ / /A4113333Q LOA./ .dl;;DLLL #"r&   c                    t          |t                    r#|                     |d         |d         |          S t          |t                    r.t	          j        |          \  }}|                     |||          S t          )z"Override the operator to set itemsr   r   )r   tupler7   strr;   excel_cell_positionr3   )r   asetr7   r   r   s        r   __setitem__zMatrix.__setitem__  st    dE"" 	??47DGZ@@@c"" 	3D99KC??3
;;;r&   c                    t          |t                    r"|                     |d         |d                   S t          |t                    r-t	          j        |          \  }}|                     ||          S t          |t                    r.t          t          j	                   | 
                    |          S t          )zNBy default, this class recognize from top to bottom
        from left to rightr   r   )r   r   r7   r   r;   r   intprintr   MESSAGE_DEPRECATED_ROW_COLUMNr?   r3   )r   r   r   r   s       r   __getitem__zMatrix.__getitem__  s     dE"" 		??47DG444c"" 	3D99KC??3///c"" 	)9:::;;t$$$r&   c                 P    |                                  D ]} ||          r dS dS )zHas something in the tableTFrW   )r   	predicater   s      r   containszMatrix.contains  s:    99;; 	 	Cy~~ ttur&   c                 z    t          | j                  | _        t          | j                  \  | _        | _        dS )zQRotate the data table by 90 degrees

        Reference :func:`transpose`
        N)rm   r   r   r   r#   s    r   rm   zMatrix.transpose  s1    
 !..%,T\%:%:"dlllr&   c                     | j         S )zGet an array outr"   r#   s    r   to_arrayzMatrix.to_array  r%   r&   c                 *    |                                  S )z
        Default iterator to go through each cell one by one from top row to
        bottom row and from left to right
        r   r#   s    r   __iter__zMatrix.__iter__  s    
 yy{{r&   c                     t          | j         S )a  
        Iterate cell by cell from top to bottom and from left to right

        .. testcode::

            >>> import pyexcel as pe
            >>> data = [
            ...     [1, 2, 3, 4],
            ...     [5, 6, 7, 8],
            ...     [9, 10, 11, 12]
            ... ]
            >>> m = pe.internal.sheets.Matrix(data)
            >>> print(list(m.enumerate()))
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

        More details see :class:`HTLBRIterator`
        )r   r   r#   s    r   r   zMatrix.enumerate  s    $ dl##r&   c              #   d   K   t          | j                  D ]}t          |          D ]}|V  dS )a  Opposite to enumerate

        each cell one by one from
        bottom row to top row and from right to left
        example::

            >>> import pyexcel as pe
            >>> data = [
            ...     [1, 2, 3, 4],
            ...     [5, 6, 7, 8],
            ...     [9, 10, 11, 12]
            ... ]
            >>> m = pe.internal.sheets.Matrix(data)
            >>> print(list(m.reverse()))
            [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

        More details see :class:`HBRTLIterator`
        Nreversedr   )r   r   cells      r   r[   zMatrix.reverse  sP      & DL)) 	 	C   



	 	r&   c                 6    t          t          j        | j          S )a  
        Default iterator to go through each cell one by one from
        leftmost column to rightmost row and from top to bottom
        example::

            import pyexcel as pe
            data = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
            ]
            m = pe.internal.sheets.Matrix(data)
            print(list(m.vertical()))

        output::

            [1, 5, 9, 2, 6, 10, 3, 7, 11, 4, 8, 12]

        More details see :class:`VTLBRIterator`
        )r   r-   czipr   r#   s    r   verticalzMatrix.vertical,  s    * glDL122r&   c              #   v   K   t          j        d | j        D              D ]}t          |          D ]}|V  dS )a  
        Default iterator to go through each cell one by one from rightmost
        column to leftmost row and from bottom to top
        example::

            import pyexcel as pe
            data = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
            ]
            m = pe.internal.sheets.Matrix(data)
            print(pe.utils.to_array(m.rvertical())

        output::

            [12, 8, 4, 11, 7, 3, 10, 6, 2, 9, 5, 1]

        More details see :class:`VBRTLIterator`
        c              3   4   K   | ]}t          |          V  d S rP   r   r   r   s     r   	<genexpr>z#Matrix.rvertical.<locals>.<genexpr>X  (      $K$KsXc]]$K$K$K$K$K$Kr&   N)r-   r   r   r   )r   r   r   s      r   	rverticalzMatrix.rverticalC  s`      * l$K$Kdl$K$K$KL 	 	F ((  



	 	r&   c              #   &   K   | j         D ]}|V  dS )a  
        Returns a top to bottom row iterator

        example::

            import pyexcel as pe
            data = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
            ]
            m = pe.internal.sheets.Matrix(data)
            print(pe.utils.to_array(m.rows()))

        output::

            [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

        More details see :class:`RowIterator`
        Nr"   r   r   s     r   rW   zMatrix.rows\  s.      * < 	 	CIIII	 	r&   c              #   @   K   t          | j                  D ]}|V  dS )a  
        Returns a bottom to top row iterator

        .. testcode::

            import pyexcel as pe
            data = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
            ]
            m = pe.internal.sheets.Matrix(data)
            print(pe.utils.to_array(m.rrows()))

        .. testoutput::

            [[9, 10, 11, 12], [5, 6, 7, 8], [1, 2, 3, 4]]

        More details see :class:`RowReverseIterator`
        Nr   r   s     r   rrowszMatrix.rrowst  s6      * DL)) 	 	CIIII	 	r&   c              #   X   K   t          j        | j         D ]}t          |          V  dS )a  
        Returns a left to right column iterator

        .. testcode::

            import pyexcel as pe
            data = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
            ]
            m = pe.internal.sheets.Matrix(data)
            print(list(m.columns()))

        .. testoutput::

            [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

        More details see :class:`ColumnIterator`
        Nr-   r   r   r   r   s     r   ro   zMatrix.columns  s;      * <. 	 	Cs))OOOO	 	r&   c              #   l   K   t          j        d | j        D              D ]}t          |          V  dS )a  
        Returns a right to left column iterator

        example::

            import pyexcel as pe
            data = [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]
            ]
            m = pe.internal.sheets.Matrix(data)
            print(pe.utils.to_array(m.rcolumns()))

        output::

            [[4, 8, 12], [3, 7, 11], [2, 6, 10], [1, 5, 9]]

        More details see :class:`ColumnReverseIterator`
        c              3   4   K   | ]}t          |          V  d S rP   r   r   s     r   r   z"Matrix.rcolumns.<locals>.<genexpr>  r   r&   Nr   )r   r   s     r   rcolumnszMatrix.rcolumns  sN      * l$K$Kdl$K$K$KL 	 	Fv,,	 	r&   c                 f    ||                      |           ||                     |           dS dS )z&Apply the filter with immediate effectN)ra   r   )r   r   r^   s      r   filterzMatrix.filter  sD    "[)))%///// &%r&   c                 Z    t          t          |          }|                     |           dS )a  Apply a formatting action for the whole sheet

        Example::

            >>> import pyexcel as pe
            >>> # Given a dictinoary as the following
            >>> data = {
            ...     "1": [1, 2, 3, 4, 5, 6, 7, 8],
            ...     "3": [1.25, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8],
            ...     "5": [2, 3, 4, 5, 6, 7, 8, 9],
            ...     "7": [1, '',]
            ...     }
            >>> sheet = pe.get_sheet(adict=data)
            >>> sheet.row[1]
            [1, 1.25, 2, 1]
            >>> sheet.format(str)
            >>> sheet.row[1]
            ['1', '1.25', '2', '1']
            >>> sheet.format(int)
            >>> sheet.row[1]
            [1, 1, 2, 1]

        N)r   r
   map)r   	formattercustom_functions      r   formatzMatrix.format  s+    0 ")Y77!!!!!r&   c                     |                                  D ]Q}|                                 D ]:}|                     ||          } ||          }|                     |||           ;RdS )a  Execute a function across all cells of the sheet

        Example::

            >>> import pyexcel as pe
            >>> # Given a dictinoary as the following
            >>> data = {
            ...     "1": [1, 2, 3, 4, 5, 6, 7, 8],
            ...     "3": [1.25, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8],
            ...     "5": [2, 3, 4, 5, 6, 7, 8, 9],
            ...     "7": [1, '',]
            ...     }
            >>> sheet = pe.get_sheet(adict=data)
            >>> sheet.row[1]
            [1, 1.25, 2, 1]
            >>> inc = lambda value: (float(value) if value != '' else 0)+1
            >>> sheet.map(inc)
            >>> sheet.row[1]
            [2.0, 2.25, 3.0, 2.0]

        N)r/   r1   r7   )r   r   r   r   values        r   r   z
Matrix.map  s    , >>## 	4 	4C++-- 4 4V44'..VU33334	4 	4r&   c                 8    t          | j        | j        |          S rP   )_addr   r   r   others     r   __iadd__zMatrix.__iadd__  s    DIt|U333r&   c                 \    t          | j        t          j        | j                  |          S )z:Overload the + sign

        :returns: a new book
        )r   r   r9   r:   r   r   s     r   __add__zMatrix.__add__  s$    
 DIt}T\::EBBBr&   c                 N    t          t          j        | j                            S rP   )r   r9   r:   r   r#   s    r   clonezMatrix.clone  s    dmDL11222r&   rP   )r   )NN)0__name__
__module____qualname____doc__r    r$   r)   r+   r/   r1   r7   r?   rD   rN   rS   rX   ra   re   rj   rq   rn   r|   r   r   r   r   r   r   r   r   r   rm   r   r   r   r[   r   r   rW   r   ro   r   r   r   r   r   r   r   r   r&   r   r   r      s          &  ! ! !  5 5 58 8 82 2 2 2(C C CC C C C  C  C  CD' ' ': : :	( 	( 	(C C C&!C !C !C !CF6 6 6*; ; ;"$- $- $-L  "  DI DI DI DIL; ; ;,; ; ;< < <      ; ; ;    $ $ $(  .3 3 3.  2  0  0  0  00 0 0 0" " "64 4 484 4 4C C C3 3 3 3 3r&   r   c                 N    t                      j        fd| D             S )zReturn a unique list of the incoming list

    Reference:
    http://stackoverflow.com/questions/480214/
    how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order
    c                 2    g | ]}|v  |          |S r   r   )r   xseenseen_adds     r   r   z_unique.<locals>.<listcomp>  s*    ===!1999A999r&   )setadd)seqr   r   s    @@r   r\   r\     s3     55DxH=====s====r&   c                 p    t          |           dk    r"t          t          t           |                     S dS )z`Find the length of the longest row in the array

    :param list in_array: a list of arrays
    r   )r(   r   r   )r   s    r   r   r     s.    
 5zzA~~3sE??###qr&   c                    t          |t          |                     }t          |           }t          ||          }|dk    rd| fS | D ]W}t          |          }t          d|          D ]}||         t          j        ||<   ||k     r|t          j        g||z
  z  z  }Xt          ||          D ]'}	t          j        g|z  }|                     |           (|| fS )zFill-in empty strings to empty cells to make it MxN

    :param list in_array: a list of arrays
    :param int row_no: desired minimum row count
    :param int column_no: desired minimum column length
    r   )r   r   r(   rG   r   rs   rR   )
r   min_rowsmin_columnsr6   rw   heightr   
row_lengthr>   r   s
             r   r   r      s    /6677Eu::Lx((Fzz%x 	E 	ECSJq*-- 6 6u:%!*!5CJE!!	,-1CDD|V,, 	 	A'(50CLLe|r&   c                 "   t          |           }g }t          d|          D ]l}g }| D ]P}|t          |          k     r|                    ||                    1|                    t          j                   Q|                    |           m|S )a
  Rotate clockwise by 90 degrees and flip horizontally

    First column become first row.
    :param list in_array: a list of arrays

    The transformation is::

        1 2 3       1  4
        4 5 6 7 ->  2  5
                    3  6
                    '' 7
    r   )r   rG   r(   rR   r   rs   )in_array
max_lengthrz   rL   row_datar   s         r   rm   rm   ;  s     $H--JI1j!! # # 	6 	6C3s88||A''''	 45555""""r&   c                 h   ddl m}m} i }||| <   t          ||          rt	          j        |                                          }|                                D ]P}|}t          |                                          dk    r|j	        }||v r |            }	|d|	}||         ||<   Qngt          |t                    rK|j        }||v r |            }	|j        d|	}t	          j        |                                          ||<   nt           |            }
|
                    |           |
S )Nr   )Book
local_uuidr   r   )pyexcel.bookr   r   r   r9   r:   to_dictkeysr(   filenamer   r   r$   r   load_from_sheets)r   rM   rightr   r   contentright_in_dictkeynew_keyuidnew_books              r   r   r   U  sc   --------GGDM% emmoo66 %%'' 	2 	2CG=%%''((A--.'!! jll%(SS##.,S1GG	2 
E6	"	" *g*,,C!&SS1G=)A)A)C)CDDtvvHg&&&Or&   )r   r   )r   r9   r   	functoolsr   	itertoolsr   pyexcelr   r-   r   pyexcel.internal.metar   pyexcel.internal.sheets.rowr   pyexcel.internal.sheets.columnr	   "pyexcel.internal.sheets.formattersr
   %pyexcel.internal.sheets.extended_listr   rg   r   r;   r   r\   r   r   rm   r   r   r&   r   <module>r     sd  	 	               ' ' ' ' ' ' * * * * * * + + + + + + + + + + + + 1 1 1 1 1 1 8 8 8 8 8 8 = = = = = =      j3 j3 j3 j3 j3Y j3 j3 j3Z	> 	> 	>	 	 	   6  4    r&   