datamancer/value

    Dark Mode
Search:
Group by:
Source   Edit  

Types

Value = object
  case kind*: ValueKind
  of VString:
      str*: string

  of VInt:
      num*: int

  of VFloat:
      fnum*: float

  of VBool:
      bval*: bool

  of VObject:
      fields*: OrderedTable[string, Value]

  of VNull:
      nil

  
Source   Edit  
ValueKind = enum
  VNull, VBool, VInt, VFloat, VString, VObject
Source   Edit  

Procs

proc `%~`(c: char): Value {....raises: [], tags: [].}
we convert a char to a string! Source   Edit  
proc `%~`(v: bool): Value {....raises: [], tags: [].}
Source   Edit  
proc `%~`(v: OrderedTable[string, Value]): Value {....raises: [], tags: [].}
Source   Edit  
proc `%~`(v: SomeFloat): Value
Source   Edit  
proc `%~`(v: SomeInteger): Value
Source   Edit  
proc `%~`(v: string): Value {....raises: [], tags: [].}
Source   Edit  
proc `%~`[T: not Value](s: openArray[T]): seq[Value]
converts a seq[T] to a seq[Value] Source   Edit  
proc `*`(v`gensym94, w`gensym94: Value): Value {....raises: [Exception], tags: [].}
Adds two Values together, if they are addeable. These operations only work for VInt and VFloat. VInt is converted to floats for the calculation. The result is always a VFloat! Source   Edit  
proc `+`(v`gensym86, w`gensym86: Value): Value {....raises: [Exception], tags: [].}
Adds two Values together, if they are addeable. These operations only work for VInt and VFloat. VInt is converted to floats for the calculation. The result is always a VFloat! Source   Edit  
proc `-`(v`gensym90, w`gensym90: Value): Value {....raises: [Exception], tags: [].}
Adds two Values together, if they are addeable. These operations only work for VInt and VFloat. VInt is converted to floats for the calculation. The result is always a VFloat! Source   Edit  
proc `/`(v`gensym98, w`gensym98: Value): Value {....raises: [Exception], tags: [].}
Adds two Values together, if they are addeable. These operations only work for VInt and VFloat. VInt is converted to floats for the calculation. The result is always a VFloat! Source   Edit  
proc `<`(v, w: Value): bool {....raises: [KeyError, Exception], tags: [].}
checks whether the v is smaller than w Note: this is only defined for a subset of the possible types! Note2: if both are numbers of different kind (VInt and VFloat) the values are compared as a float! For very large values this would be problematic, but here we are lenient and assume the user uses Value for small calculations! Source   Edit  
proc `<=`(v, w: Value): bool {....raises: [KeyError, Exception], tags: [].}
checks whether v is smaller or equal than w Source   Edit  
proc `==`(v, w: Value): bool {....raises: [KeyError], tags: [].}
checks whether the values are equal. Note: if both values are numbers of different kind (VInt and VFloat) the values are both compared as floats! The float comparison happens with a floating point comparison with relatively large epsilon (1-e8). Source   Edit  
proc `[]`(v: Value; key: string): Value {.inline, ...raises: [KeyError], tags: [].}
Source   Edit  
proc `[]=`(v: var Value; key: string; val: Value) {.inline, ...raises: [], tags: [].}
Source   Edit  
func almostEqual(a, b: float; epsilon = 1e-8): bool {....raises: [], tags: [].}
Source   Edit  
proc contains(v: Value; has: Value): bool {....raises: [KeyError], tags: [].}
checks whether has is a subset of v if both are VObject. A subset means that all keys of has are in v and their values match. There may be more fields in v than in has Source   Edit  
proc contains(v: Value; key: string): bool {....raises: [], tags: [].}
Source   Edit  
proc hash(x: Value): Hash {....raises: [], tags: [].}
Source   Edit  
func isBool(s: string): bool {....raises: [], tags: [].}
Source   Edit  
func isInt(s: string): bool {....raises: [], tags: [].}
simple "most likely int" check. If the string only contains digits and _ we consider it an Int Source   Edit  
func isInt(v: Value): bool {....raises: [], tags: [].}
checks whether the string contained in Value is likely an integer For an isFloat equivalent see isNumber. Source   Edit  
func isNull(v: Value): Value {....raises: [], tags: [].}
returns whether v is a VNull value as a VBool Source   Edit  
func isNumber(s: string): bool {....raises: [], tags: [].}
returns true, if s is a number according to our rules:
  • starts with {0..9}
  • ends with {0..9}
  • may contain a single .
  • may contain a single e, E
  • may contain one minus, one plus at beginning and one for exponent
  • else may only contain {0..9}
  • e, +, -, . may not appear one after another
  • may contain space before and after the number

