forked from pythonql/pythonql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
66 lines (55 loc) · 1.64 KB
/
Copy pathhelpers.py
File metadata and controls
66 lines (55 loc) · 1.64 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
from pythonql.PQTuple import PQTuple
import types
# The tuples that are used within the processor, as well as
# in the return values
def wrap_tuples(list,schema):
_schema = {n:i for (i,n) in schema }
for item in list:
yield PQTuple(item,_schema)
def outer(tuple_list):
if not tuple_list:
return [ None ]
else:
return tuple_list
def flatten(nested_list):
if isinstance(nested_list,list) or isinstance(nested_list,types.GeneratorType):
for i in nested_list:
if isinstance(i,list) or isinstance(i,types.GeneratorType):
for x in flatten(i):
yield x
else:
yield i
else:
yield nested_list
def empty(seq):
if isinstance(seq,types.GeneratorType):
try:
next(seq)
return False
except:
return True
else:
try:
seq[0]
return False
except:
return True
def print_table(tuples,max_len=0):
def fit(str,max_len):
if len(str)<=max_len:
return str + " " * int((max_len - len(str))*1.8)
else:
return str[0:max_len]
if not tuples:
return
schema = [ x[0] for x in sorted(tuples[0].schema.items(), key=lambda z:z[1] ) ]
lengths = [0]* len(tuples[0].tuple)
for x in range(len(tuples[0].tuple)):
lengths[x] = max([ len(repr(y[x])) for y in tuples ] + [len(schema[x])])
if max_len:
lengths[x] = lengths[x] if lengths[x]<max_len else max_len
print( " | ".join( [ fit(x, lengths[i]) for (i,x) in enumerate(schema) ] ))
print( "-".join( [ fit('-'*len(x), lengths[i]) for (i,x) in enumerate(schema) ] ))
for t in tuples:
print( " | ".join( [fit(repr(x), lengths[i]) for (i,x) in enumerate(t.tuple) ] ))
print()