table

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2022 License: MIT Imports: 9 Imported by: 0

README

Table

go get github.com/nathangreene3/table

A table holds tabular data. Each column has a name and type defined by the first value in the column. The supported types are integers, floats, booleans, time stamps, and strings. A table may be imported from or exported to csv or json.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Fmt0 ...
	//  Int  Str
	// ----------
	//    0  a
	//    1  b
	//    2  c
	Fmt0 = Format{
		UpperHoriz:           "",
		UpperLeftHorizDelim:  "",
		UpperMidHorizDelim:   "",
		UpperRightHorizDelim: "",

		MiddleHoriz:           "-",
		MiddleLeftHorizDelim:  "",
		MiddleMidHorizDelim:   "",
		MiddleRightHorizDelim: "",

		BottomHoriz:           "",
		BottomLeftHorizDelim:  "",
		BottomMidHorizDelim:   "",
		BottomRightHorizDelim: "",

		HeaderLeftDelim:  "",
		HeaderMidDelim:   "",
		HeaderRightDelim: "",

		RowLeftDelim:  "",
		RowMidDelim:   "",
		RowRightDelim: "",
	}

	// Fmt1 ...
	// ----------
	//  Int  Str
	// ----------
	//    0  a
	//    1  b
	//    2  c
	Fmt1 = Format{
		UpperHoriz:           "-",
		UpperLeftHorizDelim:  "",
		UpperMidHorizDelim:   "",
		UpperRightHorizDelim: "",

		MiddleHoriz:           "-",
		MiddleLeftHorizDelim:  "",
		MiddleMidHorizDelim:   "",
		MiddleRightHorizDelim: "",

		BottomHoriz:           "",
		BottomLeftHorizDelim:  "",
		BottomMidHorizDelim:   "",
		BottomRightHorizDelim: "",

		HeaderLeftDelim:  "",
		HeaderMidDelim:   "",
		HeaderRightDelim: "",

		RowLeftDelim:  "",
		RowMidDelim:   "",
		RowRightDelim: "",
	}

	// Fmt2 ...
	//  Int  Str
	// -----------
	//    0  a
	//    1  b
	//    2  c
	// -----------
	Fmt2 = Format{
		UpperHoriz:           "",
		UpperLeftHorizDelim:  "",
		UpperMidHorizDelim:   "",
		UpperRightHorizDelim: "",

		MiddleHoriz:           "-",
		MiddleLeftHorizDelim:  "",
		MiddleMidHorizDelim:   "",
		MiddleRightHorizDelim: "",

		BottomHoriz:           "-",
		BottomLeftHorizDelim:  "",
		BottomMidHorizDelim:   "",
		BottomRightHorizDelim: "",

		HeaderLeftDelim:  "",
		HeaderMidDelim:   "",
		HeaderRightDelim: "",

		RowLeftDelim:  "",
		RowMidDelim:   "",
		RowRightDelim: "",
	}

	// Fmt3 ...
	// ----------
	//  Int  Str
	// ----------
	//    0  a
	//    1  b
	//    2  c
	// ----------
	Fmt3 = Format{
		UpperHoriz:           "-",
		UpperLeftHorizDelim:  "",
		UpperMidHorizDelim:   "",
		UpperRightHorizDelim: "",

		MiddleHoriz:           "-",
		MiddleLeftHorizDelim:  "",
		MiddleMidHorizDelim:   "",
		MiddleRightHorizDelim: "",

		BottomHoriz:           "-",
		BottomLeftHorizDelim:  "",
		BottomMidHorizDelim:   "",
		BottomRightHorizDelim: "",

		HeaderLeftDelim:  "",
		HeaderMidDelim:   "",
		HeaderRightDelim: "",

		RowLeftDelim:  "",
		RowMidDelim:   "",
		RowRightDelim: "",
	}

	// Fmt4 ...
	//  Int   Str
	// ----- -----
	//    0   a
	//    1   b
	//    2   c
	Fmt4 = Format{
		UpperHoriz:           "",
		UpperLeftHorizDelim:  "",
		UpperMidHorizDelim:   "",
		UpperRightHorizDelim: "",

		MiddleHoriz:           "-",
		MiddleLeftHorizDelim:  "",
		MiddleMidHorizDelim:   " ",
		MiddleRightHorizDelim: "",

		BottomHoriz:           "",
		BottomLeftHorizDelim:  "",
		BottomMidHorizDelim:   "",
		BottomRightHorizDelim: "",

		HeaderLeftDelim:  "",
		HeaderMidDelim:   " ",
		HeaderRightDelim: "",

		RowLeftDelim:  "",
		RowMidDelim:   " ",
		RowRightDelim: "",
	}

	// Fmt5 ...
	// +-----+-----+
	// | Int | Str |
	// +-----+-----+
	// |   0 | a   |
	// |   1 | b   |
	// |   2 | c   |
	// +-----+-----+
	Fmt5 = Format{
		UpperHoriz:           "-",
		UpperLeftHorizDelim:  "+",
		UpperMidHorizDelim:   "+",
		UpperRightHorizDelim: "+",

		MiddleHoriz:           "-",
		MiddleLeftHorizDelim:  "+",
		MiddleMidHorizDelim:   "+",
		MiddleRightHorizDelim: "+",

		BottomHoriz:           "-",
		BottomLeftHorizDelim:  "+",
		BottomMidHorizDelim:   "+",
		BottomRightHorizDelim: "+",

		HeaderLeftDelim:  "|",
		HeaderMidDelim:   "|",
		HeaderRightDelim: "|",

		RowLeftDelim:  "|",
		RowMidDelim:   "|",
		RowRightDelim: "|",
	}
)

