code.Utils

 1import math
 2import re
 3import sys
 4import random
 5import Num 
 6
 7help = " \n\
 8CSV : summarized csv file \n\
 9(c) 2022 Tim Menzies <timm@ieee.org> BSD-2 license\n\
10USAGE: lua seen.lua [OPTIONS]\n\
11OPTIONS:\n\
12-e  --eg        start-up example                      = nothing\n\
13-d  --dump      on test failure, exit with stack dump = false\n\
14-f  --file      file with csv data                    = ../data/auto93.csv\n\
15-h  --help      show help                             = false\n\
16-n  --nums      number of nums to keep                = 512\n\
17-s  --seed      random number seed                    = 10019\n\
18-S  --seperator feild seperator                       = , "
19
20def coerce(s):
21    s = s.strip()
22    if isinstance(s, int):
23        return int(s)
24    else:
25        try:
26            return float(s)
27        except ValueError:
28            if s in ['true', 'TRUE', 'True']:
29                return True
30            elif s in ['false', 'FALSE', 'False']:
31                return False
32
33def oo(t):
34    if isinstance(t, list):
35        out = "{" + " ".join(t)[:-1] + "}"
36    elif isinstance(t, dict):
37        out = o(t)
38    else:
39        dict_obj = vars(t)
40        del dict_obj['_has']
41        out = (o(vars(t)))
42    print(out)
43    return out
44
45def o(t):
46    out = "{"
47    for k,v in t.items():
48        out += ":" + str(k) + " " + str(v) + " "
49    out = out.strip()
50    out += "}"
51    return out
52
53def parse_csv(fname, fun=None, sep=','):
54    with open(fname, "r") as csv_file:
55        csv_file = csv_file.readlines()
56        for line in csv_file:
57            row = line.split(sep)
58            if fun:
59                fun(row)
60
61def copy(t):
62    if type(t) != dict:
63        return t
64    u = {}
65    for k, v in t.items():
66        u[k] = copy(v)
67    return u
68
69def create_the():
70    the = {}
71    tup_list = re.findall(r'[-][\S]+[\s]+[-][-]([\S]+)[^\n]+= ([\S]+)', help)
72
73    for k,x in tup_list:
74        the[k] = coerce(x)
75    return the
76
77the = create_the()
def coerce(s):
21def coerce(s):
22    s = s.strip()
23    if isinstance(s, int):
24        return int(s)
25    else:
26        try:
27            return float(s)
28        except ValueError:
29            if s in ['true', 'TRUE', 'True']:
30                return True
31            elif s in ['false', 'FALSE', 'False']:
32                return False
def oo(t):
34def oo(t):
35    if isinstance(t, list):
36        out = "{" + " ".join(t)[:-1] + "}"
37    elif isinstance(t, dict):
38        out = o(t)
39    else:
40        dict_obj = vars(t)
41        del dict_obj['_has']
42        out = (o(vars(t)))
43    print(out)
44    return out
def o(t):
46def o(t):
47    out = "{"
48    for k,v in t.items():
49        out += ":" + str(k) + " " + str(v) + " "
50    out = out.strip()
51    out += "}"
52    return out
def parse_csv(fname, fun=None, sep=','):
54def parse_csv(fname, fun=None, sep=','):
55    with open(fname, "r") as csv_file:
56        csv_file = csv_file.readlines()
57        for line in csv_file:
58            row = line.split(sep)
59            if fun:
60                fun(row)
def copy(t):
62def copy(t):
63    if type(t) != dict:
64        return t
65    u = {}
66    for k, v in t.items():
67        u[k] = copy(v)
68    return u
def create_the():
70def create_the():
71    the = {}
72    tup_list = re.findall(r'[-][\S]+[\s]+[-][-]([\S]+)[^\n]+= ([\S]+)', help)
73
74    for k,x in tup_list:
75        the[k] = coerce(x)
76    return the