Example #1
0
def test_random():
    """Try to sort 100,000 randomly generated strings without exception."""

    # Repeat test 100,000 times
    for _ in py23_range(100000):
        # Made a list of five randomly generated strings
        lst = [''.join(sample(printable, randint(7, 30)))
               for __ in py23_range(5)]
        # Try to sort.  If there is an exception, give some detailed info.
        try:
            natsorted(lst)
        except Exception as e:
            msg = "Ended with exception type '{exc}: {msg}'.\n"
            msg += "Failed on the input {lst}."
            fail(msg.format(exc=type(e).__name__, msg=str(e), lst=str(lst)))
Example #2
0
def test_numeric_chars_contains_all_valid_unicode_numeric_and_digit_characters(
):
    set_numeric_hex = set(numeric_hex)
    set_numeric_chars = set(numeric_chars)
    set_digit_chars = set(digit_chars)
    set_decimal_chars = set(decimal_chars)
    for i in py23_range(0X110000):
        try:
            a = py23_unichr(i)
        except ValueError:
            break
        if a in set('0123456789'):
            continue
        if unicodedata.numeric(a, None) is not None:
            assert i in set_numeric_hex
            assert a in set_numeric_chars
        if unicodedata.digit(a, None) is not None:
            assert i in set_numeric_hex
            assert a in set_digit_chars
        if unicodedata.decimal(a, None) is not None:
            assert i in set_numeric_hex
            assert a in set_decimal_chars

    assert set_decimal_chars.isdisjoint(digits_no_decimals)
    assert set_digit_chars.issuperset(digits_no_decimals)

    assert set_decimal_chars.isdisjoint(numeric_no_decimals)
    assert set_numeric_chars.issuperset(numeric_no_decimals)
def test_digit_chars_contains_all_valid_unicode_digit_characters():
    for i in py23_range(0X10FFFF):
        try:
            a = py23_unichr(i)
        except ValueError:
            break
        if a in set('0123456789'):
            continue
        if unicodedata.digit(a, None) is not None:
            assert a in digit_chars
Example #4
0
def test_digit_chars_contains_all_valid_unicode_digit_characters():
    set_numeric_hex = set(numeric_hex)
    set_numeric_chars = set(numeric_chars)
    for i in py23_range(0X110000):
        try:
            a = py23_unichr(i)
        except ValueError:
            break
        if a in set('0123456789'):
            continue
        if unicodedata.digit(a, None) is not None:
            assert i in set_numeric_hex
            assert a in set_numeric_chars
Example #5
0
def test_similar():
    """Try to sort 100,000 randomly generated
    similar strings without exception.
    """

    # Repeat test 100,000 times
    for _ in py23_range(100000):
        # Create a randomly generated string
        base = sample(printable, randint(7, 30))
        # Make a list of strings based on this string,
        # with some randomly generated modifications
        lst = []
        for __ in py23_range(5):
            new_str = copy(base)
            for ___ in py23_range(randint(1, 5)):
                new_str[randint(0, len(base)-1)] = choice(printable)
            lst.append(''.join(new_str))
        # Try to sort.  If there is an exception, give some detailed info.
        try:
            natsorted(lst)
        except Exception as e:
            msg = "Ended with exception type '{exc}: {msg}'.\n"
            msg += "Failed on the input {lst}."
            fail(msg.format(exc=type(e).__name__, msg=str(e), lst=str(lst)))
def chain_functions(functions):
    """
    Chain a list of single-argument functions together and return.

    The functions are applied in list order, and the output of the
    previous functions is passed to the next function.

    Parameters
    ----------
    functions : list
        A list of single-argument functions to chain together.

    Returns
    -------
    A single argument function.

    Examples
    --------
    Chain several functions together!

        >>> funcs = [lambda x: x * 4, len, lambda x: x + 5]
        >>> func = chain_functions(funcs)
        >>> func('hey')
        17

    """
    functions = list(functions)
    if not functions:
        return _no_op
    elif len(functions) == 1:
        return functions[0]
    else:
        func = 'x'
        for i in py23_range(len(functions)):
            func = '_f[{0:d}]({1})'.format(i, func)
        func = 'lambda x, _f=functions: ' + func
        return eval(func, None, {'functions': functions})
Example #7
0
def prof_num_str(a):
    print('*** Basic Call, Numbers as Strings ***')
    for _ in py23_range(1000):
        natsorted(a)
Example #8
0
def prof_parsing(a, msg, key=basic_key):
    print(msg)
    for _ in py23_range(100000):
        key(a)
