Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed Compat/Missings and bring code up to Julia 1.x #60

Merged
merged 1 commit into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ authors = ["Tom Kwong <tk3369@gmail.com>"]
version = "1.0.0"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
StringEncodings = "69024149-9ee7-55f6-a4c4-859efe599b68"
TabularDisplay = "3eeacb1d-13c2-54cc-9b18-30c86af3cadb"

[compat]
Compat = "3.2"
Missings = "0.4"
StringEncodings = "0.3"
TabularDisplay = "1"
julia = "1"

[extras]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Statistics", "InteractiveUtils", "Printf"]
test = ["Test", "Statistics", "InteractiveUtils", "Printf", "SharedArrays"]
6 changes: 3 additions & 3 deletions src/Metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ function colfmt(md::Metadata)
end

# Compact types format
# e.g. (Date, Missings.Missing) => "Date/Missings.Missing"
# e.g. (Date, Missing) => "Date/Missing"
function typesfmt(ty::Tuple; excludemissing = false)
ar = excludemissing ?
collect(Iterators.filter(x -> x != Missings.Missing, ty)) : [ty...]
collect(Iterators.filter(x -> x != Missing, ty)) : [ty...]
join(string.(ar), "/")
end

Expand All @@ -88,4 +88,4 @@ function typesof(ty::Type)
else
(ty,)
end
end
end
32 changes: 15 additions & 17 deletions src/SASLib.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
VERSION < v"0.7-" && __precompile__()

module SASLib

using StringEncodings, Missings, Compat.Dates, Compat.Distributed, Compat
using StringEncodings
using TabularDisplay
using Dates

export readsas, REGULAR_STR_ARRAY

Expand Down Expand Up @@ -66,8 +65,7 @@ read the entire file content. When called again, fetch the next `nrows` rows.
function read(handler::Handler, nrows=0)
# println("Reading $(handler.config.filename)")
elapsed = @elapsed result = read_chunk(handler, nrows)
# TODO base keyword arg should not be needed due to Compat.jl issue #567
elapsed = Compat.round(elapsed; digits = 5, base = 10)
elapsed = round(elapsed, digits = 5)
println1(handler, "Read $(handler.config.filename) with size $(size(result, 1)) x $(size(result, 2)) in $elapsed seconds")
return result
end
Expand Down Expand Up @@ -186,7 +184,7 @@ end
return handler.cached_page[offset+1:offset+len] #offset is 0-based
# => too conservative.... we expect cached_page to be filled before this function is called
# if handler.cached_page == []
# Compat.@warn("_read_byte function going to disk")
# @warn("_read_byte function going to disk")
# seek(handler.io, offset)
# try
# return Base.read(handler.io, len)
Expand Down Expand Up @@ -275,14 +273,14 @@ function read_header(handler)
else
handler.file_encoding = FALLBACK_ENCODING # hope for the best
handler.config.verbose_level > 0 &&
Compat.@warn("Unknown file encoding value ($buf), defaulting to $(handler.file_encoding)")
@warn("Unknown file encoding value ($buf), defaulting to $(handler.file_encoding)")
end
#println2(handler, "file_encoding = $(handler.file_encoding)")

# User override for encoding
if handler.config.encoding != ""
handler.config.verbose_level > 0 &&
Compat.@warn("Encoding has been overridden from $(handler.file_encoding) to $(handler.config.encoding)")
@warn("Encoding has been overridden from $(handler.file_encoding) to $(handler.config.encoding)")
handler.file_encoding = handler.config.encoding
end
# println2(handler, "Final encoding = $(handler.file_encoding)")
Expand Down Expand Up @@ -405,7 +403,7 @@ function check_user_column_types(handler)
# check column_types
for k in keys(handler.config.column_types)
if !case_insensitive_in(k, handler.column_symbols)
Compat.@warn("Unknown column symbol ($k) in column_types. Ignored.")
@warn("Unknown column symbol ($k) in column_types. Ignored.")
end
end
end
Expand Down Expand Up @@ -613,7 +611,7 @@ function _process_columnsize_subheader(handler, offset, length)
offset += int_len
handler.column_count = _read_int(handler, offset, int_len)
if (handler.col_count_p1 + handler.col_count_p2 != handler.column_count)
Compat.@warn("Warning: column count mismatch ($(handler.col_count_p1) + $(handler.col_count_p2) != $(handler.column_count))")
@warn("Warning: column count mismatch ($(handler.col_count_p1) + $(handler.col_count_p2) != $(handler.column_count))")
end
end

