예제 #1
0
def test_decoder_returns_function_that_can_decode_bytes_but_return_non_bytes_as_is(
):
    func = decoder("latin1")
    str_obj = "bytes"
    int_obj = 14
    assert func(b"bytes") == str_obj
    assert func(int_obj) is int_obj  # returns as-is, same object ID
    assert func(
        str_obj) is str_obj  # same object returned b/c only bytes has decode
예제 #2
0
def test_decoder_returns_function_that_can_decode_bytes_but_return_non_bytes_as_is():
    f = decoder('latin1')
    a = 'bytes'
    b = 14
    assert f(b'bytes') == a
    assert f(b) is b  # returns as-is, same object ID
    if sys.version[0] == '3':
        assert f(a) is a  # same object returned on Python3 b/c only bytes has decode
    else:
        assert f(a) is not a
        assert f(a) == a  # not same object on Python2 because str can decode
예제 #3
0
from natsort import index_natsorted, order_by_index
a = ['a2', 'a9', 'a1', 'a4', 'a10']
b = [4, 5, 6, 7, 8]
c = ['hi', 'lo', 'ah', 'do', 'up']
index = index_natsorted(a)
order_by_index(a, index)
order_by_index(b, index)
order_by_index(c, index)

a = ['a2', 'a9', 'a1', 'a4', 'a10']
natsorted(a, reverse=True)

from natsort import as_ascii
a = [b'a', 14.0, 'b']
natsorted(a, key=as_ascii) == [14.0, b'a', 'b']

from natsort import as_utf8
a = [b'a56', b'a5', b'a6', b'a40']
natsorted(a)  # doctest: +SKIP
natsorted(a, key=as_utf8) == [b'a5', b'a6', b'a40', b'a56']

from natsort import decoder
a = [b'a56', b'a5', b'a6', b'a40']
natsorted(a, key=decoder('latin1')) == [b'a5', b'a6', b'a40', b'a56']

a = [1, 2, 3, 4, 5]
b = sorted(a, key=lambda x: x, reverse=True)
b

sorted(a, key=lambda x: x[0], reverse=True)
예제 #4
0
def test_as_utf8_returns_bytes_as_utf8():
    assert decoder('utf8')(b'bytes') == as_utf8(b'bytes')
예제 #5
0
def test_as_ascii_returns_bytes_as_ascii():
    assert decoder('ascii')(b'bytes') == as_ascii(b'bytes')
예제 #6
0
def test_as_utf8_converts_bytes_to_utf8():
    assert decoder("utf8")(b"bytes") == as_utf8(b"bytes")
예제 #7
0
def test_as_ascii_converts_bytes_to_ascii():
    assert decoder("ascii")(b"bytes") == as_ascii(b"bytes")