App-CSVUtils-csv_mix_formulas

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

     csv_mix_formulas(%args) -> [$status_code, $reason, $payload, \%result_meta]

    Mix several formulas/recipes (lists of ingredients and their
    weights/volumes) into one, and output the combined formula.

    Each formula is a CSV comprised of at least two fields. The first field
    (by default literally the first field, but can also be specified using
    "--ingredient-field") is assumed to contain the name of ingredients. The
    second field (by default literally the second field, but can also be
    specified using "--weight-field") is assumed to contain the weight of
    ingredients. A percent form is recognized and will be converted to its
    decimal form (e.g. "60%" or "60.0 %" will become 0.6).

    Example, mixing this CSV:

     ingredient,%weight,extra-field1,extra-field2
     water,80,foo,bar
     sugar,15,foo,bar
     citric acid,0.3,foo,bar
     strawberry syrup,4.7,foo,bar

README  view on Meta::CPAN


        Whether output CSV should have a header row.

        By default, a header row will be output *if* input CSV has header
        row. Under "--output-header", a header row will be output even if
        input CSV does not have header row (value will be something like
        "col0,col1,..."). Under "--no-output-header", header row will *not*
        be printed even if input CSV has header row. So this option can be
        used to unconditionally add or remove header row.

    *   output_percent => *bool*

        If enabled, will convert output weights to percent with the percent
        sign (e.g. 0.6 to "60%").

    *   output_percent_nosign => *bool*

        If enabled, will convert output weights to percent without the
        percent sign (e.g. 0.6 to "60").

    *   output_quote_char => *str*

        Specify field quote character in output CSV, will be passed to
        Text::CSV_XS.

        This is like "--input-quote-char" option but for output instead of
        input.

        Defaults to """ (double quote). Overrides "--output-tsv" option.

lib/App/CSVUtils/csv_mix_formulas.pm  view on Meta::CPAN

gen_csv_util(
    name => 'csv_mix_formulas',
    summary => 'Mix several formulas/recipes (lists of ingredients and their weights/volumes) into one, '.
        'and output the combined formula',
    description => <<'MARKDOWN',

Each formula is a CSV comprised of at least two fields. The first field (by
default literally the first field, but can also be specified using
`--ingredient-field`) is assumed to contain the name of ingredients. The second
field (by default literally the second field, but can also be specified using
`--weight-field`) is assumed to contain the weight of ingredients. A percent
form is recognized and will be converted to its decimal form (e.g. "60%" or
"60.0 %" will become 0.6).

Example, mixing this CSV:

    ingredient,%weight,extra-field1,extra-field2
    water,80,foo,bar
    sugar,15,foo,bar
    citric acid,0.3,foo,bar
    strawberry syrup,4.7,foo,bar

lib/App/CSVUtils/csv_mix_formulas.pm  view on Meta::CPAN

        },
        weight_field => {
            summary => 'Specify field which contain the weights',
            schema => 'str*',
        },
        output_format => {
            summary => 'A sprintf() template to format the weight',
            schema => 'str*',
            tags => ['category:formatting'],
        },
        output_percent => {
            summary => 'If enabled, will convert output weights to percent with the percent sign (e.g. 0.6 to "60%")',
            schema => 'bool*',
            tags => ['category:formatting'],
        },
        output_percent_nosign => {
            summary => 'If enabled, will convert output weights to percent without the percent sign (e.g. 0.6 to "60")',
            schema => 'bool*',
            tags => ['category:formatting'],
        },
    },
    add_args_rels => {
        choose_one => ['output_percent', 'output_percent_nosign'],
        choose_all => ['ingredient_field', 'weight_field'],
    },
    tags => ['category:combining'],

    # we modify from csv-concat

    reads_multiple_csv => 1,

    before_open_input_files => sub {
        my $r = shift;

lib/App/CSVUtils/csv_mix_formulas.pm  view on Meta::CPAN


        # calculate the weights of the mixed formula
        for my $ingredient (keys %{ $ingredients }) {
            $ingredients->{$ingredient}{ $r->{weight_field} } = sum( @{ $ingredients->{$ingredient}{ $r->{weight_field} } } ) / $num_formulas;
        }

        for my $ingredient (sort { ($ingredients->{$b}{ $r->{weight_field} } <=> $ingredients->{$a}{ $r->{weight_field} }) ||
                                       (lc($a) cmp lc($b)) } keys %$ingredients) {

          FORMAT: for my $weight ($ingredients->{ $r->{weight_field} }) {
                if ($r->{util_args}{output_percent}) {
                    $weight = ($weight * 100) . "%";
                    last FORMAT;
                } elsif ($r->{util_args}{output_percent_nosign}) {
                    $weight = ($weight * 100);
                }
                if ($r->{util_args}{output_format}) {
                    $weight = sprintf($r->{util_args}{output_format}, $weight);
                }
            } # FORMAT

            $r->{code_print_row}->($ingredients->{$ingredient});
        }
    },

lib/App/CSVUtils/csv_mix_formulas.pm  view on Meta::CPAN

Usage:

 csv_mix_formulas(%args) -> [$status_code, $reason, $payload, \%result_meta]

Mix several formulasE<sol>recipes (lists of ingredients and their weightsE<sol>volumes) into one, and output the combined formula.

Each formula is a CSV comprised of at least two fields. The first field (by
default literally the first field, but can also be specified using
C<--ingredient-field>) is assumed to contain the name of ingredients. The second
field (by default literally the second field, but can also be specified using
C<--weight-field>) is assumed to contain the weight of ingredients. A percent
form is recognized and will be converted to its decimal form (e.g. "60%" or
"60.0 %" will become 0.6).

Example, mixing this CSV:

 ingredient,%weight,extra-field1,extra-field2
 water,80,foo,bar
 sugar,15,foo,bar
 citric acid,0.3,foo,bar
 strawberry syrup,4.7,foo,bar

lib/App/CSVUtils/csv_mix_formulas.pm  view on Meta::CPAN


Whether output CSV should have a header row.

By default, a header row will be output I<if> input CSV has header row. Under
C<--output-header>, a header row will be output even if input CSV does not have
header row (value will be something like "col0,col1,..."). Under
C<--no-output-header>, header row will I<not> be printed even if input CSV has
header row. So this option can be used to unconditionally add or remove header
row.

=item * B<output_percent> => I<bool>

If enabled, will convert output weights to percent with the percent sign (e.g. 0.6 to "60%").

=item * B<output_percent_nosign> => I<bool>

If enabled, will convert output weights to percent without the percent sign (e.g. 0.6 to "60").

=item * B<output_quote_char> => I<str>

Specify field quote character in output CSV, will be passed to Text::CSV_XS.

This is like C<--input-quote-char> option but for output instead of input.

Defaults to C<"> (double quote). Overrides C<--output-tsv> option.

=item * B<output_quote_empty> => I<bool> (default: 0)

script/csv-mix-formulas  view on Meta::CPAN

=head1 VERSION

This document describes version 0.002 of csv-mix-formulas (from Perl distribution App-CSVUtils-csv_mix_formulas), released on 2024-02-24.

=head1 SYNOPSIS

B<csv-mix-formulas> B<L<--help|/"--help, -h, -?">> (or B<L<-h|/"--help, -h, -?">>, B<L<-?|/"--help, -h, -?">>)

B<csv-mix-formulas> B<L<--version|/"--version, -v">> (or B<L<-v|/"--version, -v">>)

B<csv-mix-formulas> [B<L<--debug|/"--debug">>|B<L<--log-level|/"--log-level=s">>=I<level>|B<L<--quiet|/"--quiet">>|B<L<--trace|/"--trace">>|B<L<--verbose|/"--verbose">>] [B<L<--format|/"--format=s">>=I<name>|B<L<--json|/"--json">>] [B<L<--ingredient-...

=head1 DESCRIPTION

Each formula is a CSV comprised of at least two fields. The first field (by
default literally the first field, but can also be specified using
C<--ingredient-field>) is assumed to contain the name of ingredients. The second
field (by default literally the second field, but can also be specified using
C<--weight-field>) is assumed to contain the weight of ingredients. A percent
form is recognized and will be converted to its decimal form (e.g. "60%" or
"60.0 %" will become 0.6).

Example, mixing this CSV:

 ingredient,%weight,extra-field1,extra-field2
 water,80,foo,bar
 sugar,15,foo,bar
 citric acid,0.3,foo,bar
 strawberry syrup,4.7,foo,bar

script/csv-mix-formulas  view on Meta::CPAN

=back

=head2 Formatting options

=over

=item B<--output-format>=I<s>

A sprintf() template to format the weight.

=item B<--output-percent>

If enabled, will convert output weights to percent with the percent sign (e.g. 0.6 to "60%").

=item B<--output-percent-nosign>

If enabled, will convert output weights to percent without the percent sign (e.g. 0.6 to "60").

=back

=head2 Input options

=over

=item B<--input-escape-char>=I<s>

Specify character to escape value in field in input CSV, will be passed to Text::CSV_XS.



( run in 0.404 second using v1.01-cache-2.11-cpan-05162d3a2b1 )