Functions

This section is empty.

Types

type Body

type Body []interface{}

Body is a list of mn values.

func NewBody

func NewBody(values ...interface{}) Body

NewBody returns a new body of values.

func (Body) Copy

func (b Body) Copy() Body

Copy returns a copy of a body.

func (Body) Equal

func (b Body) Equal(bdy Body) bool

Equal determines if two bodies are equal.

func (Body) String

func (b Body) String() string

String returns a body formatted as [ v0 v1 ... ].

func (Body) Strings

func (b Body) Strings() []string

Strings returns a list of strings in which each string is the string value converted to string by parsing the type. If a value does not parse, the empty string will be inserted.

func (Body) Types

func (b Body) Types() Types

Types returns a list of types of each value.

type Column

type Column []interface{}

Column is a list of values.

func NewCol

func NewCol(values ...interface{}) Column

NewCol returns a new column of values.

func (Column) Copy

func (c Column) Copy() Column

Copy returns a copy of a column.

func (Column) Equal

func (c Column) Equal(col Column) bool

Equal determines if two columns are equal.

func (Column) Type

func (c Column) Type() Type

Type returns the type of the column.

type FTime

type FTime struct {
	// contains filtered or unexported fields
}

FTime is a time.Time together with a format that was either used in parsing the timestamp from a string or set to format the timestamp.

func NewFTime

func NewFTime(t time.Time, format ...string) FTime

NewFTime returns a new FTime. A single format may be passed indicating how the underlying time.Time will be formatted when writing to string. This panics if more than one format is provided. If no format is provided, time.RFC3339Nano is set as the format by default.

func ParseFTime

func ParseFTime(timeStr string, additionalTimeFmts ...string) (FTime, error)

ParseFTime returns a new FTime parsed from the default time formats and any additional time formats.

func (FTime) Compare

func (ft FTime) Compare(ftime FTime) int

Compare compares two time stamps.

func (FTime) Equal

func (ft FTime) Equal(fTime FTime) bool

Equal determines if two FTimes are equal.

func (FTime) Format

func (ft FTime) Format() string

Format returns the underlying format.

func (FTime) String

func (ft FTime) String() string

String returns a timestamp formatted by the underlying format.

func (FTime) Time

func (ft FTime) Time() time.Time

Time returns the underlying time.Time.

type Filterer

type Filterer func(r Row) bool

Filterer determines the criteria for retaining a row.

type Format added in v1.1.0

type Format struct {
	UpperHoriz           string
	UpperLeftHorizDelim  string
	UpperMidHorizDelim   string
	UpperRightHorizDelim string

	MiddleHoriz           string
	MiddleLeftHorizDelim  string
	MiddleMidHorizDelim   string
	MiddleRightHorizDelim string

	BottomHoriz           string
	BottomLeftHorizDelim  string
	BottomMidHorizDelim   string
	BottomRightHorizDelim string

	HeaderLeftDelim  string
	HeaderMidDelim   string
	HeaderRightDelim string

	RowLeftDelim  string
	RowMidDelim   string
	RowRightDelim string
}

