On this page:
3.23.1 The Cell  Content Type
Cell  Content
c-empty
c-str
c-num
c-bool
c-custom
is-c-empty
is-c-str
is-c-num
is-c-bool
is-c-custom
3.23.2 Sanitizers
Sanitizer
3.23.2.1 Pre-defined sanitizers
string-sanitizer
num-sanitizer
bool-sanitizer
strict-num-sanitizer
strings-only
booleans-only
numbers-only
empty-only
option-sanitizer

3.23 data-source

Usage:
include data-source
import data-source as ...
The general idea of loading a spreadsheet into a table is straightforward: each cell of the spreadsheet corresponds to a cell of the table. However, spreadsheet files (such as Google Sheets or .csv files) store their data in a serialized format, and loading the file must convert from that representation back into useful Pyret data types. Moreover, Pyret tables expect each of their columns to be homogeneous, but there is nothing enforcing that restriction on arbitrary spreadsheet files.

Accordingly, Pyret exposes a CellContent data type to record what type it thinks each cell contains, and a Sanitizer to allow enforcing a uniform type over all values in a column. These two notions are used by the gdrive-sheets library to convert Google Sheets files into Pyret Tables. A similar library could be written to use these two types to load tables from .csv files or other data sources.

3.23.1 The CellContent Type

data CellContent<A>:
| c-empty
| c-str(s :: String)
| c-num(n :: Number)
| c-bool(b :: Boolean)
| c-custom(datum :: A)
end

c-str :: (n :: String) -> CellContent<a>

c-num :: (s :: Number) -> CellContent<a>

c-bool :: (b :: Boolean) -> CellContent<a>

c-custom :: (datum :: A) -> CellContent<A>

is-c-empty :: (val :: Any) -> Boolean

is-c-str :: (val :: Any) -> Boolean

is-c-num :: (val :: Any) -> Boolean

is-c-bool :: (val :: Any) -> Boolean

is-c-custom :: (val :: Any) -> Boolean

This datatype describes Pyret’s

3.23.2 Sanitizers

Sanitizer<A, B>
A Sanitizer<A, B> is a function with signature (CellContent<A>, String,Number -> B). It takes in the contents of a cell, as well as the coordinates (column name, as given by the load-table header, and zero-indexed row number) of that cell, and attempts to parse the contents of that cell to a value of the intended Pyret type. If a cell’s contents cannot be parsed, the sanitizer may raise an error explaining the problem.

3.23.2.1 Pre-defined sanitizers

string-sanitizer :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> String

This sanitizer tries to convert CellContents containing anything to a String, by calling tostring on it.

num-sanitizer :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> Number

This sanitizer tries to convert CellContents containing numbers, strings, or booleans to a Number. Strings are attempted to be parsed. True and false convert to 1 and 0 respectively. Any other values are rejected, including blank cells.

bool-sanitizer :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> Boolean

This sanitizer tries to convert CellContents containing numbers, strings, or booleans to a Boolean. 0 and 1 convert to false and true respectively. The (case-insensitive) strings "true" and "false" convert appropriately. Any other values are rejected, including blank cells.

strict-num-sanitizer :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> Number

This sanitizer tries to convert CellContents containing strings or numbers to a Number. Strings are attempted to be parsed. Any other values are rejected, including blank cells.

strings-only :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> String

This sanitizer accepts CellContents containing strings only, and rejects all other values, including blank cells.

booleans-only :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> Boolean

This sanitizer accepts CellContents containing booleans only, and rejects all other values, including blank cells.

numbers-only :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> Number

This sanitizer accepts CellContents containing numbers only, and rejects all other values, including blank cells.

empty-only :: (
x :: CellContent<A>,
col :: String,
row :: Number
)
-> Option<A>

This sanitizer accepts CellContents containing blank cells only, and rejects all other values.

option-sanitizer :: (
value-sanitizer :: Sanitizer<A>
)
-> Sanitizer<Option<A>>

This higher-order sanitizer takes in another sanitizer that does not expect to be given blank cells, and produces a new sanitizer that can accept them. Blank cells are converted to none, while valid non-blank cells are converted to a some value containing the converted contents of the cell. Values rejected by the value-sanitizer are rejected by this sanitizer as well.

This sanitizer is useful for handling incomplete data that might contain blank values; a subsequent processing step could filter out the none values.