Example #1
0
def test_option():
    assert_dshape_equal(Option(int32), Option(int32))

    with pytest.raises(AssertionError) as e:
        assert_dshape_equal(Option(int32), Option(float32))
    assert "'int32' != 'float32'" in str(e.value)
    assert '_.ty' in str(e.value)
Example #2
0
def test_unite_records():
    assert (discover([{
        'name': 'Alice',
        'balance': 100
    }, {
        'name': 'Bob',
        'balance': ''
    }]) == 2 * Record([['balance', Option(int64)], ['name', string]]))

    assert (discover([{
        'name': 'Alice',
        's': 'foo'
    }, {
        'name': 'Bob',
        's': None
    }]) == 2 * Record([['name', string], ['s', Option(string)]]))

    assert (discover([{
        'name': 'Alice',
        's': 'foo',
        'f': 1.0
    }, {
        'name': 'Bob',
        's': None,
        'f': None
    }]) == 2 * Record([['f', Option(float64)], ['name', string],
                       ['s', Option(string)]]))
Example #3
0
def test_unite_tuples():
    assert (discover([[1, 1, 'hello'], [1, '', ''], [1, 1, 'hello']]) == 3 *
            Tuple([int64, Option(int64), Option(string)]))

    assert (discover([[1, 1, 'hello', 1], [1, '', '', 1], [1, 1, 'hello',
                                                           1]]) == 3 *
            Tuple([int64, Option(int64),
                   Option(string), int64]))
Example #4
0
def build_datashape(sample_data):
    """ Build a datashape from sample_data 
        In case of multiple dimensions, the firt is replace by 'var' to 
        handle varing time samples in models
    """
    if isinstance(sample_data, collections.Mapping):
        return Record((k,build_datashape(v)) for k,v in sample_data.items() )
    else:
        ds = discover(sample_data)
        if len(ds) > 1:
            ds = var *  ds.subarray(1)
        else:
            ds = Option( ds )
        return ds
Example #5
0
def test_unite_missing_values():
    assert unite([int32, null, int32]) == 3 * Option(int32)
    assert unite([string, null, int32])
def test_to_numpy_fails():
    ds = var * int32
    with pytest.raises(TypeError):
        to_numpy(ds)
    with pytest.raises(TypeError):
        to_numpy(Option(int32))
def test_tuple_str():
    assert str(Tuple([Option(int32), float64])) == '(?int32, float64)'
def test_funcproto_attrs():
    f = dshape('(int32, ?float64) -> {a: ?string}').measure
    assert f.restype == DataShape(Record([('a', Option(String()))]))
    assert f.argtypes == (DataShape(int32), DataShape(Option(float64)))
def test_option_to_numpy_dtype_fails(base):
    Option(base).to_numpy_dtype()
def test_option_to_numpy_dtype(base, expected):
    assert Option(base).to_numpy_dtype() == np.dtype(expected)
def test_option_timedelta_to_numpy(unit):
    assert (Option(TimeDelta(unit=unit)).to_numpy_dtype() == np.dtype(
        'timedelta64[%s]' % unit))
def test_option_datetime_to_numpy():
    assert Option(DateTime()).to_numpy_dtype() == np.dtype('datetime64[us]')
def test_option_sanitizes_strings():
    assert Option('float32').ty == dshape('float32').measure
    ))
def test_invalid_record_literal(invalid):
    assert pytest.raises(TypeError, getitem, R, invalid)


@pytest.mark.parametrize(['names', 'typ'],
                         [(['foo', b'\xc4\x87'.decode('utf8')], unicode),
                          (['foo', 'bar'], str), (list(u'ab'), unicode)])
def test_unicode_record_names(names, typ):
    types = [int64, float64]
    record = Record(list(zip(names, types)))
    string_type, = set(map(type, record.names))
    assert record.names == names
    assert record.types == types
    assert all(isinstance(s, string_type) for s in record.names)


equiv_dshape_pairs = [(dshape('?string'), Option('string')),
                      (dshape('string'), string),
                      (dshape('datetime'), datetime_),
                      (dshape('?datetime'), Option(datetime_)),
                      (dshape('10 * int32'), 10 * int32),
                      (dshape('var * ?int32'), var * Option(int32)),
                      (dshape('10 * ?float64'), Fixed(10) * Option(float64))]


@pytest.mark.parametrize('a,b', equiv_dshape_pairs)
def test_hash_and_eq_consistency(a, b):
    assert a == b
    assert hash(a) == hash(b)