Format holds decoration characters for displaying a table.

type Generator

type Generator func(i, j int) interface{}

Generator defines the (i,j)th value.

type Header []string

Header is a list of column names.

func NewHeader

func NewHeader(ss ...string) Header

NewHeader returns a new header.

func (Header) Copy

func (h Header) Copy() Header

Copy returns a copy of a header.

func (Header) Equal

func (h Header) Equal(hdr Header) bool

Equal determines if two headers are equal.

func (Header) String

func (h Header) String() string

String ...

func (Header) Strings

func (h Header) Strings() []string

Strings returns a list of strings in a header.

type Mapper

type Mapper func(r Row)

Mapper mutates a given row.

type Reducer

type Reducer func(dst, src Row)

Reducer combines two rows into one. Only the destination row should be mutated.

type Row

type Row []interface{}

Row is a list of values.

func NewRow

func NewRow(values ...interface{}) Row

NewRow returns a new row.

func (Row) Copy

func (r Row) Copy() Row

Copy returns a copy of a row.

func (Row) Equal

func (r Row) Equal(row Row) bool

Equal determines if two rows are equal.

func (Row) Interfaces

func (r Row) Interfaces() []interface{}

Interfaces returns a list of values in a row.

func (Row) Types

func (r Row) Types() Types

Types returns a list of a row's types.

type Table

type Table struct {
	// contains filtered or unexported fields
}

A Table holds tabular data.

func FromCSV

func FromCSV(fileName string) (*Table, error)

FromCSV returns a new table with data read from a csv file.

func FromJSON

func FromJSON(s string) (*Table, error)

FromJSON returns a new table with data parsed from a json-encoded string. This string should adhere to the following format.

{"header":["", ...],"types":[0, ...],"body":["", ...]}

func Generate added in v1.1.0

func Generate(h Header, m int, f Generator) *Table

Generate returns a new table generated by a generator.

func Join

func Join(tbl ...*Table) *Table

Join several tables having the same number of rows into one.

func New

func New(h Header, r ...Row) *Table

New returns a new table.

func (*Table) Append

func (t *Table) Append(r ...Row) *Table

Append several rows to a table.

func (*Table) AppendCol

func (t *Table) AppendCol(colName string, c Column) *Table

AppendCol appends a column to a table.

func (*Table) Bool added in v1.1.0

func (t *Table) Bool(i, j int) bool

Bool returns the (i,j)th value as a boolean.

func (*Table) Col

func (t *Table) Col(j int) Column

Col returns the jth Column.

func (*Table) ColBools

func (t *Table) ColBools(j int) []bool

ColBools returns the jth column with each value cast as a boolean.

func (*Table) ColFloats added in v1.1.0

func (t *Table) ColFloats(j int) []float64

ColFloats returns the jth column with each value cast as a float.

func (*Table) ColInts

func (t *Table) ColInts(j int) []int

ColInts returns the jth column with each value cast as an integer.

func (*Table) ColStrs

func (t *Table) ColStrs(j int) []string

ColStrs returns the jth column with each value cast as a string.

func (*Table) ColTimes

func (t *Table) ColTimes(j int) []time.Time

ColTimes returns the jth column with each value cast as a time object.

func (*Table) ColType

func (t *Table) ColType(j int) Type

ColType returns the type of the jth column.

func (*Table) ColTypes

func (t *Table) ColTypes() Types

ColTypes returns the column types.

func (*Table) Copy

func (t *Table) Copy() *Table

Copy a table.

func (*Table) Dims

func (t *Table) Dims() (int, int)

Dims returns the number of rows and the number of columns in a table body.

func (*Table) Equal

func (t *Table) Equal(tbl *Table) bool

Equal determines if two tables are equal.

func (*Table) Filter

func (t *Table) Filter(f Filterer) *Table

Filter applies a filterer on each row. Each row in which f evaluates as true is retained; all other rows are discarded.

func (*Table) Float added in v1.1.0

func (t *Table) Float(i, j int) float64

Float returns the (i,j)th value as a float.

func (*Table) Format

func (t *Table) Format(fmt Format) string

