code cleaning
This commit is contained in:
+50
-46
@@ -150,53 +150,10 @@ def find_nearest(word, wlist, threshold=5):
|
||||
return found[1]
|
||||
|
||||
|
||||
def safe_index(src, pattern, start=0):
|
||||
"""
|
||||
:param (list|str) src:
|
||||
:param pattern:
|
||||
:param (int) start:
|
||||
"""
|
||||
try:
|
||||
return src.index(pattern, start)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def find_all(src, pattern):
|
||||
"""
|
||||
:param (str) src:
|
||||
:param (str) pattern:
|
||||
:rtype: list of int
|
||||
"""
|
||||
o = []
|
||||
i = safe_index(src, pattern)
|
||||
while i is not None:
|
||||
o += [i]
|
||||
i = safe_index(src, pattern, i + 1)
|
||||
return o
|
||||
|
||||
|
||||
def replace_at(src, pattern, indexes, remove):
|
||||
"""
|
||||
:param (str) src:
|
||||
:param (str) pattern:
|
||||
:param (list of int) indexes:
|
||||
:param (int) remove:
|
||||
:rtype: str
|
||||
"""
|
||||
o = ""
|
||||
last = 0
|
||||
for i in indexes:
|
||||
o += src[last:i] + pattern
|
||||
last = i + remove
|
||||
o += src[last:]
|
||||
return o
|
||||
|
||||
|
||||
def break_text(src, n):
|
||||
"""
|
||||
:param (str) src:
|
||||
:param (int) n:
|
||||
:param (str) src: source string
|
||||
:param (int) n: number of lines
|
||||
:rtype: str
|
||||
"""
|
||||
spaces = find_all(src, " ")
|
||||
@@ -209,13 +166,48 @@ def break_text(src, n):
|
||||
return replace_at(src, "\n", indexes, 1)
|
||||
|
||||
|
||||
def find_all(src, pattern):
|
||||
"""
|
||||
:param (str) src: source string
|
||||
:param (str) pattern: pattern to find
|
||||
:rtype: list of int
|
||||
:return: all indexes of the pattern
|
||||
"""
|
||||
o = []
|
||||
i = safe_index(src, pattern)
|
||||
while i is not None:
|
||||
o += [i]
|
||||
i = safe_index(src, pattern, i + 1)
|
||||
return o
|
||||
|
||||
|
||||
def replace_at(src, pattern, indexes, remove):
|
||||
"""
|
||||
:param (str) src: source string
|
||||
:param (str) pattern: string to inject
|
||||
:param (list of int) indexes: places to inject
|
||||
:param (int) remove: how much to remove at each index
|
||||
:rtype: str
|
||||
:return
|
||||
"""
|
||||
o = ""
|
||||
last = 0
|
||||
for i in indexes:
|
||||
o += src[last:i] + pattern
|
||||
last = i + remove
|
||||
o += src[last:]
|
||||
return o
|
||||
|
||||
|
||||
def best_fit(a, b):
|
||||
"""
|
||||
select for each item of a the closest item of b
|
||||
|
||||
:param (list of float) a:
|
||||
:param (list of int) b:
|
||||
:rtype: list of int
|
||||
"""
|
||||
a = a[::]
|
||||
a = a[:]
|
||||
o = []
|
||||
dist = sys.maxsize
|
||||
for i, value in enumerate(b):
|
||||
@@ -229,3 +221,15 @@ def best_fit(a, b):
|
||||
if len(a):
|
||||
o += [b[-1]]
|
||||
return o
|
||||
|
||||
|
||||
def safe_index(src, pattern, start=0):
|
||||
"""
|
||||
:param (list|str) src:
|
||||
:param pattern:
|
||||
:param (int) start:
|
||||
"""
|
||||
try:
|
||||
return src.index(pattern, start)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user