Julia Package Butler Updates
1 |
module DataTables |
|
2 |
|
|
3 |
import TableShowUtils, TableTraitsUtils, ReadOnlyArrays |
|
4 |
|
|
5 |
using DataValues |
|
6 |
|
|
7 |
export DataTable, NA, isna |
|
8 |
|
|
9 |
struct DataTable{T, TCOLS} <: AbstractVector{T} |
|
10 | 3 |
columns::TCOLS |
11 |
end
|
|
12 |
|
|
13 |
function fromNT(nt) |
|
14 | 3 |
nt = map(i->i isa ReadOnlyArrays.ReadOnlyArray ? i : ReadOnlyArrays.ReadOnlyArray(i), nt) |
15 |
tx = typeof(nt) |
|
16 | 3 |
et = NamedTuple{propertynames(nt), Tuple{(eltype(fieldtype(tx, i)) for i in 1:fieldcount(typeof(nt)))...}} |
17 | 3 |
return DataTable{et, typeof(nt)}(nt) |
18 |
end
|
|
19 |
|
|
20 |
swap_dva_in(A) = A |
|
21 |
swap_dva_in(A::Array{<:DataValue}) = DataValueArray(A) |
|
22 |
|
|
23 |
function DataTable(;cols...) |
|
24 | 3 |
return fromNT(map(col -> swap_dva_in(col), values(cols))) |
25 |
end
|
|
26 |
|
|
27 |
function DataTable(table) |
|
28 | 3 |
cols, colnames = TableTraitsUtils.create_columns_from_iterabletable(table) |
29 |
|
|
30 | 3 |
return fromNT(NamedTuple{tuple(colnames...)}(tuple(cols...))) |
31 |
end
|
|
32 |
|
|
33 | 3 |
columns(dt::DataTable) = getfield(dt, :columns) |
34 |
|
|
35 | 3 |
@inline Base.getproperty(dt::DataTable, name::Symbol) = getproperty(columns(dt), name) |
36 |
|
|
37 | 1 |
Base.size(dt::DataTable) = size(columns(dt)[1]) |
38 |
|
|
39 | 3 |
Base.IndexStyle(::Type{T}) where {T<:DataTable}= Base.IndexLinear() |
40 |
|
|
41 |
function Base.checkbounds(::Type{Bool}, dt::DataTable, i) |
|
42 | 1 |
cols = columns(dt) |
43 |
if length(cols)==0 |
|
44 |
return true |
|
45 |
else
|
|
46 | 1 |
return checkbounds(Bool, cols[1], i) |
47 |
end
|
|
48 |
end
|
|
49 |
|
|
50 |
@inline function Base.getindex(dt::DataTable{T}, i::Int) where {T} |
|
51 | 3 |
@boundscheck checkbounds(dt, i) |
52 | 3 |
return map(col -> @inbounds(getindex(col, i)), columns(dt)) |
53 |
end
|
|
54 |
|
|
55 |
function Base.show(io::IO, dt::DataTable) |
|
56 | 3 |
TableShowUtils.printtable(io, dt, "DataTable") |
57 |
end
|
|
58 |
|
|
59 |
function Base.show(io::IO, ::MIME"text/plain", dt::DataTable) |
|
60 | 3 |
TableShowUtils.printtable(io, dt, "DataTable") |
61 |
end
|
|
62 |
|
|
63 |
function Base.show(io::IO, ::MIME"text/html", dt::DataTable) |
|
64 | 3 |
TableShowUtils.printHTMLtable(io, dt) |
65 |
end
|
|
66 |
|
|
67 | 3 |
Base.showable(::MIME"text/html", dt::DataTable) = true |
68 |
|
|
69 |
function Base.show(io::IO, ::MIME"application/vnd.dataresource+json", dt::DataTable) |
|
70 | 3 |
TableShowUtils.printdataresource(io, dt) |
71 |
end
|
|
72 |
|
|
73 | 3 |
Base.showable(::MIME"application/vnd.dataresource+json", dt::DataTable) = true |
74 |
|
|
75 |
end
|
Read our documentation on viewing source code .