§
    Ҭ¸j¼V  ã                  ó˜   — 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 erd dlmZmZ d dlmZmZmZmZmZ  G d	„ d
¦  «        ZdS )é    )Úannotations)ÚTYPE_CHECKING)Úparse_into_expression)Úscale_bytes)Ú	wrap_expr)Úparse_into_datatype_expr)ÚDataTypeExprÚExpr)Ú
EndiannessÚIntoExprÚPolarsDataTypeÚSizeUnitÚTransferEncodingc                  ó’   — e Zd ZdZdZd1d„Zd2d
„Zd3d„Zd4d„Zddœd5d„Z	d6d„Z
d7d8d„Zddœd9d!„Z	 d:d;d'„Zd<d=d*„Zd<d=d+„Zd,d-œd>d0„Zd"S )?ÚExprBinaryNameSpacez&Namespace for bin related expressions.ÚbinÚexprr
   ÚreturnÚNonec                ó   — |j         | _         d S ©N)Ú_pyexpr)Úselfr   s     úb/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/polars/expr/binary.pyÚ__init__zExprBinaryNameSpace.__init__   s   € Ø”|ˆŒˆˆó    Úliteralr   c                ór   — t          |d¬¦  «        }t          | j                             |¦  «        ¦  «        S )u¿  
        Check if binaries in Series contain a binary substring.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        literal
            The binary substring to look for

        Returns
        -------
        Expr
            Expression of data type :class:`Boolean`.

        See Also
        --------
        starts_with : Check if the binary substring exists at the start
        ends_with : Check if the binary substring exists at the end

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...         "lit": [b"\x00", b"\xff\x00", b"\xff\xff"],
        ...     }
        ... )
        >>> colors.select(
        ...     "name",
        ...     pl.col("code").bin.contains(b"\xff").alias("contains_with_lit"),
        ...     pl.col("code").bin.contains(pl.col("lit")).alias("contains_with_expr"),
        ... )
        shape: (3, 3)
        ┌────────┬───────────────────┬────────────────────┐
        │ name   ┆ contains_with_lit ┆ contains_with_expr │
        │ ---    ┆ ---               ┆ ---                │
        │ str    ┆ bool              ┆ bool               │
        ╞════════╪═══════════════════╪════════════════════╡
        │ black  ┆ false             ┆ true               │
        │ yellow ┆ true              ┆ true               │
        │ blue   ┆ true              ┆ false              │
        └────────┴───────────────────┴────────────────────┘
        T©Ú
