create.data¶
Data files: Table and TableRow for CSV and TSV, and JSON.
Table¶
struct Table
Rows of text cells read from CSV or TSV, like Processing's Table.
The first line names the columns unless loaded with header=False.
Rows are handed out by reference, so reading a cell copies nothing:
var scores = Table.load("scores.csv")
var best = scores.row(0).string("name")
for ref row in scores.rows():
total += row.int("score")
Implements Copyable, Deinitable, Movable.
Constructors¶
@staticmethod
def __init__(out self, var columns: List[String])
An empty table with these column names, to fill with add_row.
load¶
@staticmethod
def load(
path: String,
header: Bool = True,
separator: Optional[String] = None,
) raises -> Self
Load a CSV or TSV file.
The separator is a tab for a .tsv file and a comma otherwise,
unless separator names one. Quoted fields may hold separators,
newlines and doubled quotes, as RFC 4180 has it.
A relative path is resolved against the directory of the calling
source file, not the working directory, as Image.load's is.
parse¶
@staticmethod
def parse(text: String, header: Bool = True, separator: String = ",") raises -> Self
Parse CSV or TSV text already in memory.
row_count¶
def row_count(self) -> Int
The number of rows, the header line not counted.
column_count¶
def column_count(self) -> Int
The number of columns: the header's, or the widest row's.
columns¶
def columns(self) -> List[String]
The column names, or an empty list for a table with no header.
has_column¶
def has_column(self, name: String) -> Bool
Whether a column has this name.
row¶
def row(ref self, index: Int) -> ref[self._rows["element"]] TableRow
Row index, from 0 to row_count() - 1, by reference.
rows¶
def rows(ref self) -> Span[TableRow, origin_of(self._rows)]
Every row, in order, by reference: for ref row in t.rows().
Rows can be edited through it but not added or removed; that is
add_row and remove_row.
add_row¶
def add_row(mut self) -> ref[self._rows["element"]] TableRow
Append an empty row and return it, to fill with set.
ref row = scores.add_row()
row.set("name", "Ada")
row.set("score", 120)
def add_row(mut self, var cells: List[String]) raises
Append a row of cells, one per column in order.
A table with a header takes exactly one cell per column; one without grows to fit a longer row.
add_column¶
def add_column(mut self, name: String) raises
Append a column, empty in every row.
remove_column¶
def remove_column(mut self, name: String) raises
Remove the named column from the header and every row; the columns after it move left by one.
def remove_column(mut self, index: Int) raises
Remove column index, from 0, as the named form.
remove_row¶
def remove_row(mut self, index: Int) raises
Remove row index; the rows after it move up by one.
find_row_index¶
def find_row_index(self, column: String, value: String) raises -> Optional[Int]
The index of the first row whose cell in the named column is exactly value, or None; row(index) reaches it by reference.
An index rather than the row, since there is no optional reference:
ref row = t.row(t.find_row_index("name", "Ada").value()).
def find_row_index(self, column: Int, value: String) raises -> Optional[Int]
The index of the first row whose cell in column column is exactly value, or None.
find_row_indices¶
def find_row_indices(self, column: String, value: String) raises -> List[Int]
The indices of every row whose cell in the named column is exactly value, in order.
def find_row_indices(self, column: Int, value: String) raises -> List[Int]
The indices of every row whose cell in column column is exactly value, in order.
sort¶
def sort(mut self, column: String, descending: Bool = False) raises
Order the rows by the named column, smallest first.
By number when every cell in the column is one, so "10" follows
"9"; by text otherwise. Stable: rows that tie keep their order.
def sort(mut self, column: Int, descending: Bool = False) raises
Order the rows by column column, from 0, as the named form.
save¶
def save(self, path: String, separator: Optional[String] = None) raises
Write the table as CSV or TSV, the header line first.
The separator follows load's rule. A field is quoted only when it
holds the separator, a quote or a line break, so a file load read
is written back as it was. A relative path is resolved against the
directory of the calling source file, as load's is.
TableRow¶
struct TableRow
One row of a Table, its cells reached by column name or index.
Cells are kept as the text the file held and parsed on read, so a row loads and saves without losing anything. A row shorter than the table reads its missing cells as empty.
for ref row in scores.rows():
canvas.text(row.string("name"), (0, y))
var points = row.float("score")
Implements Copyable, Deinitable, Movable.
string¶
def string(self, column: String) raises -> String
The cell in the named column, as written.
def string(self, column: Int) raises -> String
The cell in column column, from 0, as written.
float¶
def float(self, column: String) raises -> Float64
The cell in the named column, parsed as a number.
def float(self, column: Int) raises -> Float64
The cell in column column, from 0, parsed as a number.
int¶
def int(self, column: String) raises -> Int
The cell in the named column, parsed as a whole number.
def int(self, column: Int) raises -> Int
The cell in column column, from 0, parsed as a whole number.
bool¶
def bool(self, column: String) raises -> Bool
The cell in the named column as a truth value: true or 1, false or 0, case folded.
def bool(self, column: Int) raises -> Bool
The cell in column column, from 0, as a truth value.
set¶
def set(mut self, column: String, var value: String) raises
Set the cell in the named column.
def set(mut self, column: Int, var value: String) raises
Set the cell in column column, from 0.
def set(mut self, column: String, value: Float64) raises
Set the cell in the named column to a number.
def set(mut self, column: Int, value: Float64) raises
Set the cell in column column, from 0, to a number.
def set(mut self, column: String, value: Int) raises
Set the cell in the named column to a whole number.
def set(mut self, column: Int, value: Int) raises
Set the cell in column column, from 0, to a whole number.
def set(mut self, column: String, value: Bool) raises
Set the cell in the named column to true or false.
def set(mut self, column: Int, value: Bool) raises
Set the cell in column column, from 0, to true or false.
JSON¶
struct JSON
A JSON value: an object, array, string, number, bool or null.
Loaded or parsed, a document is read through [] by key or index, and
each step hands back a handle into the same document, so reading a
nested value copies nothing:
var config = JSON.load("config.json")
var speed = config["player"]["speed"].float()
for enemy in config["enemies"].values():
canvas.text(enemy["name"].string(), (0, y))
A handle edits the document it points into, nested assignment and
loops included, so a value read from a document aliases it, as in
Processing, JavaScript and Python. copy() gives an independent one:
config["player"]["speed"] = 3.0
for enemy in config["enemies"].values():
enemy["hp"] = enemy["hp"].int() - 1
var player = config["player"] # still part of config
var snapshot = config.copy() # a document of its own
Assigning a value copies it in, so a value set under two keys is two
values. Replacing a value leaves the old one in the document, unread,
until copy() or save drops it: a document rewritten every frame for
a long run stays small with doc = doc.copy() now and then. Prints as
compact JSON text.
Implements Copyable, Deinitable, Movable, Sized, Writable.
Constructors¶
@staticmethod
def __init__(out self, value: Float64)
A number.
@staticmethod
def __init__(out self, value: Int)
A whole number, kept exact.
@staticmethod
def __init__(out self, value: String)
A string.
@staticmethod
def __init__(out self, value: Bool)
A bool.
@staticmethod
def __init__(out self, *, copy: Self)
A deep copy: a document of its own holding only this value.
__getitem__¶
def __getitem__(self, key: String) raises -> Self
The value under key in an object; raises if there is none.
def __getitem__(self, index: Int) raises -> Self
Item index of an array, from 0; raises if out of range.
__setitem__¶
def __setitem__(self, key: String, value: Self) raises
Set key in an object to a copy of value, adding the key last if it is new.
def __setitem__(self, index: Int, value: Self) raises
Set item index of an array to a copy of value.
__contains__¶
def __contains__(self, key: String) -> Bool
Whether this is an object with key.
object¶
@staticmethod
def object() -> Self
An empty object, to fill with json[key] = value.
array¶
@staticmethod
def array() -> Self
An empty array, to fill with append.
null¶
@staticmethod
def null() -> Self
null.
load¶
@staticmethod
def load(path: String) raises -> Self
Load a JSON file. Errors name path:line:column.
A relative path is resolved against the directory of the calling
source file, not the working directory, as Image.load's is.
parse¶
@staticmethod
def parse(text: String) raises -> Self
Parse JSON text already in memory.
kind¶
def kind(self) -> JSONKind
Which kind of value this is.
is_null¶
def is_null(self) -> Bool
Whether this is null.
__len__¶
def __len__(self) -> Int
The number of an object's keys or an array's items; 0 otherwise.
get¶
def get(self, key: String, default: Self) raises -> Self
The value under key in an object, or default if it has none.
append¶
def append(self, value: Self) raises
Add a copy of value to the end of an array.
remove¶
def remove(self, key: String) raises
Remove key from an object; raises if it has none.
def remove(self, index: Int) raises
Remove item index from an array; later items move up by one.
keys¶
def keys(self) raises -> List[String]
An object's keys, in the order the document has them.
values¶
def values(self) raises -> List[Self]
An array's items, or an object's values, in order.
Each is a handle into this document, like []'s.
items¶
def items(self) raises -> List[Tuple[String, Self]]
An object's keys, each with its value, in the order the document has them, as Python's dict.items(): for name, value in doc.items().
Each value is a handle into this document, like []'s.
float¶
def float(self) raises -> Float64
A number, as a float.
int¶
def int(self) raises -> Int
A whole number. Raises for one with a fraction, like 1.5.
A number written without a fraction or exponent is read exactly,
all 64 bits; 3.0 and 3e2 are whole too.
string¶
def string(self) raises -> String
A string's text.
bool¶
def bool(self) raises -> Bool
A bool's value.
save¶
def save(self, path: String, indent: Int = Int(2)) raises
Write the value to a JSON file, keys in their order.
Indented indent spaces a level, one item per line; 0 writes it
compact, as it prints. Raises for an infinite or NaN number, which
JSON has no way to write. A relative path is resolved against the
directory of the calling source file, as load's is.
JSONKind¶
struct JSONKind
Which of JSON's six kinds of value a JSON holds.
Implements Copyable, Deinitable, Equatable, ImplicitlyCopyable, Movable, Writable.
Constants¶
NULL = JSONKind(Int(0))BOOL = JSONKind(Int(1))NUMBER = JSONKind(Int(2))STRING = JSONKind(Int(3))ARRAY = JSONKind(Int(4))OBJECT = JSONKind(Int(5))
Fields¶
value: Int
Constructors¶
@staticmethod
def __init__(out self, value: Int)
__eq__¶
def __eq__(self, other: Self) -> Bool
__ne__¶
def __ne__(self, other: Self) -> Bool