Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offering: Add array support to format function #47

Open
jdhedden opened this issue May 9, 2022 · 1 comment
Open

Offering: Add array support to format function #47

jdhedden opened this issue May 9, 2022 · 1 comment

Comments

@jdhedden
Copy link

jdhedden commented May 9, 2022

The current format function only support single-valued inputs. The below permits it to take arrays as inputs:

For ft/ft/functions.py:

@register("format")
@typed(None, T_STRING)
def format(format_str, inp):
    try:
        if type(inp) == list:
            args = list(map(lambda v: v.value, inp))
            return format_str.value.format(*args)
        else:
            return format_str.value.format(inp)
    except ValueError:
        panic("Incorrect format string '{}' for input '{}'.".format(format_str.value, inp))
    except IndexError:
        if type(inp) == list:
            count = len(inp)
        else:
            count = 1
        panic("Not enough arguments (only {} given) for format string '{}'.".format(count, format_str.value))

Example:

> echo -e '1\tone' | map format "'{}' is spelled '{}'"
'1' is spelled 'one'
@sharkdp
Copy link
Owner

sharkdp commented May 22, 2022

Great idea. Note that I'm not using this project myself anymore, so I'm not really actively maintaining it. I'd be happy to merge a PR with proper tests though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants