Skip to content

transforms

Pad

Type transformation to pad a value to length with padding. Ensures that value will be at least length long. padding should be the same type as value, so the concatenation functions properly

Type Constraints

  • Support len()
  • Support + for concatenation (like str or list)

Example

import arc
from arc.transforms import Pad

@arc.command()
def command(val: Annotated[str, Pad(6, 'b')])
    arc.print(val)

command()
$ python example.py a
abbbbbb
Source code in arc/types/transforms/size.py
class Pad:
    """Type transformation to pad a value to `length` with `padding`.
    Ensures that value will be at least `length` long. `padding`
    should be the same type as value, so the concatenation functions properly

    ## Type Constraints
    - Support `len()`
    - Support `+` for concatenation (like `str` or `list`)

    ## Example
    ```py
    import arc
    from arc.transforms import Pad

    @arc.command()
    def command(val: Annotated[str, Pad(6, 'b')])
        arc.print(val)

    command()
    ```

    ```console
    $ python example.py a
    abbbbbb
    ```
    """

    def __init__(self, length: int, padding: t.Any = None):
        self.length = length
        self.padding = padding

    def __call__(self, value: PadProtocol):
        while len(value) < self.length:
            value += self.padding

        return value

Truncate

Type transformation to truncate a value to length

Type Constraints

  • Support list-like slice access

Example

import arc
from arc.transforms import Truncate

@arc.command()
def command(val: Annotated[str, Truncate(6)])
    arc.print(val)

command()
$ python example.py 'string longer than 6 chars'
string
Source code in arc/types/transforms/size.py
class Truncate:
    """Type transformation to truncate a value to `length`

    ## Type Constraints
    - Support list-like slice access

    ## Example
    ```py
    import arc
    from arc.transforms import Truncate

    @arc.command()
    def command(val: Annotated[str, Truncate(6)])
        arc.print(val)

    command()
    ```

    ```console
    $ python example.py 'string longer than 6 chars'
    string
    ```
    """

    def __init__(self, length: int):
        self.length = length

    def __call__(self, value: SupportsSlice) -> SupportsSlice:
        return value[0 : self.length]

Round

Type Tranformation to round given input to ndigits

Type Contraints

  • Supports round()

Example

import arc
from arc.transforms import Round

@arc.command()
def command(val: Annotated[float, Round(2)])
    arc.print(val)

command()
$ python example.py 1.123456789
1.23
Source code in arc/types/transforms/numbers.py
class Round:
    """Type Tranformation to round given input to `ndigits`

    ## Type Contraints
    - Supports `round()`

    ## Example
    ```py
    import arc
    from arc.transforms import Round

    @arc.command()
    def command(val: Annotated[float, Round(2)])
        arc.print(val)

    command()
    ```

    ```console
    $ python example.py 1.123456789
    1.23
    ```
    """

    __slots__ = ("ndigits",)

    def __init__(self, ndigits: int) -> None:
        self.ndigits = ndigits

    def __call__(self, value: t.SupportsRound) -> t.SupportsRound:
        return round(value, self.ndigits)