Beispiel #1
0
def test__natsort_key_with_numeric_input_and_PATH_returns_number_in_nested_tuple(
):
    # It gracefully handles as_path for numeric input by putting an extra tuple around it
    # so it will sort against the other as_path results.
    sfunc = _parse_path_factory(string_func)
    bytes_func = _parse_bytes_factory(ns.PATH)
    num_func = _parse_number_factory(ns.PATH, '')
    assert _natsort_key(10, None, sfunc, bytes_func, num_func) == (('', 10), )
Beispiel #2
0
def test__natsort_key_with_tuple_of_paths_and_PATH_returns_triply_nested_tuple(
):
    # PATH also handles recursion well.
    sfunc = _parse_path_factory(string_func)
    bytes_func = _parse_bytes_factory(ns.PATH)
    num_func = _parse_number_factory(ns.PATH, '')
    assert _natsort_key(
        ('/Folder', '/Folder (1)'), None, sfunc, bytes_func,
        num_func) == ((('/', ), ('Folder', )), (('/', ), ('Folder (', 1, ')')))
Beispiel #3
0
def test_parse_number_factory_with_PATH_UNGROUPLETTERS_LOCALE_makes_function_that_returns_nested_tuple(
        x):
    assert _parse_number_factory(ns.PATH | ns.UNGROUPLETTERS | ns.LOCALE,
                                 '')(x) == ((('', ), ('', x)), )
Beispiel #4
0
    text,
    floats,
    integers,
    binary,
)

if PY_VERSION >= 3:
    long = int

regex = _regex_chooser[ns.INT]
pre = _input_string_transform_factory(ns.INT)
post = _string_component_transform_factory(ns.INT)
after = _final_data_transform_factory(ns.INT, '')
string_func = _parse_string_factory(ns.INT, '', regex.split, pre, post, after)
bytes_func = _parse_bytes_factory(ns.INT)
num_func = _parse_number_factory(ns.INT, '')


def test__natsort_key_with_numeric_input_and_PATH_returns_number_in_nested_tuple(
):
    # It gracefully handles as_path for numeric input by putting an extra tuple around it
    # so it will sort against the other as_path results.
    sfunc = _parse_path_factory(string_func)
    bytes_func = _parse_bytes_factory(ns.PATH)
    num_func = _parse_number_factory(ns.PATH, '')
    assert _natsort_key(10, None, sfunc, bytes_func, num_func) == (('', 10), )


@pytest.mark.skipif(PY_VERSION < 3, reason='only valid on python3')
def test__natsort_key_with_bytes_input_and_PATH_returns_number_in_nested_tuple(
):
Beispiel #5
0
def test_parse_number_factory_with_UNGROUPLETTERS_LOCALE_makes_function_that_returns_nested_tuple_example(
):
    assert _parse_number_factory(ns.UNGROUPLETTERS | ns.LOCALE,
                                 '')(57) == (('', ), ('', 57))
Beispiel #6
0
def test_parse_number_factory_with_PATH_makes_function_that_returns_nested_tuple(
        x):
    assert _parse_number_factory(ns.PATH, '')(x) == (('', x), )
Beispiel #7
0
def test_parse_number_factory_makes_function_that_returns_tuple(x):
    assert _parse_number_factory(0, '')(x) == ('', x)
Beispiel #8
0
def test_parse_number_factory_makes_function_that_returns_tuple_example():
    assert _parse_number_factory(0, '')(57) == ('', 57)
    assert _parse_number_factory(0, '')(float('nan')) == ('', float('-inf'))
    assert _parse_number_factory(ns.NANLAST,
                                 '')(float('nan')) == ('', float('+inf'))
Beispiel #9
0
def natsort_keygen(key=None, alg=0, **_kwargs):
    """\
    Generate a key to sort strings and numbers naturally.

    Generate a key to sort strings and numbers naturally,
    not lexicographically. This key is designed for use as the
    `key` argument to functions such as the `sorted` builtin.

    The user may customize the generated function with the
    arguments to `natsort_keygen`, including an optional
    `key` function.

    Parameters
    ----------
    key : callable, optional
        A key used to manipulate the input value before parsing for
        numbers. It is **not** applied recursively.
        It should accept a single argument and return a single value.

    alg : ns enum, optional
        This option is used to control which algorithm `natsort`
        uses when sorting. For details into these options, please see
        the :class:`ns` class documentation. The default is `ns.INT`.

    Returns
    -------
    out : function
        A function that parses input for natural sorting that is
        suitable for passing as the `key` argument to functions
        such as `sorted`.

    See Also
    --------
    natsorted

    Examples
    --------
    `natsort_keygen` is a convenient way to create a custom key
    to sort lists in-place (for example).::

        >>> a = ['num5.10', 'num-3', 'num5.3', 'num2']
        >>> a.sort(key=natsort_keygen(alg=ns.REAL))
        >>> a
        [{u}'num-3', {u}'num2', {u}'num5.10', {u}'num5.3']

    """
    # Transform old arguments to the ns enum.
    try:
        alg = _args_to_enum(**_kwargs) | alg
    except TypeError:
        msg = "natsort_keygen: 'alg' argument must be from the enum 'ns'"
        raise ValueError(msg+', got {0}'.format(py23_str(alg)))

    # Add the _DUMB option if the locale library is broken.
    if alg & ns.LOCALEALPHA and natsort.compat.locale.dumb_sort():
        alg |= ns._DUMB

    # Set some variables that will be passed to the factory functions
    sep = natsort.compat.locale.null_string if alg & ns.LOCALEALPHA else ''
    regex = _regex_chooser[alg & ns._NUMERIC_ONLY]

    # Create the functions that will be used to split strings.
    input_transform = _input_string_transform_factory(alg)
    component_transform = _string_component_transform_factory(alg)
    final_transform = _final_data_transform_factory(alg, sep)

    # Create the high-level parsing functions for strings, bytes, and numbers.
    string_func = _parse_string_factory(
        alg, sep, regex.split,
        input_transform, component_transform, final_transform
    )
    if alg & ns.PATH:
        string_func = _parse_path_factory(string_func)
    bytes_func = _parse_bytes_factory(alg)
    num_func = _parse_number_factory(alg, sep)

    # Return the natsort key with the parsing path pre-chosen.
    return partial(
        _natsort_key,
        key=key,
        string_func=string_func,
        bytes_func=bytes_func,
        num_func=num_func
    )