str_as_lit)r   r   r   Úbin_contains)r   r   Úliteral_pyexprs      r   ÚcontainszExprBinaryNameSpace.contains   s5   € õ\ /¨wÀ4ÐHÑHÔHˆÝ˜œ×2Ò2°>ÑBÔBÑCÔCÐCr   Úsuffixc                ór   — t          |d¬¦  «        }t          | j                             |¦  «        ¦  «        S )u0  
        Check if string values end with a binary substring.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        suffix
            Suffix substring.

        Returns
        -------
        Expr
            Expression of data type :class:`Boolean`.

        See Also
        --------
        starts_with : Check if the binary substring exists at the start
        contains : Check if the binary substring exists anywhere

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...         "suffix": [b"\x00", b"\xff\x00", b"\x00\x00"],
        ...     }
        ... )
        >>> colors.select(
        ...     "name",
        ...     pl.col("code").bin.ends_with(b"\xff").alias("ends_with_lit"),
        ...     pl.col("code").bin.ends_with(pl.col("suffix")).alias("ends_with_expr"),
        ... )
        shape: (3, 3)
        ┌────────┬───────────────┬────────────────┐
        │ name   ┆ ends_with_lit ┆ ends_with_expr │
        │ ---    ┆ ---           ┆ ---            │
        │ str    ┆ bool          ┆ bool           │
        ╞════════╪═══════════════╪════════════════╡
        │ black  ┆ false         ┆ true           │
        │ yellow ┆ false         ┆ true           │
        │ blue   ┆ true          ┆ false          │
        └────────┴───────────────┴────────────────┘
        Tr   )r   r   r   Úbin_ends_with)r   r$   Úsuffix_pyexprs      r   Ú	ends_withzExprBinaryNameSpace.ends_withN   s5   € õ\ .¨fÀÐFÑFÔFˆÝ˜œ×3Ò3°MÑBÔBÑCÔCÐCr   Úprefixc                ór   — t          |d¬¦  «        }t          | j                             |¦  «        ¦  «        S )u�  
        Check if values start with a binary substring.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        prefix
            Prefix substring.

        Returns
        -------
        Expr
            Expression of data type :class:`Boolean`.

        See Also
        --------
        ends_with : Check if the binary substring exists at the end
        contains : Check if the binary substring exists anywhere

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...         "prefix": [b"\x00", b"\xff\x00", b"\x00\x00"],
        ...     }
        ... )
        >>> colors.select(
        ...     "name",
        ...     pl.col("code").bin.starts_with(b"\xff").alias("starts_with_lit"),
        ...     pl.col("code")
        ...     .bin.starts_with(pl.col("prefix"))
        ...     .alias("starts_with_expr"),
        ... )
        shape: (3, 3)
        ┌────────┬─────────────────┬──────────────────┐
        │ name   ┆ starts_with_lit ┆ starts_with_expr │
        │ ---    ┆ ---             ┆ ---              │
        │ str    ┆ bool            ┆ bool             │
        ╞════════╪═════════════════╪══════════════════╡
        │ black  ┆ false           ┆ true             │
        │ yellow ┆ true            ┆ false            │
        │ blue   ┆ false           ┆ true             │
        └────────┴─────────────────┴──────────────────┘
        Tr   )r   r   r   Úbin_starts_with)r   r)   Úprefix_pyexprs      r   Ústarts_withzExprBinaryNameSpace.starts_with   s5   € õ` .¨fÀÐFÑFÔFˆÝ˜œ×5Ò5°mÑDÔDÑEÔEÐEr   T)ÚstrictÚencodingr   r.   Úboolc               óÞ   — |dk    r't          | j                             |¦  «        ¦  «        S |dk    r't          | j                             |¦  «        ¦  «        S d|›�}t	          |¦  «        ‚)u&  
        Decode values using the provided encoding.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        encoding : {'hex', 'base64'}
            The encoding to use.
        strict
            Raise an error if the underlying value cannot be decoded,
            otherwise mask out with a null value.

        Returns
        -------
        Expr
            Expression of data type :class:`Binary`.

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "encoded": [b"000000", b"ffff00", b"0000ff"],
        ...     }
        ... )
        >>> colors.with_columns(
        ...     pl.col("encoded").bin.decode("hex").alias("code"),
        ... )
        shape: (3, 3)
        ┌────────┬───────────┬─────────────────┐
        │ name   ┆ encoded   ┆ code            │
        │ ---    ┆ ---       ┆ ---             │
        │ str    ┆ binary    ┆ binary          │
        ╞════════╪═══════════╪═════════════════╡
        │ black  ┆ b"000000" ┆ b"\x00\x00\x00" │
        │ yellow ┆ b"ffff00" ┆ b"\xff\xff\x00" │
        │ blue   ┆ b"0000ff" ┆ b"\x00\x00\xff" │
        └────────┴───────────┴─────────────────┘
        ÚhexÚbase64ú1`encoding` must be one of {'hex', 'base64'}, got )r   r   Úbin_hex_decodeÚbin_base64_decodeÚ
ValueError)r   r/   r.   Úmsgs       r   ÚdecodezExprBinaryNameSpace.decode²   sp   € ðR �uÒÐÝ˜Tœ\×8Ò8¸Ñ@Ô@ÑAÔAÐAØ˜Ò!Ð!Ý˜Tœ\×;Ò;¸FÑCÔCÑDÔDÐDàTÈÐTÐTˆCÝ˜S‘/”/Ð!r   c                óÚ   — |dk    r&t          | j                             ¦   «         ¦  «        S |dk    r&t          | j                             ¦   «         ¦  «        S d|›�}t	          |¦  «        ‚)u’  
        Encode a value using the provided encoding.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        encoding : {'hex', 'base64'}
            The encoding to use.

        Returns
        -------
        Expr
            Expression of data type :class:`Binary`.

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "color": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...     }
        ... )
        >>> colors.with_columns(
        ...     pl.col("code").bin.encode("hex").alias("encoded"),
        ... )
        shape: (3, 3)
        ┌────────┬─────────────────┬─────────┐
        │ color  ┆ code            ┆ encoded │
        │ ---    ┆ ---             ┆ ---     │
        │ str    ┆ binary          ┆ str     │
        ╞════════╪═════════════════╪═════════╡
        │ black  ┆ b"\x00\x00\x00" ┆ 000000  │
        │ yellow ┆ b"\xff\xff\x00" ┆ ffff00  │
        │ blue   ┆ b"\x00\x00\xff" ┆ 0000ff  │
        └────────┴─────────────────┴─────────┘
        r2   r3   r4   )r   r   Úbin_hex_encodeÚbin_base64_encoder7   )r   r/   r8   s      r   ÚencodezExprBinaryNameSpace.encodeã   sl   € ðL �uÒÐÝ˜Tœ\×8Ò8Ñ:Ô:Ñ;Ô;Ð;Ø˜Ò!Ð!Ý˜Tœ\×;Ò;Ñ=Ô=Ñ>Ô>Ð>àTÈÐTÐTˆCÝ˜S‘/”/Ð!r   ÚbÚunitr   c                ór   — t          | j                             ¦   «         ¦  «        }t          ||¦  «        }|S )uØ  
        Get the size of binary values in the given unit.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        unit : {'b', 'kb', 'mb', 'gb', 'tb'}
            Scale the returned size to the given unit.

        Returns
        -------
        Expr
            Expression of data type :class:`UInt32` or `Float64`.

        Examples
        --------
        >>> from os import urandom
        >>> df = pl.DataFrame({"data": [urandom(n) for n in (512, 256, 1024)]})
        >>> df.with_columns(  # doctest: +IGNORE_RESULT
        ...     n_bytes=pl.col("data").bin.size(),
        ...     n_kilobytes=pl.col("data").bin.size("kb"),
        ... )
        shape: (4, 3)
        ┌─────────────────────────────────┬─────────┬─────────────┐
        │ data                            ┆ n_bytes ┆ n_kilobytes │
        │ ---                             ┆ ---     ┆ ---         │
        │ binary                          ┆ u32     ┆ f64         │
        ╞═════════════════════════════════╪═════════╪═════════════╡
        │ b"y?~B\x83\xf4V\x07\xd3\xfb\xb… ┆ 512     ┆ 0.5         │
        │ b"\xee$4@f\xc14\x07\x8e\x88\x1… ┆ 256     ┆ 0.25        │
        │ b"\x80\xbd\xb9nEq;2\x99$\xf9\x… ┆ 1024    ┆ 1.0         │
        └─────────────────────────────────┴─────────┴─────────────┘
        )r   r   Úbin_size_bytesr   )r   r?   Úszs      r   ÚsizezExprBinaryNameSpace.size  s4   € õF �t”|×2Ò2Ñ4Ô4Ñ5Ô5ˆÝ˜˜TÑ"Ô"ˆØˆ	r   Úlittle)Ú
endiannessÚdtypeúPolarsDataType | DataTypeExprrE   r   c               óz   — t          |¦  «        }t          | j                             |j        |¦  «        ¦  «        S )u€  
        Interpret bytes as another type.

        Supported types are numerical or temporal dtypes, or an ``Array`` of
        these dtypes.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        dtype : PolarsDataType
            Which type to interpret binary column into.
        endianness : {"big", "little"}, optional
            Which endianness to use when interpreting bytes, by default "little".

        Returns
        -------
        Expr
            Expression of data type `dtype`.
            Note that rows of the binary array where the length does not match
            the size in bytes of the output array (number of items * byte size
            of item) will become NULL.

        Examples
        --------
        >>> df = pl.DataFrame({"data": [b"\x05\x00\x00\x00", b"\x10\x00\x01\x00"]})
        >>> df.with_columns(  # doctest: +IGNORE_RESULT
        ...     bin2int=pl.col("data").bin.reinterpret(
        ...         dtype=pl.Int32, endianness="little"
        ...     ),
        ... )
        shape: (2, 2)
        ┌─────────────────────┬─────────┐
        │ data                ┆ bin2int │
        │ ---                 ┆ ---     │
        │ binary              ┆ i32     │
        ╞═════════════════════╪═════════╡
        │ b"\x05\x00\x00\x00" ┆ 5       │
        │ b"\x10\x00\x01\x00" ┆ 65552   │
        └─────────────────────┴─────────┘
        )r   r   r   Úbin_reinterpretÚ_pydatatype_expr)r   rF   rE   s      r   ÚreinterpretzExprBinaryNameSpace.reinterpret8  s<   € õX )¨Ñ/Ô/ˆåØŒL×(Ò(¨Ô)?ÀÑLÔLñ
ô 
ð 	
r   NÚoffsetúint | IntoExprÚlengthúint | IntoExpr | Nonec                óŽ   — t          |¦  «        }t          |¦  «        }t          | j                             ||¦  «        ¦  «        S )u?  
        Slice the binary values.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        offset
            Start index. Negative indexing is supported.
        length
            Length of the slice. If set to ``None`` (default), the slice is taken to the
            end of the value.

        Returns
        -------
        Expr
            Expression of data type :class:`Binary`.

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...     }
        ... )
        >>> colors.with_columns(
        ...     pl.col("code").bin.slice(1, 2).alias("sliced"),
        ... )
        shape: (3, 3)
        ┌────────┬─────────────────┬─────────────┐
        │ name   ┆ code            ┆ sliced      │
        │ ---    ┆ ---             ┆ ---         │
        │ str    ┆ binary          ┆ binary      │
        ╞════════╪═════════════════╪═════════════╡
        │ black  ┆ b"\x00\x00\x00" ┆ b"\x00\x00" │
        │ yellow ┆ b"\xff\xff\x00" ┆ b"\xff\x00" │
        │ blue   ┆ b"\x00\x00\xff" ┆ b"\x00\xff" │
        └────────┴─────────────────┴─────────────┘
        )r   r   r   Ú	bin_slice)r   rL   rN   Úoffset_pyexprÚlength_pyexprs        r   ÚslicezExprBinaryNameSpace.slicej  s?   € õV .¨fÑ5Ô5ˆÝ-¨fÑ5Ô5ˆÝ˜œ×/Ò/°¸}ÑMÔMÑNÔNÐNr   é   Únc                ór   — t          |d¬¦  «        }t          | j                             |¦  «        ¦  «        S )u  
        Take the first `n` bytes of the binary values.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        n
            Length of the slice (integer or expression). Negative indexing is supported;
            see note (2) below.

        Returns
        -------
        Expr
            Expression of data type :class:`Binary`.

        Notes
        -----
        (1) A similar method exists for taking the last `n` bytes: :func:`tail`.
        (2) If `n` is negative, it is interpreted as "until the nth byte from the end",
            e.g., ``head(-3)`` returns all but the last three bytes.

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...     }
        ... )
        >>> colors.with_columns(
        ...     pl.col("code").bin.head(2).alias("head"),
        ... )
        shape: (3, 3)
        ┌────────┬─────────────────┬─────────────┐
        │ name   ┆ code            ┆ head        │
        │ ---    ┆ ---             ┆ ---         │
        │ str    ┆ binary          ┆ binary      │
        ╞════════╪═════════════════╪═════════════╡
        │ black  ┆ b"\x00\x00\x00" ┆ b"\x00\x00" │
        │ yellow ┆ b"\xff\xff\x00" ┆ b"\xff\xff" │
        │ blue   ┆ b"\x00\x00\xff" ┆ b"\x00\x00" │
        └────────┴─────────────────┴─────────────┘
        Fr   )r   r   r   Úbin_head©r   rV   Ún_pyexprs      r   ÚheadzExprBinaryNameSpace.head™  ó5   € õZ )¨°uÐ=Ñ=Ô=ˆÝ˜œ×.Ò.¨xÑ8Ô8Ñ9Ô9Ð9r   c                ór   — t          |d¬¦  «        }t          | j                             |¦  «        ¦  «        S )u	  
        Take the last `n` bytes of the binary values.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        n
            Length of the slice (integer or expression). Negative indexing is supported;
            see note (2) below.

        Returns
        -------
        Expr
            Expression of data type :class:`Binary`.

        Notes
        -----
        (1) A similar method exists for taking the first `n` bytes: :func:`head`.
        (2) If `n` is negative, it is interpreted as "starting at the nth byte",
            e.g., ``tail(-3)`` returns all but the first three bytes.

        Examples
        --------
        >>> colors = pl.DataFrame(
        ...     {
        ...         "name": ["black", "yellow", "blue"],
        ...         "code": [b"\x00\x00\x00", b"\xff\xff\x00", b"\x00\x00\xff"],
        ...     }
        ... )
        >>> colors.with_columns(
        ...     pl.col("code").bin.tail(2).alias("tail"),
        ... )
        shape: (3, 3)
        ┌────────┬─────────────────┬─────────────┐
        │ name   ┆ code            ┆ tail        │
        │ ---    ┆ ---             ┆ ---         │
        │ str    ┆ binary          ┆ binary      │
        ╞════════╪═════════════════╪═════════════╡
        │ black  ┆ b"\x00\x00\x00" ┆ b"\x00\x00" │
        │ yellow ┆ b"\xff\xff\x00" ┆ b"\xff\x00" │
        │ blue   ┆ b"\x00\x00\xff" ┆ b"\x00\xff" │
        └────────┴─────────────────┴─────────────┘
        Fr   )r   r   r   Úbin_tailrY   s      r   ÚtailzExprBinaryNameSpace.tailÉ  r\   r   F)Únull_on_oobÚindexr`   c               óp   — t          |¦  «        }t          | j                             ||¦  «        ¦  «        S )u3  
        Get the byte value at the given index.

        For example, index `0` would return the first byte of every binary value
        and index `-1` would return the last byte of every binary value.
        The behavior if an index is out of bounds is determined by the argument
        `null_on_oob`.

        .. engine-support:: in-memory, streaming, distributed

        Parameters
        ----------
        index
            Index to return per binary value
        null_on_oob
            Behavior if an index is out of bounds:

            * True -> set as null
            * False -> raise an error

        Examples
        --------
        >>> df = pl.DataFrame({"a": [b"\x01\x02\x03", b"", b"\x04\x05"]})
        >>> df.with_columns(get=pl.col("a").bin.get(0, null_on_oob=True))
        shape: (3, 2)
        ┌─────────────────┬──────┐
        │ a               ┆ get  │
        │ ---             ┆ ---  │
        │ binary          ┆ u8   │
        ╞═════════════════╪══════╡
        │ b"\x01\x02\x03" ┆ 1    │
        │ b""             ┆ null │
        │ b"\x04\x05"     ┆ 4    │
        └─────────────────┴──────┘
        )r   r   r   Úbin_get)r   ra   r`   Úindex_pyexprs       r   ÚgetzExprBinaryNameSpace.getù  s2   € õH -¨UÑ3Ô3ˆÝ˜œ×-Ò-¨l¸KÑHÔHÑIÔIÐIr   )r   r
   r   r   )r   r   r   r
   )r$   r   r   r
   )r)   r   r   r
   )r/   r   r.   r0   r   r
   )r/   r   r   r
   )r>   )r?   r   r   r
   )rF   rG   rE   r   r   r
   r   )rL   rM   rN   rO   r   r
   )rU   )rV   rM   r   r
   )ra   rM   r`   r0   r   r
   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	_accessorr   r#   r(   r-   r9   r=   rC   rK   rT   r[   r_   re   © r   r   r   r      s€  € € € € € Ø0Ð0à€Ið$ð $ð $ð $ð/Dð /Dð /Dð /Dðb/Dð /Dð /Dð /Dðb1Fð 1Fð 1Fð 1Fðf DHð /"ð /"ð /"ð /"ð /"ð /"ðb,"ð ,"ð ,"ð ,"ð\%ð %ð %ð %ð %ðP QYð0
ð 0
ð 0
ð 0
ð 0
ð 0
ðf GKð-Oð -Oð -Oð -Oð -Oð^.:ð .:ð .:ð .:ð .:ð`.:ð .:ð .:ð .:ð .:ð` AFð %Jð %Jð %Jð %Jð %Jð %Jð %Jð %Jr   r   N)Ú
__future__r   Útypingr   Úpolars._utils.parser   Úpolars._utils.variousr   Úpolars._utils.wrapr   Úpolars.datatypesr   Úpolarsr	   r
   Úpolars._typingr   r   r   r   r   r   rk   r   r   ú<module>rt      s  ðØ "Ð "Ð "Ð "Ð "Ð "à  Ð  Ð  Ð  Ð  Ð  à 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø -Ð -Ð -Ð -Ð -Ð -Ø (Ð (Ð (Ð (Ð (Ð (Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5àð Ø)Ð)Ð)Ð)Ð)Ð)Ð)Ð)ðð ð ð ð ð ð ð ð ð ð ð ð ð ðIJð IJð IJð IJð IJñ IJô IJð IJð IJð IJr   