예제 #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)))
예제 #2
0
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
예제 #3
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)))
예제 #4
0
def prof_str_asint_unsigned(a):
    print('*** Unsigned Int (Versions) Call ***')
    for _ in py23_range(1000):
        natsorted(a, number_type=int, signed=False)
예제 #5
0
# -*- coding: utf-8 -*-
"""\
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.py23compat 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)
예제 #6
0
def prof_num_str(a):
    print('*** Basic Call, Numbers as Strings ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #7
0
def prof_str_index(a):
    print('*** Basic Index Call ***')
    for _ in py23_range(1000):
        index_natsorted(a)
예제 #8
0
def prof_str_noexp(a):
    print('*** No-Exp Call ***')
    for _ in py23_range(1000):
        natsorted(a, exp=False)
예제 #9
0
def prof_str_asint(a):
    print('*** Int Call ***')
    for _ in py23_range(1000):
        natsorted(a, number_type=int)
예제 #10
0
def prof_nums(a):
    print('*** Basic Call, Numbers ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #11
0
def prof_str_unsigned(a):
    print('*** Unsigned Call ***')
    for _ in py23_range(1000):
        natsorted(a, signed=False)
예제 #12
0
def prof_str_unsigned_noexp(a):
    print('*** Unsigned No-Exp Call ***')
    for _ in py23_range(1000):
        natsorted(a, signed=False, exp=False)
예제 #13
0
def prof_str_noexp(a):
    print('*** No-Exp Call ***')
    for _ in py23_range(1000):
        natsorted(a, exp=False)
예제 #14
0
def prof_nested(a):
    print('*** Basic Call, Nested Strings ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #15
0
def prof_str_index(a):
    print('*** Basic Index Call ***')
    for _ in py23_range(1000):
        index_natsorted(a)
예제 #16
0
def prof_str_index_key(a):
    print('*** Basic Index Call With Key ***')
    for _ in py23_range(1000):
        index_natsorted(a, key=lambda x: x.upper())
예제 #17
0
def prof_str_asint_unsigned(a):
    print('*** Unsigned Int (Versions) Call ***')
    for _ in py23_range(1000):
        natsorted(a, number_type=int, signed=False)
예제 #18
0
def prof_str_unorderable(a):
    print('*** Basic Index Call, "Unorderable" ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #19
0
def prof_str_key(a):
    print('*** Basic Call With Key ***')
    for _ in py23_range(1000):
        natsorted(a, key=lambda x: x.upper())
예제 #20
0
def prof_num_str(a):
    print('*** Basic Call, Numbers as Strings ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #21
0
def prof_str_unsigned_noexp(a):
    print('*** Unsigned No-Exp Call ***')
    for _ in py23_range(1000):
        natsorted(a, signed=False, exp=False)
예제 #22
0
def prof_nested(a):
    print('*** Basic Call, Nested Strings ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #23
0
def prof_str_unorderable(a):
    print('*** Basic Index Call, "Unorderable" ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #24
0
def prof_str_unsigned(a):
    print('*** Unsigned Call ***')
    for _ in py23_range(1000):
        natsorted(a, signed=False)
예제 #25
0
def prof_nums(a):
    print('*** Basic Call, Numbers ***')
    for _ in py23_range(1000):
        natsorted(a)
예제 #26
0
def prof_str_asint(a):
    print('*** Int Call ***')
    for _ in py23_range(1000):
        natsorted(a, number_type=int)
예제 #27
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.py23compat 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):