utilities

package module
v0.1.34 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 24 Imported by: 4

README

Utilities

Go Report Card godoc

Everybody has little functions that the find themselves writing ... and re-writing in different packages. Enough! I got tired of that, so I created a package for it.

You might find them useful, too.

plotly

Included are functions that work with the excellent Go Plotly project. Some of these are convenience functions -- structs to hold common plot parameters such as title. Others output plots in standard formats such as png and jpeg. These rely on the orca app. The steps to use this app on Linux are:

  1. Download the latest release of the AppImage.

  2. Put it somewhere safe.

  3. chmod +x

  4. make a symbolic link called "orca" in your path.

    orca requires FUSE. FUSE installation instructions are here

Note that --no-sandbox is added to orca per this thread.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Browser = "xdg-open"

Browser is the browser in which to show plots. This starts as the system default, but can be changed. For instance, Browser="vivaldi" will place the plot in Vivaldi.

Functions

func Aligner added in v0.1.20

func Aligner[ChLeft, ChRight any | int32 | int64 | float32 | float64 | string | time.Time](left []ChLeft, right []ChRight, pad int) []string

Aligner returns a slice of strings suitable for printing the two input slices

func Any2Date added in v0.0.9

func Any2Date(inVal any) (*time.Time, error)

Any2Date attempts to convert inVal to a date (time.Time). Returns nil if this fails.

func Any2Float32 added in v0.0.9

func Any2Float32(inVal any) (*float32, error)

Any2Float32 attempts to convert inVal to float32. Returns nil if this fails.

func Any2Float64 added in v0.0.9

func Any2Float64(inVal any) (*float64, error)

Any2Float64 attempts to convert inVal to float64. Returns nil if this fails.

func Any2Int added in v0.0.9

func Any2Int(inVal any) (*int, error)

Any2Int attempts to convert inVal to int. Returns nil if this fails.

func Any2Int32 added in v0.0.9

func Any2Int32(inVal any) (*int32, error)

Any2Int32 attempts to convert inVal to int32. Returns nil if this fails.

func Any2Int64 added in v0.0.9

func Any2Int64(inVal any) (*int64, error)

Any2Int64 attempts to convert inVal to int64. Returns nil if this fails.

func Any2Kind added in v0.0.9

func Any2Kind(inVal any, kind reflect.Kind) (any, error)

func Any2String added in v0.0.9

func Any2String(inVal any) string

func AnySlice2Float64 added in v0.0.12

func AnySlice2Float64(inVal []any) ([]float64, error)

func BuildQuery

func BuildQuery(srcQry string, replacers keyval.KeyVal) (qry string)

BuildQuery replaces the placeholders with values placeholders have the form "?key". BuildQuery prepends a "?" to the keys in replacers.

func Comparer added in v0.0.9

func Comparer(xa, xb any, comp string) (truth bool, err error)

Comparer compares xa and xb. Comparisons available are: ==, !=, >, <, >=, <=

func CopyFile added in v0.0.6

func CopyFile(sourceFile, destFile string) error

CopyFile copies sourceFile to destFile

func CopyFiles added in v0.0.6

func CopyFiles(fromDir, toDir string) error

CopyFiles recursively copies files from fromDir to toDir

func DBExists added in v0.0.11

func DBExists(db string, conn *chutils.Connect) error

DBExists returns an error if db does not exist

func DeDupeSlice added in v0.1.31

func DeDupeSlice(inSlc []string, sortReturn bool) (outSlc []string)

DeDupeSlice removes duplicate entries of inSlc, sorting if sortReturn

func DropTable added in v0.0.5

func DropTable(table string, conn *chutils.Connect) error

DropTable drops the table from ClickHouse

func Fig2File added in v0.1.16

func Fig2File(fig *grob.Fig, plotType PlotlyImage, outDir, outFile string) error

Fig2File outputs a plotly figure to a graphics file (png, jpg, etc.) This func requires that orca be installed. Inputs:

  • fig. plotly figure
  • plotType. graph type. One of: png, jpeg, webp, svg, pdf, eps, emf
  • outDir. Output directory.
  • outFile. Filename of output, with NO extension.

func FileExists

func FileExists(file string) error

