forked from iamlemec/fastpat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_common.py
More file actions
61 lines (53 loc) · 1.71 KB
/
parse_common.py
File metadata and controls
61 lines (53 loc) · 1.71 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
# common parsing tools
# get descendent text
def get_text(parent,tag,default=''):
child = parent.find(tag)
return (child.text or default) if child is not None else default
# get all text of node
def raw_text(par, sep=''):
return sep.join(par.itertext()).strip()
# preserve memory
def clear(elem):
elem.clear()
while elem.getprevious() is not None:
del elem.getparent()[0]
# insert in chunks
class ChunkInserter:
def __init__(self, con, table=None, cmd=None, cur=None, chunk_size=1000, output=False):
if table is None and cmd is None:
raise('Must specify either table or cmd')
self.con = con
self.cur = cur if cur is not None else con.cursor()
self.table = table
self.cmd = cmd
self.chunk_size = chunk_size
self.output = output
self.items = []
self.i = 0
def insert(self,*args):
self.items.append(args)
if len(self.items) >= self.chunk_size:
self.commit()
return True
else:
return False
def insertmany(self,args):
self.items += args
if len(self.items) >= self.chunk_size:
self.commit()
return True
else:
return False
def commit(self):
self.i += 1
if len(self.items) == 0:
return
if self.cmd is None:
nargs = len(self.items[0])
sign = ','.join(nargs*'?')
self.cmd = 'insert or replace into %s values (%s)' % (self.table, sign)
if self.output:
print('Committing chunk %d (%d)' % (self.i, len(self.items)))
self.cur.executemany(self.cmd, self.items)
self.con.commit()
self.items = []