Ejemplo n.º 1
0
def main(encoders, printer):

	summary = OrderedDict()
	for name, encoder in encoders:
		summary[name] = 0

	for index, count, name, values in get_all():
		item = {}

		for enc_name, encoder in encoders:
			size = encoder.bytes_length(values)
			item[enc_name] = size
			summary[enc_name] += size

		printer.write_item(name, index + 1, count, item)
	else:
		printer.write_summary(summary)
from sampledata import get_all

class Stats:
	def __init__(self):
		self.count = 0
		self.sum = 0
		self.min = None
		self.max = None

value  = Stats()
length = Stats()
all_values = set()

for i, count, name, values in get_all():
	value.count += 1

	all_values.update(set(values))

	if value.min is None:
		value.min = min(values)
		value.max = max(values)
	else:
		value.min = min(value.min, min(values))
		value.max = max(value.max, max(values))

	if length.min is None:
		length.min = length.max = len(values)
		
	length.min = min(length.min, len(values))
	length.max = max(length.max, len(values))
	length.count += 1
def demo(values):
	enc = Encoder(values)
	sequence, pairs = enc.encode()

	orig_size   = sum(encoded_size(v) for v in values)

	seq_size    = sum(encoded_size(v) for v in sequence)

	pairs_size  = sum(encoded_size(v) for v in pairs)
	pairs_size += sum(encoded_size(pair[0]) + encoded_size(pair[1]) for pair in pairs.itervalues())

	print 'original size: ', orig_size
	print
	print 'sequence size: ', seq_size
	print '   pairs size: ', pairs_size
	print '   total size: ', seq_size + pairs_size


if __name__ == '__main__':

	def get_diff(values):
		return [values[0]] + [b - a for a, b in zip(values, values[1:])]

	for index, count, name, values in get_all():
		print "verifying %d/%d..." % (index + 1, count)
		verify(get_diff(values))
		print "ok"

	print "encoding example"
	demo(get_diff(sample()))