-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsutils.py
More file actions
111 lines (94 loc) · 3.41 KB
/
wsutils.py
File metadata and controls
111 lines (94 loc) · 3.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
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
#!/usr/bin/python
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
import sys
from collections import Counter
from io import StringIO
from pathlib import Path
import sqlite3
#---------------------------------------------------------------------------
class Pattern:
def __init__(self, patt=""):
self.patt = patt
@classmethod
def build(cls, word):
charCount = Counter(word)
groupMap = {}
nextGroup = '1'
buffer = StringIO()
for char in word:
if not char.islower():
buffer.write(char)
elif charCount[char] <= 1:
buffer.write('_')
else:
group = groupMap.setdefault(char, nextGroup)
if group == nextGroup:
if nextGroup == '9':
nextGroup = 'A'
else:
nextGroup = chr(ord(nextGroup) + 1)
buffer.write(group)
return cls(buffer.getvalue())
def __str__(self):
return self.patt
def __repr__(self):
return "Pattern('{}')".format(self.patt)
def __eq__(self, other):
return self.patt == other.patt
def matches(self, word):
return self == Pattern.build(word)
def groups(self):
groupCount = Counter(self.patt)
del groupCount['_']
return list(groupCount.items())
#---------------------------------------------------------------------------
class Catalog:
def __init__(self, path):
conn = sqlite3.connect(path, isolation_level="EXCLUSIVE")
self.curs = conn.cursor()
@classmethod
def create(cls, path):
cat = Catalog(path)
cat._createDatabase()
return cat
def add(self, word):
patt = Pattern.build(word)
pattern = str(patt)
self.curs.execute("insert or ignore into words values (?, ?)",
(word, pattern))
def count(self, pattern, glob):
rows = self._query("count(*)", pattern, glob)
return rows[0][0]
def words(self, pattern, glob):
rows = self._query("word", pattern, glob)
return [row[0] for row in rows]
def _query(self, select, pattern, glob):
pattern = str(pattern)
if any(goo != '?' for goo in glob):
qry = "select {} from words where pattern=? and word glob ?" \
.format(select)
args = (pattern, glob)
else:
qry = "select {} from words where pattern=? ".format(select)
args = (pattern,)
self.curs.execute(qry, args)
rows = self.curs.fetchall()
return rows
def close(self):
conn = self.curs.connection
if conn.in_transaction:
conn.commit()
conn.close()
def _createDatabase(self):
self.curs.executescript("""
drop table if exists words;
create table words (
word text not null primary key,
pattern text not null
);
create index idx_words_pattern on words (pattern);
""")
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------
#---------------------------------------------------------------------------