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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lkj
3
- Version: 0.1.33
3
+ Version: 0.1.34
4
4
  Summary: A dump of homeless useful utils
5
5
  Home-page: https://github.com/thorwhalen/lkj
6
6
  Author: Thor Whalen
@@ -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
- {'apple': 'orange', 'banana': 'grape'}
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
- replacements = dict(replacements)
215
-
216
- if not replacements: # if replacements iterable is empty.
217
- return lambda s: s # return identity function
218
-
219
- regex = re.compile("|".join(re.escape(key) for key in replacements.keys()))
220
-
221
- substitute = partial(regex_based_substitution, replacements, regex)
222
- substitute.replacements = replacements
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
- else:
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: lkj
3
- Version: 0.1.33
3
+ Version: 0.1.34
4
4
  Summary: A dump of homeless useful utils
5
5
  Home-page: https://github.com/thorwhalen/lkj
6
6
  Author: Thor Whalen
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = lkj
3
- version = 0.1.33
3
+ version = 0.1.34
4
4
  url = https://github.com/thorwhalen/lkj
5
5
  platforms = any
6
6
  description_file = README.md
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