-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
52 lines (38 loc) · 1.41 KB
/
parser.py
File metadata and controls
52 lines (38 loc) · 1.41 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
# -*- coding: utf-8 -*-
import json
class AbsParser(object):
PARSER_NAME = 'abstract'
def __init__(self):
self.pos = 0
def parse(self, data):
self.pos += len(data)
def calculate(self):
return None
def finish(self):
return json.dumps({
'parser': self.PARSER_NAME,
'result': self.calculate()
})
def get_pos(self):
return self.pos
class CounterParser(AbsParser):
PARSER_NAME = 'counter'
def __init__(self):
super(CounterParser, self).__init__()
self.count_table = [ 0 for i in xrange(0, 256)]
def parse(self, data):
for iter in data:
self.count_table[ord(iter)] += 1
super(CounterParser, self).parse(data)
def calculate(self):
items = map(lambda x: (x, self.count_table[x]), xrange(0, len(self.count_table)))
max_value = max(self.count_table)
if max_value == 0: # Empty file
return []
filtered_items = filter(lambda x: x[1] == max_value, items)
return map(lambda x: x[0], filtered_items)
PARSER_COUNTER = 0
def build_parser(id):
if id == PARSER_COUNTER:
return CounterParser()
return None