From 6f47a0dc0abb760a048ec7497f5a988bb6f0f283 Mon Sep 17 00:00:00 2001 From: Tom Kwong Date: Wed, 1 Jan 2020 10:19:41 -0800 Subject: [PATCH] Removed Compat/Missings and bring code up to Julia 1.x --- Project.toml | 9 ++++----- src/Metadata.jl | 6 +++--- src/SASLib.jl | 32 +++++++++++++++----------------- src/constants.jl | 2 +- test/runtests.jl | 44 +++++++++++++++++--------------------------- 5 files changed, 40 insertions(+), 53 deletions(-) diff --git a/Project.toml b/Project.toml index cd611f0..defc788 100644 --- a/Project.toml +++ b/Project.toml @@ -4,14 +4,11 @@ authors = ["Tom Kwong "] 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" @@ -19,7 +16,9 @@ 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"] diff --git a/src/Metadata.jl b/src/Metadata.jl index e7a712c..44474ea 100644 --- a/src/Metadata.jl +++ b/src/Metadata.jl @@ -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 @@ -88,4 +88,4 @@ function typesof(ty::Type) else (ty,) end -end \ No newline at end of file +end diff --git a/src/SASLib.jl b/src/SASLib.jl index 1271a34..8dbb9ae 100644 --- a/src/SASLib.jl +++ b/src/SASLib.jl @@ -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 @@ -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 @@ -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) @@ -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)") @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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)") diff --git a/src/constants.jl b/src/constants.jl index 8a4bf90..9f001fe 100644 --- a/src/constants.jl +++ b/src/constants.jl @@ -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 = "" diff --git a/test/runtests.jl b/test/runtests.jl index f3d27bb..991ebb2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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" ] @@ -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]) @@ -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 @@ -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