dev4py.utils.tuples
1# Copyright 2022 the original author or authors (i.e.: St4rG00se for Dev4py). 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from typing import Any, cast 16 17from dev4py.utils.objects import require_non_none 18from dev4py.utils.types import T, Ts, U 19 20 21def empty_tuple() -> tuple[()]: 22 """ 23 Returns an empty tuple 24 25 Returns: 26 tuple[Any, ...]: An empty tuple 27 """ 28 return () 29 30 31def append(tpl: tuple[*Ts], value: T) -> tuple[*Ts, T]: 32 """ 33 Adds the given value to the given tuple and returns the new tuple with added value 34 35 Args: 36 tpl: the tuple 37 value: the value to add 38 39 Returns: 40 tuple[T, ...]: A new tuple with all elements from tpl and the added value 41 42 Raises: 43 TypeError: if the tuple is None 44 """ 45 return cast(tuple[*Ts, T], extend(tpl, (value,))) 46 47 48def extend(tpl_1: tuple[T, ...], tpl_2: tuple[U, ...]) -> tuple[T | U, ...]: 49 """ 50 Adds all values from the second tuple to the first one and returns the new created tuple 51 52 Args: 53 tpl_1: The tuple where to add elements 54 tpl_2: The tuple with the elements to add 55 56 Returns: 57 tuple[T, ...]: A new tuple with all elements from tpl_1 and tpl_2 58 59 Raises: 60 TypeError: if tpl_1 or tpl_2 is None 61 """ 62 return require_non_none(tpl_1) + require_non_none(tpl_2)
def
empty_tuple() -> tuple[()]:
22def empty_tuple() -> tuple[()]: 23 """ 24 Returns an empty tuple 25 26 Returns: 27 tuple[Any, ...]: An empty tuple 28 """ 29 return ()
Returns an empty tuple
Returns:
tuple[Any, ...]: An empty tuple
def
append(tpl: tuple[*Ts], value: ~T) -> tuple[*Ts, ~T]:
32def append(tpl: tuple[*Ts], value: T) -> tuple[*Ts, T]: 33 """ 34 Adds the given value to the given tuple and returns the new tuple with added value 35 36 Args: 37 tpl: the tuple 38 value: the value to add 39 40 Returns: 41 tuple[T, ...]: A new tuple with all elements from tpl and the added value 42 43 Raises: 44 TypeError: if the tuple is None 45 """ 46 return cast(tuple[*Ts, T], extend(tpl, (value,)))
Adds the given value to the given tuple and returns the new tuple with added value
Arguments:
- tpl: the tuple
- value: the value to add
Returns:
tuple[T, ...]: A new tuple with all elements from tpl and the added value
Raises:
- TypeError: if the tuple is None
def
extend( tpl_1: tuple[~T, ...], tpl_2: tuple[~U, ...]) -> tuple[typing.Union[~T, ~U], ...]:
49def extend(tpl_1: tuple[T, ...], tpl_2: tuple[U, ...]) -> tuple[T | U, ...]: 50 """ 51 Adds all values from the second tuple to the first one and returns the new created tuple 52 53 Args: 54 tpl_1: The tuple where to add elements 55 tpl_2: The tuple with the elements to add 56 57 Returns: 58 tuple[T, ...]: A new tuple with all elements from tpl_1 and tpl_2 59 60 Raises: 61 TypeError: if tpl_1 or tpl_2 is None 62 """ 63 return require_non_none(tpl_1) + require_non_none(tpl_2)
Adds all values from the second tuple to the first one and returns the new created tuple
Arguments:
- tpl_1: The tuple where to add elements
- tpl_2: The tuple with the elements to add
Returns:
tuple[T, ...]: A new tuple with all elements from tpl_1 and tpl_2
Raises:
- TypeError: if tpl_1 or tpl_2 is None