mirror of
http://aero2k.de/t/repos/urlbot-native.git
synced 2017-09-06 15:25:38 +02:00
simple 1v1 chat; thread-safe persistent db; remove levenshtein; cleanup
This commit is contained in:
29
common.py
29
common.py
@@ -32,36 +32,21 @@ def logger(severity, message):
|
||||
sys.stderr.write('%s %s %s: %s\n' % args)
|
||||
|
||||
def conf_save(obj):
|
||||
if conf('persistent_locked'):
|
||||
return False
|
||||
|
||||
set_conf('persistent_locked', True)
|
||||
|
||||
with open(conf('persistent_storage'), 'wb') as fd:
|
||||
return pickle.dump(obj, fd)
|
||||
|
||||
set_conf('persistent_locked', False)
|
||||
|
||||
def conf_load():
|
||||
with open(conf('persistent_storage'), 'rb') as fd:
|
||||
fd.seek(0)
|
||||
return pickle.load(fd)
|
||||
|
||||
def levenshtein(a, b, return_table=False):
|
||||
'''returns the levenshtein distance between a and b'''
|
||||
# initialisize a table with 0, but the 0-rows/cols with their index
|
||||
d = [[(i if 0 == j else j if 0 == i else 0) for j in range(len(b)+1)] for i in range(len(a)+1)]
|
||||
|
||||
i = j = 0
|
||||
for i in range(1, len(a)+1):
|
||||
for j in range(1, len(b)+1):
|
||||
if a[i-1] == b[j-1]:
|
||||
d[i][j] = d[i-1][j-1]
|
||||
else:
|
||||
d[i][j] = min(
|
||||
d[i-1][j] + 1, # deletion
|
||||
d[i][j-1] + 1, # insertion
|
||||
d[i-1][j-1] + 1, # substitution
|
||||
)
|
||||
|
||||
if return_table:
|
||||
return (d, d[i][j])
|
||||
else:
|
||||
return d[i][j]
|
||||
|
||||
def get_version_git():
|
||||
import subprocess
|
||||
|
||||
|
||||
Reference in New Issue
Block a user