Builtins and Libraries
3.1 Global Utilities
3.2 Numbers
3.3 Strings
3.4 Booleans
3.5 Raw  Array
3.6 Tables
3.7 lists
3.8 sets
3.9 arrays
3.10 string-dict
3.11 option
3.12 pick
3.13 either
3.14 srcloc
3.15 pprint
3.16 s-exp
3.17 s-exp-structs
3.18 image-structs
3.19 image
3.20 world
3.21 gdrive-sheets
3.22 data-source
3.23 reactors
3.24 chart
3.25 plot
3.26 statistics
3.27 math
On this page:
3.22.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.22.2 Sanitizers
Sanitizer
3.22.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.22 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.22.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.22.2 Sanitizers

Sanitizer<A, B>
A Sanitizer<A, B> is a function with signature CellContent<A>, String, Number -> B.

3.22.2.1 Pre-defined sanitizers

string-sanitizer :: (
x :: CellContent<A>,
col :: Number,
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 :: Number,
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 :: Number,
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 :: Number,
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 :: Number,
row :: Number
)
-> String

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

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

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

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

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

empty-only :: (
x :: CellContent<A>,
col :: Number,
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.