It is only used to decide whether the stringifaction of s will be surrounded by ".

Source   Edit  
func isNumber(v: Value): bool {....raises: [], tags: [].}
Source   Edit  
proc largerOrFalse(v: Value; f: float): bool {.inline, ...raises: [], tags: [].}
extension of < for Value to return false if v is not a valid VInt/VFloat. Source   Edit  
proc newVObject(length = 8): Value {....raises: [], tags: [].}
Source   Edit  
proc null(): Value {....raises: [], tags: [].}
Constructs a VNull value. Source   Edit  
proc pretty(v: Value; precision = 4; emphStrNumber = true): string {.
    ...raises: [ValueError], tags: [].}
converts the given value to its value as a string. For VFloat the precision can be given. If emphStrNumber is true, a number stored as a string will be emphasized by enclosing it with explicit ". This is mainly for printing DFs to show the user if a number is a number or a string. Source   Edit  
proc smallerOrFalse(v: Value; f: float): bool {.inline, ...raises: [], tags: [].}
extension of < for Value to return false if v is not a valid VInt/VFloat. Source   Edit  
proc to[T: int | float | string | bool](v: Value; dtype: typedesc[T]): T
Source   Edit  
proc toBool(v: Value): bool {....raises: [], tags: [].}
Checks if the value is a bool and returns its value Source   Edit  
proc toFloat(v: Value; allowNull: static bool = false): float
Source   Edit  
proc toInt(v: Value): int {....raises: [], tags: [].}
Converts a numeric value to an int. If the value is a float we round and convert to int Source   Edit  
proc toObject(s: (string, Value)): Value {....raises: [], tags: [].}
Source   Edit  
proc toObject(s: seq[(string, Value)]): Value {....raises: [], tags: [].}
converts the given sequence to an object This is only used to store the result of the groups iterator as a Value. Source   Edit  
proc toStr(v: Value): string {....raises: [Exception, ValueError],
                               tags: [RootEffect].}
Returns the value v as a string. If the value is of kind VString, no conversion is required. This however will fail, if the input is of type
  • VNull
  • VObject

if you want string representations of those value types, use $

Source   Edit  
func toValKind[T](dtype: typedesc[T]): ValueKind
Source   Edit  

Iterators

iterator items(row: Value): Value {....raises: [], tags: [].}
Source   Edit  
iterator keys(row: Value): string {....raises: [], tags: [].}
Source   Edit  
iterator pairs(row: Value): tuple[key: string, val: Value] {....raises: [],
    tags: [].}
Iterator for the elements of row. row has to be a JObject representing a row of a DataFrame Source   Edit  

Templates

template `$`(v: Value): string
Source   Edit  
template `%~`(s: openArray[Value]): seq[Value]
Source   Edit  
template convenienceValueComparisons(): untyped

These are a few comparison procedures that are defined for convenience within the context of a f{} formula to allow writing foo == "bar" even if foo is actually a Value based column.

This is handled by simply inserting these templates into the closure body.

Feel free to call the template in a local (or global) scope to make them available in other scopes. Note that the templates are not exported!

Source   Edit  
template withNative(v: Value; valName: untyped; body: untyped): untyped
Source   Edit  
template withNativeConversion(kind: ValueKind; procName: untyped; body: untyped): untyped
generates an environment, in which the correct to* proc is named procName for kind Source   Edit