multiple html output

This commit is contained in:
ppwwyyxx
2014-12-23 00:03:40 +08:00
parent 7ee52e32f5
commit bcaa7dcae6
4 changed files with 107 additions and 17 deletions
+29 -3
View File
@@ -1,11 +1,12 @@
#!/usr/bin/env python2
# -*- coding: UTF-8 -*-
# File: msgslice.py
# Date: Mon Dec 22 23:07:02 2014 +0800
# Date: Mon Dec 22 23:56:55 2014 +0800
# Author: Yuxin Wu <[email protected]>
class MessageSlicer(object):
""" Separate messages into slices by time.
class MessageSlicerByTime(object):
""" Separate messages into slices by time,
for time display in html.
A new day always begins a new slice.
"""
def __init__(self, diff_thres=5 * 60):
@@ -31,4 +32,29 @@ class MessageSlicer(object):
assert len(msgs) == sum([len(k) for k in ret])
return ret
class MessageSlicerBySize(object):
""" Separate messages into slices by max slice size,
to avoid too large html.
"""
def __init__(self, size=1000):
""" a slice will have <= 1.5 * cnt messages"""
self.size = size
assert self.size > 1
def slice(self, msgs):
ret = []
now = []
for m in msgs:
if len(now) >= self.size:
nowtime, lasttime = m.createTime, now[-1].createTime
if nowtime.date() != lasttime.date():
ret.append(now)
now = [m]
continue
now.append(m)
if len(now) > self.size / 2:
ret.append(now)
else:
ret[-1].extend(now)
assert len(msgs) == sum([len(k) for k in ret])
return ret