typestar

csv_report.cs in C#

CsvHelper parses, LINQ aggregates, Spectre renders.

// csv_report: parse CSV with CsvHelper, aggregate, render with Spectre
using System.Globalization;
using CsvHelper;
using Spectre.Console;

var csv = """
Language,Tpm,Accuracy
csharp,95,98.2
rust,88,97.1
csharp,101,99.0
python,104,98.8
rust,92,98.5
""";

using var reader = new StringReader(csv);
using var parser = new CsvReader(reader, CultureInfo.InvariantCulture);
var runs = parser.GetRecords<Run>().ToList();

var table = new Table();
table.AddColumn("language");
table.AddColumn(new TableColumn("runs").RightAligned());
table.AddColumn(new TableColumn("best tpm").RightAligned());

foreach (var g in runs.GroupBy(r => r.Language).OrderBy(g => g.Key))
{
    table.AddRow(g.Key, g.Count().ToString(),
                 g.Max(r => r.Tpm).ToString());
}
AnsiConsole.Write(table);
Console.WriteLine($"{runs.Count} runs total");

record Run(string Language, int Tpm, double Accuracy);

How it works

  1. GetRecords<Run> maps CSV rows onto a record by header name.
  2. GroupBy and Max shape the per-language summary.
  3. The Spectre table gives the report right-aligned numbers.

Keywords and builtins used here

The run, in numbers

Lines
32
Characters to type
864
Tokens
220
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 155 seconds.

Type this snippet

Step 1 of 2 in Encore, step 15 of 16 in The open ecosystem.

← Previous Next →