Expand Down Expand Up @@ -833,13 +831,13 @@ end
function read_chunk(handler, nrows=0)

if !isdefined(handler, :column_types)
Compat.@warn("No columns to parse from file")
@warn("No columns to parse from file")
return ResultSet()
end
# println("column_types = $(handler.column_types)")

if handler.row_count == 0
Compat.@warn("File has no data")
@warn("File has no data")
return ResultSet()
end

Expand Down Expand Up @@ -971,7 +969,7 @@ end

# convert Float64 value into Date object
function date_from_float(x::Vector{Float64})
@compat v = Vector{Union{Date, Missing}}(undef, length(x))
v = Vector{Union{Date, Missing}}(undef, length(x))
for i in 1:length(x)
v[i] = isnan(x[i]) ? missing : (sas_date_origin + Dates.Day(round(Int64, x[i])))
end
Expand All @@ -980,7 +978,7 @@ end

# convert Float64 value into DateTime object
function datetime_from_float(x::Vector{Float64})
@compat v = Vector{Union{DateTime, Missing}}(undef, length(x))
v = Vector{Union{DateTime, Missing}}(undef, length(x))
for i in 1:length(x)
v[i] = isnan(x[i]) ? missing : (sas_datetime_origin + Dates.Second(round(Int64, x[i])))
end
Expand Down Expand Up @@ -1040,7 +1038,7 @@ function convert_column_type_if_needed!(handler, rslt, name)
try
rslt[name] = convert(Vector{type_wanted}, rslt[name])
catch ex
Compat.@warn("Unable to convert column to type $type_wanted, error=$ex")
@warn("Unable to convert column to type $type_wanted, error=$ex")
end
end
end
Expand Down Expand Up @@ -1587,13 +1585,13 @@ function populate_column_indices(handler)
if inflag && length(processed) != length(handler.config.include_columns)
diff = setdiff(handler.config.include_columns, processed)
for c in diff
Compat.@warn("Unknown include column $c")
@warn("Unknown include column $c")
end
end
if exflag && length(processed) != length(handler.config.exclude_columns)
diff = setdiff(handler.config.exclude_columns, processed)
for c in diff
Compat.@warn("Unknown exclude column $c")
@warn("Unknown exclude column $c")
end
end
# println2(handler, "column_indices = $(handler.column_indices)")
Expand Down
2 changes: 1 addition & 1 deletion src/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ const VENDOR_SAS = 0x01
const FALLBACK_ENCODING = "UTF-8"
const ENCODINGS_OK_WITH_BASE_TRANSCODER = [ "UTF-8" , "US-ASCII" ]

@compat const REGULAR_STR_ARRAY(n) = Array{String}(undef, n)
const REGULAR_STR_ARRAY(n) = Array{String}(undef, n)
const EMPTY_STRING = ""

44 changes: 17 additions & 27 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using SASLib, Missings
using Compat.Test, Compat.Dates, Compat.Distributed, Compat.SharedArrays, Compat
using Test
using SASLib

@static if VERSION > v"0.7-"
import Statistics: mean
end
using Dates
using Statistics: mean
using SharedArrays: SharedArray

