Skip to content

Commit

Permalink
feat: use polars dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
badele committed Dec 8, 2023
1 parent 4872f35 commit f55ddf9
Show file tree
Hide file tree
Showing 11 changed files with 274 additions and 40 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ jobs:
with:
deno-version: v1.36.0

- name: coverage
- name: Install requirements
run: just install-requirements

- name: Coverage
run: just coverage

- name: Upload coverage
Expand Down
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
.direnv
.coverage

# Node
/.npm-global
/node_modules

.download

samples/initdatas/*.csv
samples/initdatas/*.db
samples/initdatas/tennis_atp
Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ BROWSER := "chromium"
@help:
just -lu --list-heading=$'{{ file_name(justfile()) }} commands:\n'

# Install requirements
@install-requirements:
npm install nodejs-polars

# Check conventional commits
@conventional-check:
cog check
Expand Down
14 changes: 5 additions & 9 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
import { existsSync } from "https://deno.land/std@0.205.0/fs/exists.ts";
import * as path from "https://deno.land/std@0.205.0/path/mod.ts";
import { CFWF } from "./src/cfwf.ts";
import { decodeCSVContent, readDecodedCSVFile } from "./src/converter.ts";
import { readTextFile } from "./src/utils.ts";
import { readCSV } from "./src/converter.ts";

// deno-lint-ignore no-explicit-any
function initDatasetFile(options: any): void {
Expand Down Expand Up @@ -87,7 +86,7 @@ async function convertFile(options: any): Promise<void> {
// Input
switch (iext) {
case ".csv":
tables.push(await readDecodedCSVFile(options.input));
tables.push(await readCSV(options.input));
break;

default:
Expand Down Expand Up @@ -167,13 +166,12 @@ async function addTable(options: any): Promise<void> {
}

if (options.filename) {
const content = await readTextFile(options.filename);
const datas = decodeCSVContent(options.filename, content);
const df = await readCSV(options.filename);

if (table.columns && table.columns.length === 0) {
table.columns = datas.columns;
table.columns = df.columns;
}
table.rows = datas.rows;
table.rows = df.rows;
}

if (options.subtitle) {
Expand All @@ -196,8 +194,6 @@ async function addTable(options: any): Promise<void> {
table.metadatas.aligns = options.aligns;
}

// tables[options.tablename] = table;

cfwf.addTable(table);
Deno.writeTextFileSync(options.configname, JSON.stringify(cfwf.config));
}
Expand Down
166 changes: 166 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"nodejs-polars": "^0.8.3"
}
}
13 changes: 7 additions & 6 deletions samples/samplescripts/generate_samples.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// deno run -A samples/initdatas/initdatas/generate_samples.ts

import { CFWF } from "../../src/cfwf.ts";
import { readDecodedCSVFile } from "../../src/converter.ts";
import { readCSV } from "../../src/converter.ts";

const samples = new CFWF({});
samples.setDatasetProperties({
Expand All @@ -16,7 +16,8 @@ samples.setDatasetProperties({
},
});

const players = await readDecodedCSVFile("samples/initdatas/players.csv");
const players = await readCSV("samples/initdatas/players.csv");

samples.addTable({
tablename: "players",
subtitle: "The best players (number of winning matches) beetween 2012-2022",
Expand All @@ -31,7 +32,7 @@ samples.addTable({
},
});

const australian_open = await readDecodedCSVFile(
const australian_open = await readCSV(
"samples/initdatas/australian_open.csv",
);
samples.addTable({
Expand All @@ -51,7 +52,7 @@ samples.addTable({
},
});

const rgarros = await readDecodedCSVFile("samples/initdatas/roland_garros.csv");
const rgarros = await readCSV("samples/initdatas/roland_garros.csv");
samples.addTable({
tablename: "roland_garros",
subtitle: "Roland Garros winners beetween 2012-2022",
Expand All @@ -71,7 +72,7 @@ samples.addTable({
},
});

const usopen = await readDecodedCSVFile("samples/initdatas/us_open.csv");
const usopen = await readCSV("samples/initdatas/us_open.csv");
samples.addTable({
tablename: "us_open",
subtitle: "US Open winners beetween 2012-2022",
Expand All @@ -90,7 +91,7 @@ samples.addTable({
},
});

const wimbledon = await readDecodedCSVFile("samples/initdatas/wimbledon.csv");
const wimbledon = await readCSV("samples/initdatas/wimbledon.csv");
samples.addTable({
tablename: "wimbledon",
subtitle: "Wimbledon winners beetween 2012-2022",
Expand Down
19 changes: 15 additions & 4 deletions src/cfwf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,21 @@ export class CFWF {
for (let cidx = 0; cidx < row.length; cidx++) {
const col = row[cidx];

if (typeof col === "number" && colnbfloat[cidx] > 0) {
cols.push(modfmt.sprintf("%.*f", colnbfloat[cidx], col));
} else {
cols.push(col.toString());
switch (typeof col) {
case "number": {
if (colnbfloat[cidx] > 0) {
cols.push(modfmt.sprintf("%.*f", colnbfloat[cidx], col));
}

break;
}
case "string": {
cols.push(col.toString());

break;
}
default:
cols.push("");
}
}
srows.push(cols);
Expand Down
Loading

0 comments on commit f55ddf9

Please sign in to comment.