Example #9
0
def prof_str_asint_unsigned(a):
    print('*** Unsigned Int (Versions) Call ***')
    for _ in py23_range(1000):
        natsorted(a, number_type=int, signed=False)
Example #10
0
def prof_str_unsigned_noexp(a):
    print('*** Unsigned No-Exp Call ***')
    for _ in py23_range(1000):
        natsorted(a, signed=False, exp=False)
Example #11
0
def prof_str_noexp(a):
    print('*** No-Exp Call ***')
    for _ in py23_range(1000):
        natsorted(a, exp=False)
Example #12
0
def prof_parsing(a, msg, key=basic_key):
    print(msg)
    for _ in py23_range(100000):
        key(a)
Example #13
0
def prof_time_to_generate():
    print('*** Generate Plain Key ***')
    for _ in py23_range(100000):
        natsort_keygen()
Example #14
0
    0x20983,
    0x2098C,
    0x2099C,
    0x20AEA,
    0x20AFD,
    0x20B19,
    0x22390,
    0x22998,
    0x23B1B,
    0x2626D,
    0x2F890,
)

# Some code that can be used to create the above list of hex numbers.
if __name__ == "__main__":
    import unicodedata
    from natsort.compat.py23 import py23_range, py23_unichr

    hex_chars = []
    for i in py23_range(0x110000):
        try:
            a = py23_unichr(i)
        except ValueError:
            break
        if a in "0123456789":
            continue
        if unicodedata.numeric(a, None) is not None:
            hex_chars.append(i)

    print(", ".join(["0X{:X}".format(i) for i in hex_chars]))
Example #15
0
# (probably of the digits, but let's be safe).
decimal_chars = [a for a in numeric_chars
                 if unicodedata.decimal(a, None) is not None]

# Create a single string with the above data.
decimals = ''.join(decimal_chars)
digits = ''.join(digit_chars)
numeric = ''.join(numeric_chars)
digits_no_decimals = ''.join([x for x in digits if x not in decimals])
numeric_no_decimals = ''.join([x for x in numeric if x not in decimals])

# Some code that can be used to create the above list of hex numbers.
if __name__ == '__main__':
    import textwrap
    from natsort.compat.py23 import py23_range

    hex_chars = []
    for i in py23_range(0X110000):
        try:
            a = py23_unichr(i)
        except ValueError:
            break
        if a in set('0123456789'):
            continue
        if unicodedata.numeric(a, None) is not None:
            hex_chars.append(i)

    hex_string = ', '.join(['0X{:X}'.format(i) for i in hex_chars])
    for line in textwrap.wrap(hex_string, width=60):
        print('   ', line)
Example #16
0
def prof_str_index(a):
    print('*** Basic Index Call ***')
    for _ in py23_range(1000):
        index_natsorted(a)
Example #17
0
def prof_nested(a):
    print('*** Basic Call, Nested Strings ***')
    for _ in py23_range(1000):
        natsorted(a)
import pytest
from hypothesis import example, given
from hypothesis.strategies import floats, integers, text
from natsort.compat.fastnumbers import fast_float, fast_int
from natsort.compat.locale import get_strxfrm
from natsort.compat.py23 import py23_range, py23_str, py23_unichr
from natsort.ns_enum import NS_DUMB, ns
from natsort.utils import groupletters, string_component_transform_factory

# There are some unicode values that are known failures with the builtin locale
# library on BSD systems that has nothing to do with natsort (a ValueError is
# raised by strxfrm). Let's filter them out.
try:
    bad_uni_chars = frozenset(
        py23_unichr(x) for x in py23_range(0X10fefd, 0X10ffff + 1)
    )
except ValueError:
    # Narrow unicode build... no worries.
    bad_uni_chars = frozenset()


def no_bad_uni_chars(x, _bad_chars=bad_uni_chars):
    """Ensure text does not contain bad unicode characters"""
    return not any(y in _bad_chars for y in x)


def no_null(x):
    """Ensure text does not contain a null character."""
    return "\0" not in x
Example #19
0
def prof_str_unsigned(a):
    print('*** Unsigned Call ***')
    for _ in py23_range(1000):
        natsorted(a, signed=False)
Example #20
0
def load_locale(x):
    """ Convenience to load a locale, trying ISO8859-1 first."""
    try:
        locale.setlocale(locale.LC_ALL, str('{0}.ISO8859-1'.format(x)))
    except:
        locale.setlocale(locale.LC_ALL, str('{0}.UTF-8'.format(x)))

