jacinle.utils.argument#

Classes

UniqueValueGetter

A helper class to ensure that a value is unique.

Functions

asshape(arr_like)

Convert a sequence or a single value to a tuple of integers.

astuple(arr_like)

Convert a sequence or a single value to a tuple.

canonize_args_list(args, *[, allow_empty, cvt])

Convert the argument list to a tuple of values.

get_2dshape(x[, default, type])

Convert a value or a tuple to a tuple of length 2.

get_3dshape(x[, default, type])

Convert a value or a tuple to a tuple of length 3.

get_4dshape(x[, default, type])

Convert a value or a tuple to a tuple of length 4.

get_nd_shape(x, ndim[, default, type])

Convert a value or a tuple to a tuple of length ndim.

Class UniqueValueGetter

class UniqueValueGetter[source]#

Bases: object

A helper class to ensure that a value is unique.

Example

uvg = UniqueValueGetter()
uvg.set(1)
uvg.set(2)  # will raise ValueError
uvg.set(1)

print(uvg.get())  # 1
__init__(msg='Unique value checking failed', default=None)[source]#

Initialize the UniqueValueGetter.

Parameters:
  • msg (str) – the error message.

  • default (Any) – the default value.

__new__(**kwargs)#
get()[source]#
set(v)[source]#

Functions

asshape(arr_like)[source]#

Convert a sequence or a single value to a tuple of integers. It will return None if the input is None.

Parameters:

arr_like (int | Sequence[int] | None) – a sequence or a single value.

Returns:

a tuple of integers.

Return type:

Tuple[int, …] | None

astuple(arr_like)[source]#

Convert a sequence or a single value to a tuple. This method differ from the system method tuple in that a single value (incl. int, string, bytes) will be converted to a tuple of length 1.

Parameters:

arr_like (Any) – a sequence or a single value.

Returns:

a tuple.

Return type:

Tuple

canonize_args_list(args, *, allow_empty=False, cvt=None)[source]#

Convert the argument list to a tuple of values. This is useful to make unified interface for shape-related operations.

Example

def foo(*args):
    args = canonize_args_list(args, allow_empty=True)
    print(args)

foo(1, 2, 3)  # (1, 2, 3)
foo((1, 2, 3))  # (1, 2, 3)
foo(1)  # (1,)
foo()  # ()
Parameters:
  • args (Tuple[Any]) – the argument list.

  • allow_empty (bool) – whether to allow empty argument list.

  • cvt (Callable[[Any], Any] | None) – a function to be applied to each element.

Return type:

Tuple[Any]

get_2dshape(x, default=None, type=int)[source]#

Convert a value or a tuple to a tuple of length 2.

Parameters:
  • x (int | Sequence[int] | None) – a value of type type, or a tuple of length 2. If the input is a single value, it will be duplicated to a tuple of length 2.

  • default (Tuple[int, int]) – default value.

  • type (type) – expected type of the element.

Returns:

a tuple of length 2.

Return type:

Tuple[int, int]

get_3dshape(x, default=None, type=int)[source]#

Convert a value or a tuple to a tuple of length 3.

Parameters:
  • x (int | Sequence[int] | None) – a value of type type, or a tuple of length 3. If the input is a single value, it will be duplicated to a tuple of length 3.

  • default (Tuple[int, int, int]) – default value.

  • type (type) – expected type of the element.

Returns:

a tuple of length 3.

Return type:

Tuple[int, int, int]

get_4dshape(x, default=None, type=int)[source]#

Convert a value or a tuple to a tuple of length 4.

Parameters:
  • x (int | Sequence[int] | None) – a value of type type, or a tuple of length 4. If there is only one value, it will return (1, x, x, 1). If there are two values, it will return (1, x[0], x[1], 1).

  • default (Tuple[int, int, int, int]) – default value.

  • type (type) – expected type of the element.

Returns:

a tuple of length 4.

Return type:

Tuple[int, int, int, int]

get_nd_shape(x, ndim, default=None, type=int)[source]#

Convert a value or a tuple to a tuple of length ndim.

Parameters:
  • x (int | Sequence[int] | None) – a value of type type, or a tuple of length ndim. If the input is a single value, it will be duplicated to a tuple of length ndim.

  • ndim (int) – the expected length of the tuple.

  • default (Tuple[int, ...]) – default value.

  • type (type) – expected type of the element.

Returns:

a tuple of length ndim.

Return type:

Tuple[int, …]