FileExists returns an error if "file" does not exist.

func GTAny added in v0.0.9

func GTAny(xa, xb any) (truth bool, err error)

GTAny compares xa > xb

func GetTTYecho added in v0.1.33

func GetTTYecho(prompt string) string

GetTTYecho reads a response from the TTY while echoing the user's typing

func GetTTYnoecho added in v0.1.33

func GetTTYnoecho(prompt string) string

GetTTYnoecho reads a response from the TTYgit without echoing the user's typing

func HTML2File added in v0.1.18

func HTML2File(htmlFile string, plotType PlotlyImage, outDir, outFile string) error

HTML2File produces an image file from a plotly html file This func requires that orca be installed. Inputs

  • htmlFile. plotly html file
  • plotType. graph type. One of: png, jpeg, webp, svg, pdf, eps, emf
  • outDir. Output directory.
  • outFile. Filename of output, with NO extension.

func Has

func Has(needle, delim string, haystack ...string) bool

Has returns true if needle is in haystack

func LTAny added in v0.0.10

func LTAny(x, y any) (bool, error)

LTAny returns x<y for select underlying types of "any"

func Matched added in v0.1.18

func Matched(inStr, startChar, endChar string) (string, error)

Matched returns a substring that is between the outermost set of startChar/endChar

func MaxInt

func MaxInt(ints ...int) int

MaxInt returns the maximum of ints

func MinInt

func MinInt(ints ...int) int

MinInt returns the minimum of ints

func Pad added in v0.1.31

func Pad(inTable [][]string, pad int) string

Pad pads each element of inTable so the columns line up with pad spaces separating them

func Plotter added in v0.1.19

func Plotter(fig *grob.Fig, lay *grob.Layout, pd *PlotDef) error

Plotter plots the Plotly Figure fig with Layout lay. The layout is augmented by features I commonly use.

fig      plotly figure
lay      plotly layout (nil is OK)
pd       PlotDef structure with plot options.

lay can be initialized with any additional layout options needed.

func Position

func Position(needle, delim string, haystack ...string) int

Position returns the index of needle in the haystack. It returns -1 if needle is not found. If haystack has length 1, then it is split into a slice with delimiter ",".

func PrettyDur added in v0.0.12

func PrettyDur(startTime time.Time) string

PrettyDur returns a run duration in a minutes/seconds format

func PrettyString added in v0.1.31

func PrettyString(x any) string

PrettyString returns a string version of x suitable for printing

func RandNorm added in v0.0.6

func RandNorm(n int) ([]float64, error)

RandNorm generates a slice whose elements are N(0,1)

func RandUnifFlt added in v0.0.6

func RandUnifFlt(n int) ([]float64, error)

RandUnifFlt generates a slice whose elements are random U(0, 1) floats

func RandUnifInt added in v0.0.6

func RandUnifInt(n, upper int) ([]int64, error)

RandUnifInt generates a slice whose elements are random U[0,upper) int64's

func RandomLetters added in v0.0.7

func RandomLetters(length int) string

RandomLetters generates a string of length "length" by randomly choosing from a-z

func ReplaceSmart added in v0.0.12

func ReplaceSmart(source, oldChar, newChar, delim string) string

ReplaceSmart replaces old with new except when it occurs within delim

func Slash

func Slash(inStr string) string

Slash adds a trailing slash if inStr doesn't end in a slash

func String2Kind added in v0.0.9

func String2Kind(str string) reflect.Kind

String2Kind converts a string specifying a type to the reflect.Kind

func TableExists

func TableExists(table string, conn *chutils.Connect) error

TableExists returns an error if "table" does not exist. conn is the DB connector.

func TableOrQuery added in v0.0.5

func TableOrQuery(table string) string

TableOrQuery takes table and returns a query. If it is already a query (has "select"), just returns the original value.

func TempFile

func TempFile(ext string, length int) string

TempFile produces a random temp file name in the system's tmp location. The file has extension "ext". The file name begins with "tmp" has length 3 + length.

func TempTable

func TempTable(tmpDB string, length int) string

TempTable produces a random table name. The table name begins with "tmp". The table's name has length 3 +length. tmpDB is the database name.