function getpath(dir, file)
path = joinpath(dir, file)
Expand All @@ -28,7 +28,7 @@ Base.convert(::Type{YearStr}, v::Float64) = YearStr(string(round(Int, v)))
x = SASLib.ObjectPool{String, UInt8}(default, 5)
@test length(x) == 5
@test size(x) == (5, )
@test @compat lastindex(x) == 5
@test lastindex(x) == 5
@test count(v -> v == default, x) == 5
@test count(v -> v === default, x) == 5
@test map(v -> "x$v", x) == [ "x", "x", "x", "x", "x" ]
Expand Down Expand Up @@ -169,15 +169,10 @@ Base.convert(::Type{YearStr}, v::Float64) = YearStr(string(round(Int, v)))

# test bad include/exclude param
# see https://discourse.julialang.org/t/test-warn-doesnt-work-with-warn-in-0-7/9001
@static if VERSION > v"0.7-"
Compat.Test.@test_logs (:warn, "Unknown include column blah") (:warn,
"Unknown include column Year") readsas(fname, include_columns=[:blah, :Year])
Compat.Test.@test_logs (:warn, "Unknown exclude column blah") (:warn,
"Unknown exclude column Year") readsas(fname, exclude_columns=[:blah, :Year])
else
@test_warn "Unknown include column" readsas(fname, include_columns=[:blah, :Year])
@test_warn "Unknown exclude column" readsas(fname, exclude_columns=[:blah, :Year])
end
@test_logs (:warn, "Unknown include column blah") (:warn,
"Unknown include column Year") readsas(fname, include_columns=[:blah, :Year])
@test_logs (:warn, "Unknown exclude column blah") (:warn,
"Unknown exclude column Year") readsas(fname, exclude_columns=[:blah, :Year])
# error handling
@test_throws SASLib.ConfigError readsas(fname,
include_columns=[1], exclude_columns=[1])
Expand Down Expand Up @@ -240,24 +235,19 @@ Base.convert(::Type{YearStr}, v::Float64) = YearStr(string(round(Int, v)))
@test show(md) == nothing
println()

# Deal with v0.6/v0.7 difference
# v0.6 shows Missings.Missing
# v0.7 shows Missing
ty(x) = replace(x, "Missings." => "")

# convenient comparison routine since v0.6/v0.7 displays different order
same(x,y) = sort(ty.(string.(collect(x)))) == sort(ty.(string.(collect(y))))
same(x,y) = sort(string.(collect(x))) == sort(string.(collect(y)))

@test same(SASLib.typesof(Int64), (Int64,))
@test same(SASLib.typesof(Union{Int64,Int32}), (Int64,Int32))
@test same(SASLib.typesof(Union{Int64,Int32,Missings.Missing}),
(Int64,Int32,Missings.Missing))
@test same(SASLib.typesof(Union{Int64,Int32,Missing}),
(Int64,Int32,Missing))

@test SASLib.typesfmt((Int64,)) == "Int64"
@test SASLib.typesfmt((Int64,Int32)) == "Int64/Int32"
@test ty(SASLib.typesfmt((Int64,Int32,Missings.Missing))) == "Int64/Int32/Missing"
@test SASLib.typesfmt((Int64,Int32,Missing)) == "Int64/Int32/Missing"
@test SASLib.typesfmt((Int64,Int32); excludemissing=true) == "Int64/Int32"
@test SASLib.typesfmt((Int64,Int32,Missings.Missing); excludemissing=true) == "Int64/Int32"
@test SASLib.typesfmt((Int64,Int32,Missing); excludemissing=true) == "Int64/Int32"
@test SASLib.colfmt(md)[1] == "ACTUAL(Float64)"
end

Expand Down Expand Up @@ -402,8 +392,8 @@ Base.convert(::Type{YearStr}, v::Float64) = YearStr(string(round(Int, v)))
end
end

@testset "exception" begin
@testset "exception" begin
@test_throws SASLib.FileFormatError readsas("runtests.jl")
end
end

end