lkj 0.1.33__tar.gz → 0.1.34__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lkj-0.1.33 → lkj-0.1.34}/PKG-INFO +1 -1
- {lkj-0.1.33 → lkj-0.1.34}/lkj/strings.py +24 -12
- {lkj-0.1.33 → lkj-0.1.34}/lkj.egg-info/PKG-INFO +1 -1
- {lkj-0.1.33 → lkj-0.1.34}/setup.cfg +1 -1
- {lkj-0.1.33 → lkj-0.1.34}/LICENSE +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/README.md +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/__init__.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/chunking.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/dicts.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/filesys.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/funcs.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/importing.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/iterables.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/loggers.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj/misc.py +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj.egg-info/SOURCES.txt +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj.egg-info/dependency_links.txt +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj.egg-info/not-zip-safe +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/lkj.egg-info/top_level.txt +0 -0
- {lkj-0.1.33 → lkj-0.1.34}/setup.py +0 -0
|
@@ -201,29 +201,41 @@ def regex_based_substitution(replacements: dict, regex=None, s: str = None):
|
|
|
201
201
|
'I like orange and grapes.'
|
|
202
202
|
|
|
203
203
|
You have access to the ``replacements`` and ``regex`` attributes of the
|
|
204
|
-
``substitute`` function
|
|
204
|
+
``substitute`` function. See how the replacements dict has been ordered by
|
|
205
|
+
descending length of keys. This is to ensure that longer keys are replaced
|
|
206
|
+
before shorter keys, avoiding partial replacements.
|
|
205
207
|
|
|
206
208
|
>>> substitute.replacements
|
|
207
|
-
{'
|
|
209
|
+
{'banana': 'grape', 'apple': 'orange'}
|
|
208
210
|
|
|
209
211
|
"""
|
|
210
212
|
import re
|
|
211
213
|
from functools import partial
|
|
212
214
|
|
|
213
215
|
if regex is None and s is None:
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
regex
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
216
|
+
# Sort keys by length while maintaining value alignment
|
|
217
|
+
sorted_replacements = sorted(
|
|
218
|
+
replacements.items(), key=lambda x: len(x[0]), reverse=True
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
# Create regex pattern from sorted keys (without escaping to allow regex)
|
|
222
|
+
sorted_keys = [pair[0] for pair in sorted_replacements]
|
|
223
|
+
sorted_values = [pair[1] for pair in sorted_replacements]
|
|
224
|
+
regex = re.compile("|".join(sorted_keys))
|
|
225
|
+
|
|
226
|
+
# Prepare the substitution function with aligned replacements
|
|
227
|
+
aligned_replacements = dict(zip(sorted_keys, sorted_values))
|
|
228
|
+
substitute = partial(regex_based_substitution, aligned_replacements, regex)
|
|
229
|
+
substitute.replacements = aligned_replacements
|
|
223
230
|
substitute.regex = regex
|
|
224
231
|
return substitute
|
|
225
|
-
|
|
232
|
+
elif s is not None:
|
|
233
|
+
# Perform substitution using the compiled regex and aligned replacements
|
|
226
234
|
return regex.sub(lambda m: replacements[m.group(0)], s)
|
|
235
|
+
else:
|
|
236
|
+
raise ValueError(
|
|
237
|
+
"Invalid usage: provide either `s` or let the function construct itself."
|
|
238
|
+
)
|
|
227
239
|
|
|
228
240
|
|
|
229
241
|
from typing import Callable, Iterable, Sequence
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|