Format returns a formatted table given format rules.

func (*Table) Header

func (t *Table) Header() Header

Header returns the header.

func (*Table) Insert

func (t *Table) Insert(i int, r Row) *Table

Insert a row into the ith position.

func (*Table) InsertCol

func (t *Table) InsertCol(j int, colName string, c Column) *Table

InsertCol inserts a column into the jth position.

func (*Table) Int added in v1.1.0

func (t *Table) Int(i, j int) int

Int returns the (i,j)th value as an integer.

func (*Table) JSON added in v1.1.0

func (t *Table) JSON() string

JSON returns a json-encoded string representing a table.

func (*Table) Map

func (t *Table) Map(f Mapper) *Table

Map mutates each row in a table and updates the column types.

func (*Table) MarshalJSON

func (t *Table) MarshalJSON() ([]byte, error)

MarshalJSON returns a list of json-encoded bytes. This implements the json.Marshaller interface.

func (*Table) Reduce

func (t *Table) Reduce(f Reducer) Row

Reduce returns a row that is the product of applying a reducer on each row in a table. A copy of the first row is used as the accumulator.

func (*Table) Remove

func (t *Table) Remove(i int) Row

Remove removes and returns the ith row from a table.

func (*Table) RemoveCol

func (t *Table) RemoveCol(j int) (string, Column)

RemoveCol removes and returns the jth column from a table.

func (*Table) Row

func (t *Table) Row(i int) Row

Row returns the ith row from a table.

func (*Table) Rows

func (t *Table) Rows() []Row

Rows returns a list of all the rows in a table.

func (*Table) Set

func (t *Table) Set(i, j int, v interface{}) *Table

Set the (i,j)th value in a table.

func (*Table) SetColName added in v1.1.0

func (t *Table) SetColName(j int, s string) *Table

SetColName sets the jth column name.

func (*Table) SetHeader added in v1.1.0

func (t *Table) SetHeader(h Header) *Table

SetHeader sets the header.

func (*Table) Sort

func (t *Table) Sort(j int) *Table

Sort sorts a table on the jth column.

func (*Table) Stable

func (t *Table) Stable(j int) *Table

Stable sorts a table on the jth column.

func (*Table) Str added in v1.1.0

func (t *Table) Str(i, j int) string

Str returns the (i,j)th value as a string.

func (*Table) String

func (t *Table) String() string

String returns a string representing a table.

func (*Table) Strings

func (t *Table) Strings() [][]string

Strings returns a list of string lists. The first string list is the header.

func (*Table) Swap

func (t *Table) Swap(i, j int) *Table

Swap swaps two rows in a table.

func (*Table) SwapCols

func (t *Table) SwapCols(i, j int) *Table

SwapCols swaps two columns in a table.

func (*Table) Time added in v1.1.0

func (t *Table) Time(i, j int) time.Time

Time the (i,j)th value as a time object.

func (*Table) UnmarshalJSON

func (t *Table) UnmarshalJSON(b []byte) error

UnmarshalJSON reads a list of json-encoded bytes into a table. Implements the json.Unmarshaller interface.

func (*Table) Validate added in v1.1.0

func (t *Table) Validate() error

Validate returns an error if a table is in an invalid state.

func (*Table) Value added in v1.1.0

func (t *Table) Value(i, j int) interface{}

Value returns the (i,j)th value.

func (*Table) WriteCSV added in v1.1.0

func (t *Table) WriteCSV(file string) error

WriteCSV writes a table to a csv file.

type Type

type Type byte

Type corresponds to a basic type.

const (
	// Inv corresponds to any invalid type.
	Inv Type = iota

	// Int corresponds to type int.
	Int

	// Flt corresponds to type float64.
	Flt

	// Bool corresponds to type bool.
	Bool

	// Time corresponds to type time.Time.
	Time

	// Str corresponds to type string.
	Str
)

func Parse

func Parse(x interface{}) Type

Parse returns the type of a given value.

type Types

type Types []Type

Types is a list of types.

func NewTypes

func NewTypes(ts ...Type) Types

NewTypes returns a new list of types.

func (Types) Copy

func (ts Types) Copy() Types

Copy returns a copy of a list of types.

func (Types) Equal

func (ts Types) Equal(types Types) bool

Equal determines if two lists of types are equal.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL