diff options
author | Seth Morton <seth.m.morton@gmail.com> | 2023-02-26 23:32:02 -0800 |
---|---|---|
committer | Seth Morton <seth.m.morton@gmail.com> | 2023-02-26 23:32:02 -0800 |
commit | 4ae64aa2ee7f254c82ab4700147f7480eff807d7 (patch) | |
tree | 2b85e3998d77349a1f5d54394c6e7333f55642d0 /natsort/utils.py | |
parent | 24f1ba6573e837732f5c410c9b1e0f2e214cd7fb (diff) | |
download | natsort-4ae64aa2ee7f254c82ab4700147f7480eff807d7.tar.gz |
Enable new, more performant fastnumbers
The new version can convert the entire mapping internally, so we use
that if possible. A new wrapper for this new function is introduced to
make the calling code consistent.
Diffstat (limited to 'natsort/utils.py')
-rw-r--r-- | natsort/utils.py | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/natsort/utils.py b/natsort/utils.py index 062b1c6..b86225e 100644 --- a/natsort/utils.py +++ b/natsort/utils.py @@ -61,7 +61,7 @@ from typing import ( ) from unicodedata import normalize -from natsort.compat.fastnumbers import fast_float, fast_int +from natsort.compat.fastnumbers import try_float, try_int from natsort.compat.locale import ( StrOrBytes, get_decimal_point, @@ -111,7 +111,7 @@ NumTransformer = Callable[[Any], NumTransform] # For the string component transform factory StrBytesNum = Union[str, bytes, float, int] -StrTransformer = Callable[[str], StrBytesNum] +StrTransformer = Callable[[Iterable[str]], Iterator[StrBytesNum]] # For the final data transform factory FinalTransform = AnyTuple @@ -505,7 +505,7 @@ def parse_string_factory( c = compose_input(b) # Decompose unicode if using LOCALE d = splitter(c) # Split string into components. e = filter(None, d) # Remove empty strings. - f = map(component_transform, e) # Apply transform on components. + f = component_transform(e) # Apply transform on components. g = sep_inserter(f, sep) # Insert '' between numbers. return final_transform(g, original) # Apply the final transform. @@ -688,14 +688,14 @@ def string_component_transform_factory(alg: NSType) -> StrTransformer: func_chain.append(get_strxfrm()) # Return the correct chained functions. - kwargs: Dict[str, Union[float, Callable[[str], StrOrBytes]]] - kwargs = {"key": chain_functions(func_chain)} if func_chain else {} + kwargs: Dict[str, Union[float, Callable[[str], StrOrBytes], bool]] + kwargs = {"on_fail": chain_functions(func_chain)} if func_chain else {} + kwargs["map"] = True if alg & ns.FLOAT: - # noinspection PyTypeChecker kwargs["nan"] = nan_val - return cast(Callable[[str], StrOrBytes], partial(fast_float, **kwargs)) + return cast(StrTransformer, partial(try_float, **kwargs)) else: - return cast(Callable[[str], StrOrBytes], partial(fast_int, **kwargs)) + return cast(StrTransformer, partial(try_int, **kwargs)) def final_data_transform_factory( |