func ToClickHouse added in v0.1.21

func ToClickHouse(inVal any) string

ToClickHouse returns a string suitable for a ClickHouse constant value

func ToFile

func ToFile(fileName, text string) error

ToFile writes string to file fileName, which is created

func ToLastDay added in v0.0.13

func ToLastDay(dt time.Time) (eom time.Time)

ToLastDay moves a date to the last day of the month

func YesNo added in v0.0.12

func YesNo(inStr string) (bool, error)

YesNo determines if inStr is yes/no, return true if "yes"

Types

type HistData added in v0.1.20

type HistData struct {
	Levels   []any             // levels of the field
	Counts   []int64           // counts
	Prop     []float32         // proportions
	Total    int64             // total counts
	Qry      string            // query used to pull the data
	FieldDef *chutils.FieldDef // field defs of returns
	Fig      *grob.Fig         // histogram
}

HistData represents a histogram constructed from querying ClickHouse

func NewHistData added in v0.1.20

func NewHistData(rootQry, field, where string, conn *chutils.Connect) (*HistData, error)

NewHistData pulls the data from ClickHouse and creates a plotly histogram

func (*HistData) String added in v0.1.20

func (hd *HistData) String() string

type PlotDef added in v0.1.19

type PlotDef struct {
	Show       bool          // Show - true = show graph in browser
	Title      string        // Title - plot title
	XTitle     string        // XTitle - x-axis title
	YTitle     string        // Ytitle - y-axis title
	STitle     string        // STitle - sub-title (under the x-axis)
	Legend     bool          // Legend - true = show legend
	Height     float64       // Height - height of graph, in pixels
	Width      float64       // Width - width of graph, in pixels
	FileName   string        // FileName - output file for (no suffix, no path)
	OutDir     string        // Outdir - output directory
	ImageTypes []PlotlyImage // image type(s) to create (e.g. png, jpg...)
}

PlotDef specifies Plotly Layout features I commonly use.

type PlotlyImage added in v0.1.20

type PlotlyImage int

PlotlyImage is the type of image file to create

const (
	PlotlyJPEG PlotlyImage = 0 + iota
	PlotlyPNG
	PlotlyHTML
	PlotlyPDF
	PlotlyWEBP
	PlotlySVG
	PlotlyEPS
	PlotlyEMF
)

func (PlotlyImage) String added in v0.1.20

func (pi PlotlyImage) String() string

type QuantileData added in v0.1.21

type QuantileData struct {
	Q        []float32         // quantiles at u
	U        []float32         // u values (0-1)
	Total    int64             // total sample size
	Qry      string            // query used to pull the data
	FieldDef *chutils.FieldDef // field defs of fields pulled
	Fig      *grob.Fig         // quantile plot
}

QuantileData represents a quantile plot constructed from querying ClickHouse

func NewQuantileData added in v0.1.21

func NewQuantileData(rootQry, field, where string, conn *chutils.Connect) (*QuantileData, error)

NewQuantileData pulls the data from ClickHouse and creates a plotly quantile plot

type Table added in v0.1.31

type Table struct {
	RowNames []string
	ColNames []string
	Data     [][]any // stored by columns
	// contains filtered or unexported fields
}

Table holds a table

func (*Table) CleanUp added in v0.1.31

func (cd *Table) CleanUp()

CleanUp removes empty rows. A row is not empty if it has an element that is float/int/date or a string that are not ""

func (*Table) String added in v0.1.31

func (cd *Table) String() string

func (*Table) Write added in v0.1.31

func (cd *Table) Write(outFile string, markDown bool) error

Write writes the table to a file. If markDown a markdown table is created.

type XYData added in v0.1.26

type XYData struct {
	X         []any               // quantiles at u
	Y         [][]any             // u values (0-1)
	Qry       string              // query used to pull the data
	XfieldDef *chutils.FieldDef   // field def of X field
	YfieldDef []*chutils.FieldDef // field def of Y field
	Fig       *grob.Fig           // xy plot
}

func NewXYData added in v0.1.26

func NewXYData(rootQry, where, fields, colors, lineTypes string, conn *chutils.Connect) (*XYData, error)

Jump to

Keyboard shortcuts

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