Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed Nov 20, 2024
1 parent 483b7e7 commit 5f85936
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Exts"
uuid = "0b12d779-4123-4875-9d6c-e33c2e29e2c9"
authors = ["Heptazhou <zhou at 0h7z dot com>"]
version = "0.2.2"
version = "0.2.3"

[deps]
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

*****
## Usage
```julia
```julia-repl
pkg> registry add https://github.com/0h7z/0hjl.git
pkg> add Exts
```

```julia
julia> using Exts

julia> ODict(Exts.ext(:)) # to see which extensions are loaded
OrderedDict{Symbol, Union{Nothing, Module}} with 4 entries:
:BaseExt => Exts.BaseExt
:DataFramesExt => DataFramesExt
:FITSIOExt => FITSIOExt
:StatisticsExt => StatisticsExt
```

11 changes: 10 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

*****
## Usage
```julia
```julia-repl
pkg> registry add https://github.com/0h7z/0hjl.git
pkg> add Exts
```

```jldoctest
julia> using Exts
julia> ODict(Exts.ext(:)) # to see which extensions are loaded
OrderedDict{Symbol, Union{Nothing, Module}} with 4 entries:
:BaseExt => Exts.BaseExt
:DataFramesExt => DataFramesExt
:FITSIOExt => FITSIOExt
:StatisticsExt => StatisticsExt
```

*****
Expand Down
2 changes: 1 addition & 1 deletion ext/FITSIOExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ using DataFrames: DataFrame
using Exts: SymOrStr, ensure_vector, lmap
using FITSIO: FITSIO, FITS, EitherTableHDU

@doc " FITSIO.EitherTableHDU = Union{ASCIITableHDU, TableHDU}" EitherTableHDU
@doc " FITSIO.EitherTableHDU <- Union{ASCIITableHDU, TableHDU}" EitherTableHDU

"""
read(t::FITSIO.EitherTableHDU, DataFrame,
Expand Down
30 changes: 30 additions & 0 deletions src/BaseExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
"""
module BaseExt

using Base: Bottom, Fix1, Fix2
using Core: TypeofBottom

@doc """
Bottom <- Union{}
See [`Union{}`](@extref).
""" Bottom
for T in (:Fix1, :Fix2)
#! format: noindent
@eval @doc """
$($T)(f, x) -> Function
See [`Base.$($T)`](@extref).
""" $T
end

function Base.adjoint(m::T) where T <: AbstractVecOrMat{Any}
permutedims(m)::AbstractMatrix{Any}
end
Expand All @@ -41,6 +58,19 @@ function Base.convert(::Type{S}, v::AbstractVector) where S <: AbstractSet{T} wh
S(T[v;])
end

@inline Base.iterate(::Type, ::Type{T}) where T = T, Bottom
@inline Base.iterate(::Type, ::TypeofBottom) = nothing
@inline Base.iterate(::TypeofBottom) = nothing
@inline Base.iterate(::Union, T::Union) = T.a, T.b
@inline Base.iterate(T::DataType, i::Int = 1) = iterate(T.parameters, i)
@inline Base.iterate(T::Union) = iterate(T, T)
@inline Base.iterate(T::UnionAll) = begin
U = Union{T, Bottom}
U isa UnionAll ? nothing : iterate(U::Union{DataType, Union})
end

@inline Base.length(T::DataType) = length(T.parameters)

