Repository navigation
Expand file tree
/
Copy pathtabularize.py
More file actions
executable file
·73 lines (60 loc) · 2.26 KB
/
Copy pathtabularize.py
File metadata and controls
executable file
·73 lines (60 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# (c) Stefan Countryman, 2016
import sqlite3
import re
DEFAULT_TREND_TYPES = ['min','max','mean','rms','n']
conn = sqlite3.connect(':memory:')
c = conn.cursor()
def create_trend_table(trend_type):
c.execute('CREATE TABLE ' + trend_type + ' (t REAL PRIMARY KEY, v REAL)')
def insert(trend_type, line):
c.execute('INSERT INTO ' + trend_type + ' VALUES (?, ?)',
tuple(re.split('[ \t]*', line)))
def join_on_times():
"""Return a cursor (which can be iterated through) and which will return
rows of data for times where all specified trend types have been
provided."""
query = 'SELECT i.t, i.v'
for t in TREND_TYPES[1:]:
query += ', ' + t + '.v'
query += ' FROM ' + TREND_TYPES[0] + ' i'
for t in TREND_TYPES[1:]:
query += ' JOIN ' + t + ' ON i.t = ' + t + '.t'
return c.execute(query)
def read_from_file(infile):
"""Read data in from an input file."""
trend_type = None
for line in infile:
if line in TREND_TYPES:
trend_type = line
elif (not trend_type is None) and len(re.split('[ \t]*', line)) == 2:
insert(trend_type, line)
def parse_arguments():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--trend_types',
help=('Use this flag to specify which trends you will '
'will be feeding in. This can be used to specify '
'multiple trends seperated by commas. DEFAULT: '
+ ','.join(DEFAULT_TREND_TYPES)),
default=','.join(DEFAULT_TREND_TYPES))
args = parser.parse_args()
return args
def main():
import sys
# get arguments from command line
args = parse_arguments()
TREND_TYPES = args.trend_types.split(',')
if '' in TREND_TYPES:
raise ValueError('Cannot provide an empty string as a trend type.')
# create tables
for t in TREND_TYPES:
create_trend_table(t)
# read from stdin
read_from_filename(sys.stdin)
# print results, starting with a header row describing each column
print('\t'.join(TREND_TYPES))
for row in join_on_times():
print('\t'.join([str(x) for x in i]))
if __name__ == "__main__":
main()