ragworm.py :: lib.py :: tests.py

Module tests

Expand source code
# vim: set ts=2:sw=2:et:
from ragworm import *

egs=[]
def eg(fun): global egs; egs += [fun]; return fun

@eg
def thed():
  "show options"
  print(str(the)[:30],"... ",end="")

@eg
def power():
  "powerset"
  print([x for x in powerset([1,2,3])],end=" ")
  return 2**3 -1 == len([x for x in powerset([1,2,3])])


@eg
def csvd():
  "read csv"
  return 3192==sum((len(a) for a in csv(the.file)))

@eg
def lohid():
  "find num ranges"
  num = NUM()
  [add(num,x) for x in range(20)]
  return 0==num.lo and 19==num.hi

@eg
def cache():
  "keep some nums"
  the.Some=16
  num = NUM()
  [add(num,x) for x in range(10**4)]
  has = ok(num)._has
  return 16==len(has) and has[0] <= has[4] <= has[8] <= has[12] <= has[-1]

@eg
def numd():
  "collect numeric stats"
  num = NUM()
  [add(num,r()) for x in range(10**4)]
  print(the.Some,end= " ")
  return the.Some==256 and .28 < div(num) < .32 and .46 < mid(num) < .54

@eg
def symd():
  "collect symbolic stats"
  sym = SYM()
  [add(sym,c) for c in "aaaabbc"]
  return 1.37 < div(sym) < 1.38 and mid(sym)=='a'

@eg
def statd():
  "collect stats from data"
  data0 = DATA("../data/auto93.csv")
  data1,data2 = betters(data0)
  s0 = stats(data0)
  s1 = stats(data1)
  s2 = stats(data2)
  a,l,m="Acc+", "Lbs-", "Mpg+"
  print(s2[m],s0[m],s1[m],end=" ")
  return s1[a] > s2[a] and s1[m] >   s2[m] and s1[l] < s2[l] and \
         s0[a] > s2[a] and s0[m] >=  s2[m] and s0[l] < s2[l]

@eg
def bins():
  "collect stats from data"
  data0 = DATA("../data/auto93.csv")
  best,rest = betters(data0)
  s = set()
  freqs(best,rest,also=lambda cl,lo,hi: s.add((cl,lo,hi)))
  b4=None
  for (cl,lo,hi) in sorted([s1 for s1 in s]): 
     if cl != b4: print("")
     print(cl,data0.cols.all[cl].txt,
           "-inf" if lo==ninf else lo,
           "inf"  if hi==inf else hi)
     b4 = cl

if __name__ == '__main__': runs(the,egs)

Functions

def r()

random() -> x in the interval [0, 1).

def eg(fun)
Expand source code
def eg(fun): global egs; egs += [fun]; return fun
def thed()

show options

Expand source code
@eg
def thed():
  "show options"
  print(str(the)[:30],"... ",end="")
def power()

powerset

Expand source code
@eg
def power():
  "powerset"
  print([x for x in powerset([1,2,3])],end=" ")
  return 2**3 -1 == len([x for x in powerset([1,2,3])])
def csvd()

read csv

Expand source code
@eg
def csvd():
  "read csv"
  return 3192==sum((len(a) for a in csv(the.file)))
def lohid()

find num ranges

Expand source code
@eg
def lohid():
  "find num ranges"
  num = NUM()
  [add(num,x) for x in range(20)]
  return 0==num.lo and 19==num.hi
def cache()

keep some nums

Expand source code
@eg
def cache():
  "keep some nums"
  the.Some=16
  num = NUM()
  [add(num,x) for x in range(10**4)]
  has = ok(num)._has
  return 16==len(has) and has[0] <= has[4] <= has[8] <= has[12] <= has[-1]
def numd()

collect numeric stats

Expand source code
@eg
def numd():
  "collect numeric stats"
  num = NUM()
  [add(num,r()) for x in range(10**4)]
  print(the.Some,end= " ")
  return the.Some==256 and .28 < div(num) < .32 and .46 < mid(num) < .54
def symd()

collect symbolic stats

Expand source code
@eg
def symd():
  "collect symbolic stats"
  sym = SYM()
  [add(sym,c) for c in "aaaabbc"]
  return 1.37 < div(sym) < 1.38 and mid(sym)=='a'
def statd()

collect stats from data

Expand source code
@eg
def statd():
  "collect stats from data"
  data0 = DATA("../data/auto93.csv")
  data1,data2 = betters(data0)
  s0 = stats(data0)
  s1 = stats(data1)
  s2 = stats(data2)
  a,l,m="Acc+", "Lbs-", "Mpg+"
  print(s2[m],s0[m],s1[m],end=" ")
  return s1[a] > s2[a] and s1[m] >   s2[m] and s1[l] < s2[l] and \
         s0[a] > s2[a] and s0[m] >=  s2[m] and s0[l] < s2[l]
def bins()

collect stats from data

Expand source code
@eg
def bins():
  "collect stats from data"
  data0 = DATA("../data/auto93.csv")
  best,rest = betters(data0)
  s = set()
  freqs(best,rest,also=lambda cl,lo,hi: s.add((cl,lo,hi)))
  b4=None
  for (cl,lo,hi) in sorted([s1 for s1 in s]): 
     if cl != b4: print("")
     print(cl,data0.cols.all[cl].txt,
           "-inf" if lo==ninf else lo,
           "inf"  if hi==inf else hi)
     b4 = cl