Skip to content

Commit

Permalink
Update to ReadStat 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidanthoff committed Dec 18, 2017
1 parent a7d0cd6 commit a171896
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
9 changes: 5 additions & 4 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
julia 0.6
TableTraits 0.0.1
ReadStat 0.1.1
TableTraits 0.0.3
ReadStat 0.2.0
IterableTables 0.5.0
DataValues 0.1.0
DataFrames 0.10.0
DataValues 0.3.0
FileIO 0.4.0
TableTraitsUtils 0.1.2
IteratorInterfaceExtensions 0.0.2
19 changes: 11 additions & 8 deletions src/StatFiles.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module StatFiles

using ReadStat, TableTraits, DataValues, DataFrames
using ReadStat, IteratorInterfaceExtensions, TableTraits, TableTraitsUtils
using DataValues
import FileIO
import IterableTables

Expand All @@ -20,24 +21,26 @@ function load(f::FileIO.File{FileIO.format"SAS"})
return StatFile(f.filename)
end

TableTraits.isiterable(x::StatFile) = true
IteratorInterfaceExtensions.isiterable(x::StatFile) = true
TableTraits.isiterabletable(x::StatFile) = true

function TableTraits.getiterator(file::StatFile)
function IteratorInterfaceExtensions.getiterator(file::StatFile)
filename, extension = splitext(file.filename)
if extension==".dta"
dt = read_dta(file.filename)
data, header = read_dta(file.filename)
elseif extension==".por"
dt = read_por(file.filename)
data, header = read_por(file.filename)
elseif extension==".sav"
dt = read_sav(file.filename)
data, header = read_sav(file.filename)
elseif extension==".sas7bdat"
dt = read_sas7bdat(file.filename)
data, header = read_sas7bdat(file.filename)
else
error("Unknown file type.")
end

it = getiterator(dt)
println(typeof(data[1]))

it = TableTraitsUtils.create_tableiterator(data, header)

return it
end
Expand Down
33 changes: 19 additions & 14 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
using FileIO
using StatFiles
using IteratorInterfaceExtensions
using TableTraits
using IterableTables
using DataFrames
using NamedTuples
using DataValues
using Base.Test

@testset "StatFiles" begin

df = load("types.dta") |> DataFrame
ar = load("types.dta") |> IteratorInterfaceExtensions.getiterator |> collect

@test size(df) == (3,6)
@test length(ar) == 3
@test ar[1] == @NT(vfloat=DataValue{Float32}(3.14), vdouble=DataValue(3.14), vlong=DataValue{Int32}(2), vint=DataValue{Int16}(2), vbyte=DataValue{Int8}(2), vstring=DataValue("2"))
@test ar[2] == @NT(vfloat=DataValue{Float32}(7.), vdouble=DataValue(7.), vlong=DataValue{Int32}(7), vint=DataValue{Int16}(7), vbyte=DataValue{Int8}(7), vstring=DataValue("7"))
@test ar[3] == @NT(vfloat=DataValue{Float32}(), vdouble=DataValue{Float64}(), vlong=DataValue{Int32}(), vint=DataValue{Int16}(), vbyte=DataValue{Int8}(), vstring=DataValue(""))

df = load("types.sas7bdat") |> DataFrame

@test size(df) == (3,6)
ar = load("types.sas7bdat") |> IteratorInterfaceExtensions.getiterator |> collect

df = load("types.sav") |> DataFrame
@test length(ar) == 3
@test ar[1] == @NT(vfloat=DataValue{Float32}(3.14), vdouble=DataValue(3.14), vlong=DataValue{Int32}(2), vint=DataValue{Int16}(2), vbyte=DataValue{Int8}(2), vstring=DataValue("2"))
@test ar[2] == @NT(vfloat=DataValue{Float32}(7.), vdouble=DataValue(7.), vlong=DataValue{Int32}(7), vint=DataValue{Int16}(7), vbyte=DataValue{Int8}(7), vstring=DataValue("7"))
@test ar[3] == @NT(vfloat=DataValue{Float32}(), vdouble=DataValue{Float64}(), vlong=DataValue{Int32}(), vint=DataValue{Int16}(), vbyte=DataValue{Int8}(), vstring=DataValue(""))

@test size(df) == (3,6)

# df = load("types.por") |> DataFrame
ar = load("types.sav") |> IteratorInterfaceExtensions.getiterator |> collect

# @test size(df) == (3,6)

statfile = load("types.dta")

@test isiterable(statfile) == true
@test length(ar) == 3
@test ar[1] == @NT(vfloat=DataValue{Float32}(3.14), vdouble=DataValue(3.14), vlong=DataValue{Int32}(2), vint=DataValue{Int16}(2), vbyte=DataValue{Int8}(2), vstring=DataValue("2"))
@test ar[2] == @NT(vfloat=DataValue{Float32}(7.), vdouble=DataValue(7.), vlong=DataValue{Int32}(7), vint=DataValue{Int16}(7), vbyte=DataValue{Int8}(7), vstring=DataValue("7"))
@test ar[3] == @NT(vfloat=DataValue{Float32}(), vdouble=DataValue{Float64}(), vlong=DataValue{Int32}(), vint=DataValue{Int16}(), vbyte=DataValue{Int8}(), vstring=DataValue(""))

end

0 comments on commit a171896

Please sign in to comment.