# Check if de_DE is installed.
try:
    load_locale('de_DE')
    has_locale_de_DE = True
except locale.Error:
    has_locale_de_DE = False

# Depending on the python version, use lower or casefold
# to make a string lowercase.
try:
    low = py23_str.casefold
except AttributeError:
    low = py23_str.lower

# There are some unicode values that are known failures on BSD systems
# that has nothing to do with natsort (a ValueError is raised by strxfrm).
# Let's filter them out.
try:
    bad_uni_chars = set(py23_unichr(x) for x in py23_range(0X10fefd,
                                                           0X10ffff+1))
except ValueError:
    # Narrow unicode build... no worries.
    bad_uni_chars = set()
Example #21
0
def prof_str_asint(a):
    print('*** Int Call ***')
    for _ in py23_range(1000):
        natsorted(a, number_type=int)
import pytest
from hypothesis import example, given
from hypothesis.strategies import floats, integers, text
from natsort.compat.fastnumbers import fast_float, fast_int
from natsort.compat.locale import get_strxfrm
from natsort.compat.py23 import py23_range, py23_str, py23_unichr
from natsort.ns_enum import NS_DUMB, ns
from natsort.utils import groupletters, string_component_transform_factory

# There are some unicode values that are known failures with the builtin locale
# library on BSD systems that has nothing to do with natsort (a ValueError is
# raised by strxfrm). Let's filter them out.
try:
    bad_uni_chars = frozenset(
        py23_unichr(x) for x in py23_range(0X10fefd, 0X10ffff + 1))
except ValueError:
    # Narrow unicode build... no worries.
    bad_uni_chars = frozenset()


def no_bad_uni_chars(x, _bad_chars=bad_uni_chars):
    """Ensure text does not contain bad unicode characters"""
    return not any(y in _bad_chars for y in x)


def no_null(x):
    """Ensure text does not contain a null character."""
    return "\0" not in x

Example #23
0
def prof_str_key(a):
    print('*** Basic Call With Key ***')
    for _ in py23_range(1000):
        natsorted(a, key=lambda x: x.upper())
Example #24
0
"""\
This file contains functions to profile natsorted with different
inputs and different settings.
"""
from __future__ import print_function
import cProfile
import random
import sys

sys.path.insert(0, '.')
from natsort import natsorted, index_natsorted
from natsort.compat.py23 import py23_range


# Sample lists to sort
nums = random.sample(py23_range(10000), 1000)
nstr = list(map(str, random.sample(py23_range(10000), 1000)))
astr = ['a'+x+'num' for x in map(str, random.sample(py23_range(10000), 1000))]
tstr = [['a'+x, 'a-'+x]
        for x in map(str, random.sample(py23_range(10000), 1000))]
cstr = ['a'+x+'-'+x for x in map(str, random.sample(py23_range(10000), 1000))]


def prof_nums(a):
    print('*** Basic Call, Numbers ***')
    for _ in py23_range(1000):
        natsorted(a)
cProfile.run('prof_nums(nums)', sort='time')


def prof_num_str(a):
Example #25
0
    numeric_chars.append(l)

# The digit characters are a subset of the numerals.
digit_chars = [
    a for a in numeric_chars if unicodedata.digit(a, None) is not None
]

# Create a single string with the above data.
digits = ''.join(digit_chars)
numeric = ''.join(numeric_chars)

# Some code that can be used to create the above list of hex numbers.
if __name__ == '__main__':
    import textwrap
    from natsort.compat.py23 import py23_range

    hex_chars = []
    for i in py23_range(0X110000):
        try:
            a = py23_unichr(i)
        except ValueError:
            break
        if a in set('0123456789'):
            continue
        if unicodedata.numeric(a, None) is not None:
            hex_chars.append(i)

    hex_string = ', '.join(['0X{:X}'.format(i) for i in hex_chars])
    for line in textwrap.wrap(hex_string, width=60):
        print('   ', line)
Example #26
0
def prof_str_unorderable(a):
    print('*** Basic Index Call, "Unorderable" ***')
    for _ in py23_range(1000):
        natsorted(a)
Example #27
0
def prof_nums(a):
    print('*** Basic Call, Numbers ***')
    for _ in py23_range(1000):
        natsorted(a)
Example #28
0
def prof_time_to_generate():
    print("*** Generate Plain Key ***")
    for _ in py23_range(100000):
        natsort_keygen()