"""
log10(x::T, σ::T) -> NTuple{2, AbstractFloat} where T <: Real
Expand Down
7 changes: 4 additions & 3 deletions src/Exts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export UDict
export USet
export VecOrTup
export VTuple
export VTuple1
export VType

export @catch
export @noinfo
Expand Down Expand Up @@ -56,12 +56,14 @@ using Reexport: @reexport

@reexport begin
#! format: noindent
using Base: Fix1, Fix2
using Base: Bottom, Fix1, Fix2
using Base: nonnothingtype, notnothing, return_types
using Base.Threads: @spawn, @threads, nthreads
using Core: TypeofBottom, TypeofVararg
using OrderedCollections: LittleDict, OrderedDict, OrderedSet, freeze
end

include("BaseExt.jl")
include("Macro.jl")
include("Type.jl")

Expand Down Expand Up @@ -103,7 +105,6 @@ function pause(i::IO, o::IO, msg::Maybe{AbstractString} = nothing)::Nothing
print(o, '\n')
end

include("BaseExt.jl")
include("Function.jl")

# StatisticsExt
Expand Down
40 changes: 26 additions & 14 deletions src/Type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,54 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

for N 0:3
VTupleN = Symbol(:VTuple, N)
@eval const $VTupleN{T} = Tuple{NTuple{$N, T}..., Vararg{T}}
@eval export $VTupleN
end

const IntOrStr = Union{AbstractString, Integer}
const SymOrStr = Union{AbstractString, Symbol}
const VecOrTup = Union{AbstractVector, Tuple}
const VType = Union{Type, TypeofVararg}

const Datum{T} = Union{T, Missing}
const Maybe{T} = Union{T, Nothing}
const VTuple{T} = Tuple{Vararg{T}}
const VTuple1{T} = Tuple{T, Vararg{T}}
const VTuple{T} = VTuple0{T}

const datum(T::Type...) = Datum{Union{T...}}
const maybe(T::Type...) = Maybe{Union{T...}}

@doc " IntOrStr -> Union{AbstractString, Integer}" IntOrStr
@doc " SymOrStr -> Union{AbstractString, Symbol}" SymOrStr
@doc " VecOrTup -> Union{AbstractVector, Tuple}" VecOrTup
@doc " VTuple{T} -> Tuple{Vararg{T}}" VTuple
@doc " VTuple1{T} -> Tuple{T, Vararg{T}}" VTuple1
@doc " IntOrStr <- Union{AbstractString, Integer}" IntOrStr
@doc " SymOrStr <- Union{AbstractString, Symbol}" SymOrStr
@doc " VecOrTup <- Union{AbstractVector, Tuple}" VecOrTup
@doc " VType <- Union{Type, TypeofVararg}" VType

@doc """
Datum{T} -> Union{T, Missing}
Datum{T} <- Union{T, Missing}
datum(T::Type...) -> Datum{Union{T...}}
""" Datum, datum
@doc """
Maybe{T} -> Union{T, Nothing}
Maybe{T} <- Union{T, Nothing}
maybe(T::Type...) -> Maybe{Union{T...}}
""" Maybe, maybe
@doc """
VTuple{T} <- VTuple0{T}
VTuple0{T} <- Tuple{Vararg{T}} # 0 or more
VTuple1{T} <- Tuple{T, Vararg{T}} # 1 or more
VTuple2{T} <- Tuple{T, T, Vararg{T}} # 2 or more
VTuple3{T} <- Tuple{T, T, T, Vararg{T}} # 3 or more
""" VTuple

const LDict = LittleDict
const ODict = OrderedDict
const OSet = OrderedSet
const UDict = Dict
const USet = Set

@doc " LDict -> LittleDict" LDict
@doc " ODict -> OrderedDict" ODict
@doc " OSet -> OrderedSet" OSet
@doc " UDict -> Dict # UnorderedDict" UDict
@doc " USet -> Set # UnorderedSet" USet
@doc " LDict <- LittleDict" LDict
@doc " ODict <- OrderedDict" ODict
@doc " OSet <- OrderedSet" OSet
@doc " UDict <- Dict # UnorderedDict" UDict
@doc " USet <- Set # UnorderedSet" USet

33 changes: 33 additions & 0 deletions test/docs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (C) 2022-2024 Heptazhou <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

using Exts
using Pkg: Pkg

cd(@__DIR__) do
cp("Project.toml", "../docs/Project.toml", force = true)
fs = [
"../README.md"
"../docs/src/api.md"
]
md = join(readstr.(fs), "*"^5 * "\n")
md = replace(md, r"^#+\K\s+"m => " ")
md = replace(md, r"^```\Kjulia$"m => "jldoctest")
write("../docs/src/index.md", md)

Pkg.activate("../docs")
Pkg.update()
include("../docs/make.jl")
end

72 changes: 57 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

@static if ("docs") ARGS
include("docs.jl")
exit()
end

using InteractiveUtils: subtypes
using Test

Expand All @@ -34,6 +39,22 @@ end

@test 24 <= length(v)
@test all(@. float(v) <: AbstractFloat)
@test fieldnames(DataType) [:parameters]
@test fieldnames(Union) [:a, :b]
@test fieldnames(UnionAll) [:body, :var]
@test subtypes(Type) [Core.TypeofBottom, DataType, Union, UnionAll]

@test Base.VecOrMat isa UnionAll
@test Base.VecOrMat{Any} isa Union
@test Base.VecOrMat{Any}.a isa DataType
@test Base.VecOrMat{Any}.b isa DataType
@test Core.BuiltinInts isa Union
@test Core.BuiltinInts.a isa DataType
@test Core.BuiltinInts.b isa Union
@test Vector isa UnionAll
@test Vector.body isa DataType
@test Vector.body.parameters isa Core.SimpleVector
@test Vector{Any} isa DataType
end

@testset "Base" begin
Expand Down Expand Up @@ -81,7 +102,11 @@ end
@test_throws UndefVarError USet
@test_throws UndefVarError VecOrTup
@test_throws UndefVarError VTuple
@test_throws UndefVarError VTuple0
@test_throws UndefVarError VTuple1
@test_throws UndefVarError VTuple2
@test_throws UndefVarError VTuple3
@test_throws UndefVarError VType

# Function
@test_throws LoadError @eval @catch
Expand Down Expand Up @@ -109,6 +134,7 @@ end
# Reexport
@test_throws LoadError @eval @spawn
@test_throws LoadError @eval @threads
@test_throws UndefVarError Bottom
@test_throws UndefVarError Fix1
@test_throws UndefVarError Fix2
@test_throws UndefVarError freeze
Expand All @@ -119,11 +145,20 @@ end
@test_throws UndefVarError OrderedDict
@test_throws UndefVarError OrderedSet
@test_throws UndefVarError return_types
@test_throws UndefVarError TypeofBottom
@test_throws UndefVarError TypeofVararg

@test_throws MethodError (>, <)(0)
@test_throws MethodError (sin, cos)(0)
@test_throws MethodError [Base.VecOrMat...]
@test_throws MethodError [Core.BuiltinInts...]
@test_throws MethodError [NTuple{3, Int}...]
@test_throws MethodError [Tuple...]
@test_throws MethodError [Union...]
@test_throws MethodError [Union{}...]
@test_throws MethodError collect(isodd, 1:3)
@test_throws MethodError collect(isodd, i for i 1:3)
@test_throws MethodError collect(Tuple{})
@test_throws MethodError convert(Set, 1:3)
@test_throws MethodError convert(Set{Int}, 1:3)
@test_throws MethodError log10(11, 2)
Expand All @@ -133,15 +168,32 @@ end
a3_nothing = Array{Nothing, 3}(undef, Tuple(rand(0:9, 3)))
using Exts

@test (>, <)(0) === (>(0), <(0))
@test (>, <)(0) isa NTuple{2, Fix2{<:Function, Int}}
@test (sin, cos)(0) === sincos(0)
@test [:_ -1] == [:_, -1]'
@test [:p :q] == [:p, :q]'
@test ['1' '2'] == ['1', '2']'
@test ["x" "y"] == ["x", "y"]'
@test [AbstractMatrix...] == [Vector...] == []
@test [AbstractMatrix{UInt8}...] == [UInt8, 2]
@test [AbstractVecOrMat...] == [AbstractVector, AbstractMatrix]
@test [Core.BuiltinInts...] [Bool, Int8, Int16, Int32, Int64, Int128]
@test [NTuple{3, Int}...] == fill(Int, 3)
@test [Tuple...] == [Vararg{Any}]
@test [Tuple{}...] == [NTuple...] == []
@test [Tuple{Float16, Float32, Float64}...] == [Float16, Float32, Float64]
@test [Union{}...] == [Union...] == []
@test [Vector{Union{Unsigned, Signed}}...] == [Union{Unsigned, Signed}, 1]
@test [VTuple1{Function}...] == [Function, Vararg{Function}]
@test all(@. only(ntuple(1, Val(0:9))) === Val(0:9))
@test cd(() -> stdpath("../test"), @__DIR__) == "./"
@test chomp(readstr(@__FILE__)) == readchomp(@__FILE__)
@test allequal(length.([NTuple{0, Type}, collect(Tuple{}), Tuple{}]))
@test allequal(length.([NTuple{1, Type}, VTuple0{Type}, VTuple{Type}, Tuple{Type}]))
@test allequal(length.([NTuple{2, Type}, VTuple1{Type}]))
@test allequal(length.([NTuple{3, Type}, VTuple2{Type}]))
@test allequal(length.([NTuple{4, Type}, VTuple3{Type}]))
@test cd(() -> stdpath("../test"), @__DIR__) === "./"
@test chomp(readstr(@__FILE__)) === readchomp(@__FILE__)
@test collect(isodd, i for i 1:3) == collect(isodd, 1:3) == [1, 3]
@test convert(Set{Int}, 1:3) == convert(Set, 1:3) == Set(1:3)
@test copy.(ensure_vector(a2_missing)) isa Vector{Vector{Missing}}
Expand Down Expand Up @@ -183,6 +235,7 @@ end
@test stdpath("..") == "../"
@test stdpath(".") == "./"
@test stdpath("") == ""
@test Tuple === Tuple{Vararg{Any}} === VTuple{Any}
@test_throws ArgumentError notmissing(missing)
@test_throws ArgumentError notnothing(nothing)

Expand Down Expand Up @@ -229,7 +282,7 @@ end
@test df == read(tmp, DataFrame)
@test df == read(tmp, DataFrame, [:x, :y], skipstart = 1)
@test df == CSV.read(tmp, DataFrame)
@test dropmissing(DataFrame(x = [1, missing])) == DataFrame(x = [1])
@test dropmissing(DataFrame(x = [missing, 1])) == DataFrame(x = [1])
end

@testset "FITSIOExt" begin
Expand All @@ -256,16 +309,5 @@ end
end

@test all(nameof.(last.(Exts.ext(:))) .== first.(Exts.ext(:)))
@static parse(Bool, get(ENV, "CI", "0")) || cd(@__DIR__) do
cp("./Project.toml", "../docs/Project.toml", force = true)
fs = [
"../README.md"
"../docs/src/api.md"
]
md = join(readstr.(fs), "*"^5 * "\n")
md = replace(md, r"^#+\K\s+"m => " ")
write("../docs/src/index.md", md)

include("../docs/make.jl")
end
@static parse(Bool, get(ENV, "CI", "0")) || include("docs.jl")

0 comments on commit 5f85936

Please sign in to comment.