-
Notifications
You must be signed in to change notification settings - Fork 6
/
table.py
791 lines (674 loc) · 25.2 KB
/
table.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#!/usr/bin/env python
# coding: utf8
"""
"mini pandas.DataFrame"
Table class with Excel + CSV I/O, easy access to columns, HTML output, and much more.
"""
__author__ = "Philippe Guglielmetti"
__copyright__ = "Copyright 2013, Philippe Guglielmetti"
__credits__ = []
__license__ = "LGPL"
import logging
import csv, itertools, codecs, json, collections
import datetime as std_datetime
from lxml import etree as ElementTree
defaultparser=ElementTree.HTMLParser
Element=ElementTree._Element
from datetime import datetime, date, time, timedelta
from .datetime2 import datef, datetimef, timef, timedeltaf, strftimedelta
from .markup import tag, style_str2dict
from .itertools2 import isiterable
def attr(args):
res=''
for key,val in args.items():
k="class" if key=="_class" else key
res+=' %s="%s"'%(k,val)
return res
class Cell(object):
"""Table cell with HTML attributes"""
def __init__(self,data=None,align=None,fmt=None,tag=None,style={}):
"""
:param data: cell value(s) of any type
:param align: string for HTML align attribute
:param fmt: format string applied applied to data
:param tag: called to build each cell. defaults to 'td'
:param style: dict or string for HTML style attribute
"""
if isinstance(data,Element):
tag=data.tag
assert(tag in ('td','th'))
def _recurse(data):
"grab all possible text from the cell content"
if data is None:
return ''
s=''.join(_recurse(x) for x in data.getchildren())
if data.text:
s=data.text+s
if data.tail:
s=s+data.tail
return s
data=Cell.read(_recurse(data))
if isinstance(data,str):
data=data.lstrip().rstrip() #remove spaces, but also trailing \r\n
self.data=data
self.align=align
self.fmt=fmt
self.tag=tag if tag else 'td'
if not isinstance(style,dict):
style=style_str2dict(style)
self.style=style
def __repr__(self):
return str(self.data)
def _repr_html_(self):
return self.html()
@staticmethod
def read(x):
"""interprets x as int, float, string or None"""
try:
x=float(x) #works for x in unicode
xi=int(x) #does not work if x is floating point in unicode (?)
if xi==x: x=xi
except:
if x=='': x=None
return x
def html(self,**kwargs):
""":return: string HTML formatted cell:
* if data is int, default align="right"
* if data is float, default align="right" and fmt='%0.2f'
* if data is :class:`~datetime.timedelta`, align = "right" and formatting is done by :func:`datetime2.strftimedelta`
"""
args={}
args.update(kwargs) #copy needed to avoid side effects
v=self.data
f=self.fmt
if hasattr(v,'_repr_html_'):
try:
s=v._repr_html_()
except Exception as e:
s='ERROR : %s _repr_html_ failed : %s'%(v,e)
return tag(self.tag,s,**args)
style=args.get('style',{})
if not isinstance(style,dict):
style=style_str2dict(style)
if not 'text-align' in style: #HTML 4 and before
a=args.pop('align',self.align)
if isinstance(v,int):
if not a: a="right"
elif isinstance(v,float):
if not a: a="right"
if not f: f='%0.2f'
v=f%v
f=None #don't reformat below
elif isinstance(v,date):
if not a: a="right"
if not f: f='%Y-%m-%d'
v=v.strftime(f)
f=None #don't reformat below
elif isinstance(v,timedelta):
if not a: a="right"
if not f: f='%H:%M:%S'
v=strftimedelta(v,f)
f=None #don't reformat below
if a:
style['text-align']=a
# create style dict by merging default Cell style + parameters
style=dict(self.style,**style) #http://stackoverflow.com/questions/9819602/union-of-dict-objects-in-python
if style:
args['style']=style
if v is None or v=='':
v=" " #for IE8
else:
v=f%v if f else str(v)
return tag(self.tag,v,**args)
class Row(object):
"""Table row with HTML attributes"""
def __init__(self,data,align=None,fmt=None,tag=None,style={}):
"""
:param data: (list of) cell value(s) of any type
:param align: (list of) string for HTML align attribute
:param fmt: (list of) format string applied applied to data
:param tag: (list of) tags called to build each cell. defaults to 'td'
:param style: (list of) dict or string for HTML style attribute
"""
if not isiterable(data) :
data=[data]
data=list(data) #to make it mutable
#ensure params have the same length as data
if not isinstance(style,list): style=[style]
style=style+[None]*(len(data)-len(style))
if not isinstance(align,list): align=[align]
align=align+[None]*(len(data)-len(align))
if not isinstance(fmt,list) : fmt=[fmt]
fmt=fmt+[None]*(len(data)-len(fmt))
if not tag:tag='td'
if not isinstance(tag,list) :
tag=[tag]*(len(data)) #make a full row, in case it's a 'th'
tag=tag+[None]*(len(data)-len(fmt)) #fill the row with None, which will be 'td's
for i,cell in enumerate(data):
if not isinstance(cell,Cell):
cell=Cell(cell,align[i],fmt[i],tag[i],style[i])
else:
pass #ignore attribs for now
data[i]=cell
self.data=data
def __repr__(self):
return str(self.data)
def _repr_html_(self):
return self.html()
def html(self,cell_args={},**kwargs):
"""return in HTML format"""
res=''
for cell in self.data:
res+=cell.html(**cell_args)
return tag('tr',res,**kwargs)
class Table(list):
"""Table class with CSV I/O, easy access to columns, HTML output"""
def __init__(self,data=[],**kwargs):
"""inits a table, optionally by reading a Excel, csv or html file
:param data: list of list of cells, or string as filename
:param titles: optional list of strings used as column id
:param footer: optional list of functions used as column reducers
"""
try:
self.titles=data.titles
except:
self.titles=kwargs.pop('titles',[])
try:
self.footer=data.footer
except:
self.footer=kwargs.pop('footer',[])
filename=None
if isinstance(data,str):
filename=data
data=[]
else: #ensure data is 2D and mutable
if isinstance(data, dict):
data=data.values()
for row in data:
if not isiterable(row): #build a column
row=[row]
self.append(row)
if filename:
self.load(filename,**kwargs)
def __repr__(self):
""":return: repr string of titles+5 first lines"""
return 'Table(len=%d,titles=%s,data=%s)'%(len(self),self.titles,self[:5])
def __str__(self):
""":return: string of full tables with linefeeds"""
res=''
if self.titles:
res+=str(self.titles)+'\n'
for line in self:
res+=str(line)+'\n'
return res
def _repr_html_(self):
return self.html()
def html(self,head=None,foot=None,colstyle={},**kwargs):
"""HTML representation of table
:param head: optional column headers, .titles by default
:param foot: optional column footers, .footer by default
:param style: (list of) dict of style attributes
:param kwargs: optional parameters passed along to tag('table'...
except:
* start=optional start row
* stop=optional end row
used to display a subset of lines. in this case rows with '...' cells
are displayed before and/or after the lines
:return: string HTML representation of table
"""
def TR(data,align=None,fmt=None,tag=None,style={}):
res=''
row=Row(data=data,align=align,fmt=fmt,tag=tag,style=style)
res+=row.html()+'\n'
return res
def THEAD(data,fmt=None,style={}):
res="<thead>\n"
res+=TR(data=data,fmt=fmt,tag='th',style=style)
res+="</thead>\n"
return res
def TFOOT(data,fmt=None,style={}):
res="<tfoot>\n"
res+=TR(data=data,fmt=fmt,tag='th',style=style)
res+="</tfoot>\n"
return res
res=''
if head is None:
head=self.titles
if head:
res+=THEAD(head)
start=kwargs.pop('start',0)
stop=kwargs.pop('stop',len(self))
if start!=0:
res+=TR(['...']*self.ncols(),style=colstyle)
for row in self[start:stop]:
res+=TR(row,style=colstyle)
if stop!=-1 and stop<len(self):
res+=TR(['...']*self.ncols(),style=colstyle)
if foot is None:
foot=self.footer
if foot:
res+=TFOOT(foot)
return tag('table',res,**kwargs)+'\n'
def load(self,filename,**kwargs):
if self.titles: #explicitly set
l=kwargs.setdefault('titles_line',0)
kwargs.setdefault('data_line',l+1)
else: #read titles from the file
l=kwargs.setdefault('titles_line',1)
kwargs.setdefault('data_line',l+1)
ext=filename.split('.')[-1].lower()
if ext in ('xls','xlsx'):
self.read_xls(filename,**kwargs)
elif ext in ('htm','html'):
self.read_html(filename,**kwargs)
elif ext in ('json'):
self.read_json(filename,**kwargs)
else: #try ...
self.read_csv(filename,**kwargs)
return self #to allow chaining
def read_element(self,element, **kwargs):
"""read table from a DOM element.
:Warning: drops all formatting
"""
titles_line=kwargs.pop('titles_line',1)-1
data_line=kwargs.pop('data_line',2)-1
line=0
head=element.find('thead')
if head is not None:
for row in head.findall('tr'):
if line==titles_line:
self.titles=[cell.data for cell in Row(row).data]
line=line+1
body=element.find('tbody')
if body is None:
body=element
for row in body.findall('tr'):
data=[cell.data for cell in Row(row).data]
if not data: continue #skip empty lines
if not self.titles and line==titles_line:
self.titles=data
elif line>=data_line:
self.append(data)
line=line+1
return self
def read_html(self,filename, **kwargs):
"""read first table in HTML file
"""
parser=kwargs.get('parser',defaultparser)
element = ElementTree.parse(filename,parser()).getroot()
try:
if element.tag!='table': # file contains table as topmost tag
element = element.find('.//table') #find first table
except AttributeError:
pass
if element is None:
raise LookupError('no table found in file')
self.read_element(element, **kwargs)
return self
def read_json(self,filename, **kwargs):
"""appends a json file made of lines dictionaries"""
with open(filename, 'r') as file:
for row in json.load(file):
self.append(row)
return self
def read_xls(self, filename, **kwargs):
"""appends an Excel table"""
titles_line=kwargs.pop('titles_line',1)-1
data_line=kwargs.pop('data_line',2)-1
from xlrd import open_workbook
wb = open_workbook(filename)
sheet=kwargs.get('sheet',0)
if isinstance(sheet,str):
s=wb.sheet_by_name(sheet)
else:
s=wb.sheet_by_index(sheet)
for i in range(s.nrows):
line=[Cell.read(s.cell(i,j).value) for j in range(s.ncols)]
if i==titles_line:
self.titles=line
elif i>=data_line:
self.append(line)
return self
def read_csv(self, filename, **kwargs):
"""appends a .csv or similar file to the table"""
titles_line=kwargs.pop('titles_line',1)-1
data_line=kwargs.pop('data_line',2)-1
dialect=kwargs.setdefault('dialect',csv.excel)
delimiter=kwargs.setdefault('delimiter',';')
encoding=kwargs.pop('encoding','utf-8') #must be iso-8859-15 in some cases
errors=kwargs.pop('errors','strict')
def reader(): # version for Python 3
with open(filename, 'rt', errors=errors, encoding=encoding) as f:
csv_reader = csv.reader(f, **kwargs)
for row in csv_reader:
yield row
for i,row in enumerate(reader()):
if i==titles_line: #titles can have no left/right spaces
self.titles=[Cell.read(x) for x in row]
elif i>=data_line:
line=[Cell.read(x) for x in row]
if line!=[None]: #strange last line sometimes ...
self.append(line)
return self
def save(self,filename,**kwargs):
ext=filename.split('.')[-1].lower()
if ext in ('xls','xlsx'):
self.write_xlsx(filename,**kwargs)
elif ext in ('htm','html'):
with open(filename, 'w') as file:
file.write(self.html(**kwargs))
elif ext in ('json'):
with open(filename, 'w') as file:
file.write(self.json(**kwargs))
else: #try ...
self.write_csv(filename,**kwargs)
return self #to allow chaining
def write_xlsx(self,filename, **kwargs):
import xlsxwriter
workbook = xlsxwriter.Workbook(filename)
worksheet = workbook.add_worksheet()
df=workbook.add_format({'num_format': 'dd/mm/yyyy'})
tf=workbook.add_format({'num_format': 'hh:mm:ss'})
dtf=workbook.add_format({'num_format': 'dd/mm/yyyy hh:mm:ss'})
def writerow(i,line):
for j,s in enumerate(line):
if isinstance(s, datetime):
worksheet.write_datetime(i, j,s,dtf)
elif isinstance(s, date):
worksheet.write_datetime(i, j,s,df)
elif isinstance(s, (time,timedelta)):
worksheet.write_datetime(i, j,s,tf)
else:
worksheet.write(i, j,s)
writerow(0,self.titles)
for i,row in enumerate(self):
writerow(i+1,row)
workbook.close()
return self
def json(self, **kwargs):
"""
:return: string JSON representation of table
"""
def json_serial(obj):
"""JSON serializer for objects not serializable by default json code"""
if hasattr(obj, 'isoformat'):
return obj.isoformat()
try:
return str(obj)
except Exception:
pass
raise TypeError ("Type %s not serializable"%(type(obj)))
array=[self.rowasdict(i) for i in range(len(self))]
kwargs.setdefault('default',json_serial)
return json.dumps(array, **kwargs)
def write_csv(self,filename, **kwargs):
""" write the table in Excel csv format, optionally transposed"""
dialect=kwargs.get('dialect','excel')
delimiter=kwargs.get('delimiter',';')
encoding=kwargs.pop('encoding','utf-8') #was iso-8859-15 earlier
empty=''.encode(encoding)
f = open(filename, 'w', newline='', encoding=encoding)
def _encode(line):
res=[]
for s in line:
res.append(s)
return res
writer=csv.writer(f, dialect=dialect, delimiter=delimiter)
if self.titles:
s=_encode(self.titles)
writer.writerow(s)
for line in self:
s=_encode(line)
writer.writerow(s)
f.close()
return self
def __eq__(self,other):
"""compare 2 Tables contents, mainly for tests"""
if self.titles!=other.titles:
return False
if len(self)!=len(other):
return False
for i in range(len(self)):
if self[i]!=other[i]:
return False
return True
def ncols(self):
"""
:return: number of columns, ignoring title
"""
return max(map(len,self))
def find_col(self,title):
"""finds a column from a part of the title"""
title=title.lower()
for i,c in enumerate(self.titles):
if c.lower().find(title)>=0:
return i
return None
def _i(self,column):
'''column index'''
if isinstance(column, int):
return column
try:
return self.titles.index(column)
except ValueError:
return None
def icol(self,column):
"""iterates a column"""
i=self._i(column)
for row in self:
try:
yield row[i]
except:
yield None
def col(self,column,title=False):
i=self._i(column)
res=[x for x in self.icol(i)]
if title:
res=[self.titles[i]]+res
return res
def cols(self,title=False):
"""iterator through columns"""
for i in range(self.ncols()):
yield self.col(i,title)
def transpose(self,titles_column=0):
"""transpose table
:param: titles_column
:return: Table where rows are made from self's columns and vice-versa
"""
res=Table()
for i,row in enumerate(self.cols(self.titles)):
if i==titles_column:
res.titles=row
else:
res.append(row)
return res
def index(self,value,column=0):
"""
:return: int row number of first line where column contains value
"""
for i,v in enumerate(self.icol(column)):
if v==value:
return i
return None
def __getitem__(self, n):
try:
c=self._i(n[1])
except TypeError:
return super(Table,self).__getitem__(n)
else:
rows=super(Table,self).__getitem__(n[0])
if not isinstance(n[0],slice):
return rows[c]
return [row[c] for row in rows]
def get(self,row,col):
return self[row,col]
def set(self,row,col,value):
col=self._i(col)
if row>=len(self):
self.extend([list()]*(1+row-len(self)))
if col>=len(self[row]):
self[row].extend([None]*(1+col-len(self[row])))
self[row][col]=value
def setcol(self,col,value,i=0):
"""set column values
:param col: int or string column index
:param value: single value assigned to whole column or iterable assigned to each cell
:param i: optional int : index of first row to assign
"""
j=self._i(col)
if isiterable(value):
for v in value:
self.set(i,j,v)
i+=1
else:
for i in range(i,len(self)):
self.set(i,j,value)
def append(self,line):
''' appends a line to table
:param line: can be either:
* a list
* a dict or column names:values
'''
if isinstance(line,dict):
r=len(self) #row number
for k,v in line.items():
i=self._i(k)
if i is None: #column doesn't exist:
i=len(self.titles)
self.titles.append(k)
self.set(r,i,v)
else:
list.append(self,list(line))
return self
def addcol(self,title,val=None,i=0):
'''add column to the right'''
col=len(self.titles)
self.titles.append(title)
if not isiterable(val):
val=[val]*(len(self)-i)
for v in val:
self.set(i,col,v)
i+=1
return self
def sort(self,by,reverse=False):
'''sort by column'''
i=self._i(by)
if isinstance(i, int):
list.sort(self,key=lambda x:x[i],reverse=reverse)
else:
list.sort(i,reverse=reverse)
def rowasdict(self,i):
''' returns a line as a dict '''
return collections.OrderedDict(zip(self.titles,self[i]))
def asdict(self):
for i in range(len(self)):
yield self.rowasdict(i)
def groupby_gen(self,by,sort=True,removecol=True):
"""generates subtables
"""
i=self._i(by)
t=self.titles
if removecol: t=t[:i]+t[i+1:]
if sort:
self.sort(i)
else:
pass #groupby will group CONSECUTIVE lines with same i, so entries at bottom of table will replace the earlier entries in dict
for k, g in itertools.groupby(self, key=lambda x:x[i]):
if removecol:
r=Table(titles=t,data=[a[:i]+a[i+1:] for a in g])
else:
r=Table(titles=t,data=list(g))
yield k,r
def groupby(self,by,sort=True,removecol=True):
""" ordered dictionary of subtables
"""
return collections.OrderedDict(
(k,t) for (k,t) in self.groupby_gen(by,sort,removecol)
)
def hierarchy(self,by='Level',
factory=lambda row:(row,[]), #creates an object from a line
linkfct=lambda x,y,row:x[1].append(y) #creates a parend/child relation between x and y. raw is also available (for qty)
):
'''builds a structure from a table containing a "level" column'''
res=[]
i=self._i(by)
stack=[]
for row in self:
obj=factory(row)
level=row[i]
if level==1:
res.append(obj)
while level<=len(stack):
stack.pop()
if stack:
linkfct(stack[-1],obj,row)
stack.append(obj)
return res
def applyf(self,by,f,skiperrors=False):
""" apply a function to a column
:param by: column name of number
:param f: function of the form lambda cell:content
:param skiperrors: bool. if True, errors while running f are ignored
:return: bool True if ok, False if skiperrors==True and conversion failed
"""
res=True
i=self._i(by)
for row in self:
try:
x=row[i]
row[i]=f(x)
except Exception as e:
if not skiperrors:
raise # TODO: change message to ('could not applyf to %s'%x) for Py2+3
res=False
return res
def _datetimeformat(self,by,fmt,function,skiperrors):
"""convert a column to a date, time or datetime
:param by: column name of number
:param fmt: string defining format, or list of formats to try one by one
:param function: function to call
:param skiperrors: bool. if True, conversion errors are ignored
:return: bool True if ok, False if skiperrors==True and conversion failed
"""
if isinstance(fmt,list):
for f in fmt:
res=self._datetimeformat(by, f, function, True if f!=fmt[-1] else skiperrors)
return res
return self.applyf(by,lambda x: function(x,fmt=fmt),skiperrors)
def to_datetime(self,by,fmt='%Y-%m-%d %H:%M:%S',skiperrors=False):
"""convert a column to datetime
"""
return self._datetimeformat(by, fmt, datetimef, skiperrors)
def to_date(self,by,fmt='%Y-%m-%d',skiperrors=False):
"""convert a column to date
"""
return self._datetimeformat(by, fmt, datef, skiperrors)
def to_time(self,by,fmt='%H:%M:%S',skiperrors=False):
"""convert a column to time
"""
return self._datetimeformat(by, fmt, timef, skiperrors)
def to_timedelta(self,by,fmt=None,skiperrors=False):
"""convert a column to time
"""
return self._datetimeformat(by, fmt, timedeltaf, skiperrors)
def total(self,funcs):
"""build a footer row by appling funcs to all columns
"""
funcs=funcs+[None]*(len(self.titles)-len(funcs))
self.footer=[]
for i,f in enumerate(funcs):
try:
self.footer.append(f(self.col(i)))
except:
self.footer.append(f)
return self.footer
def remove_lines_where(self,f,value=(None,0,'')):
"""
:param f: function of the form lambda line:bool returning True if line should be removed
:return: int number of lines removed
"""
i=self._i(f)
if i is not None:
f=lambda x:x[i] in value
from .itertools2 import removef
return len(removef(self,f))