Skip to content

Commit 0b54af0

Browse files
Use pkgextensions in Julia v1.9 (#143)
* start work on 1.9 extensions * move Juno into an extension * move messages to the top of the file, fix SpecialFunctions * Update src/Measurements.jl Co-authored-by: Mosè Giordano <[email protected]> * Update ext/MeasurementsJunoExt.jl Co-authored-by: Mosè Giordano <[email protected]> * Update src/show.jl Co-authored-by: Mosè Giordano <[email protected]> * Update ext/MeasurementsJunoExt.jl Co-authored-by: Mosè Giordano <[email protected]> * Update ext/MeasurementsSpecialFunctionsExt.jl Co-authored-by: Mosè Giordano <[email protected]> * `@static` if !isdefined is required in the requires block * use `result` in MeasurementsSpecialFunctionsExt, remove an extra space * use value and uncertainty in MeasurementsUntifulExt.jl * move around the @static declaration in init, remove from unneeded places * Update src/Measurements.jl Co-authored-by: Mosè Giordano <[email protected]> * reactivate Aqua tests * use 1 static ifdefined statement * use 1 static ifdefined statement * add RecipesBase as an extension * typo * try again to move all cond code into 1 @static ifdefined * remove unused files, move require block at the end * trailing end space * move RecipesBase out of the condition * typo in Project.toml * add RecipesBase to ignored stale deps * typo in Aqua kwarg * change order to the one Aqua.jl likes * change order of extensions (Aqua.jl) * typo in Aqua.test_stale_deps * add Requires to stale_deps * Make PlotRecipes work * use 1 if isdefined statement (take 3) * revert using 1 statement. julia 1.0 does not like that * Update test/runtests.jl Co-authored-by: Mosè Giordano <[email protected]> * Update MeasurementsRecipesBaseExt.jl --------- Co-authored-by: Mosè Giordano <[email protected]>
1 parent f1d6e89 commit 0b54af0

8 files changed

+118
-33
lines changed

Project.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,27 @@ RecipesBase = "0.6.0, 0.7, 0.8, 1.0"
1717
Requires = "0.5.0, 1"
1818
julia = "1"
1919

20+
[extensions]
21+
MeasurementsJunoExt = "Juno"
22+
MeasurementsRecipesBaseExt = "RecipesBase"
23+
MeasurementsSpecialFunctionsExt = "SpecialFunctions"
24+
MeasurementsUnitfulExt = "Unitful"
25+
2026
[extras]
2127
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
28+
Juno = "e5e0dc1b-0480-54bc-9374-aad01c23163d"
2229
QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
30+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
2331
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
2432
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2533
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2634
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
2735

2836
[targets]
29-
test = ["Aqua", "QuadGK", "SpecialFunctions", "Statistics", "Test", "Unitful"]
37+
test = ["Aqua", "QuadGK", "RecipesBase", "SpecialFunctions", "Statistics", "Test", "Unitful"]
38+
39+
[weakdeps]
40+
Juno = "e5e0dc1b-0480-54bc-9374-aad01c23163d"
41+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
42+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
43+
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

ext/MeasurementsJunoExt.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
### special-functions.jl
2+
#
3+
# Copyright (C) 2019 Mosè Giordano.
4+
#
5+
# Maintainer: Mosè Giordano <mose AT gnu DOT org>
6+
# Keywords: uncertainty, error propagation, physics
7+
#
8+
# This file is a part of Measurements.jl.
9+
#
10+
# License is MIT "Expat".
11+
#
12+
### Commentary:
13+
#
14+
# This file contains methods to print Measurement objects within a Juno enviroment
15+
#
16+
### Code:
17+
18+
### Special functions
19+
module MeasurementsJunoExt
20+
21+
if isdefined(Base, :get_extension)
22+
using Measurements
23+
using Juno
24+
else
25+
using ..Measurements
26+
using ..Juno
27+
end
28+
29+
Juno.render(i::Juno.Inline, measure::Measurement) =
30+
Juno.render(i, Juno.Row(measure.val, Text(" ± "), measure.err))
31+
32+
Juno.Row(measure.val, Text(" ± "), measure.err)
33+
34+
function Juno.render(ji::Juno.Inline, cm::Complex{<:Measurement})
35+
r, i = reim(cm)
36+
if signbit(i) && !isnan(i)
37+
i = -i
38+
sss = " - "
39+
else
40+
sss = " + "
41+
end
42+
Juno.render(ji, Juno.Row("(", Juno.render(ji, r), ")", sss,
43+
"(", Juno.render(ji, i), ")im"))
44+
end
45+
46+
end

src/plot-recipes.jl renamed to ext/MeasurementsRecipesBaseExt.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
# This file defines the recipes to plot Measurements vectors with Plots.jl package in 2D.
1515
#
1616
### Code:
17+
module MeasurementsRecipesBaseExt
1718

18-
using RecipesBase
19+
if isdefined(Base, :get_extension)
20+
using Measurements: Measurement, value, uncertainty
21+
using RecipesBase
22+
else
23+
using ..Measurements: Measurement, value, uncertainty
24+
using ..RecipesBase
25+
end
1926

2027
@recipe function f(y::AbstractArray{<:Measurement})
2128
yerror := uncertainty.(y)
@@ -44,3 +51,5 @@ end
4451
yerror := uncertainty.(y)
4552
x, value.(y)
4653
end
54+
55+
end

src/special-functions.jl renamed to ext/MeasurementsSpecialFunctionsExt.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717
### Code:
1818

1919
### Special functions
20-
using .SpecialFunctions
20+
module MeasurementsSpecialFunctionsExt
2121

22+
if isdefined(Base, :get_extension)
23+
using Measurements
24+
using Measurements: result
25+
using SpecialFunctions
26+
else
27+
using ..Measurements
28+
using ..Measurements: result
29+
using ..SpecialFunctions
30+
end
2231
# Error function: erf, erfinv, erfc, erfcinv, erfcx, erfi, dawson
2332

2433
function SpecialFunctions.erf(a::Measurement{T}) where {T<:AbstractFloat}
@@ -230,3 +239,4 @@ function Base.factorial(a::Measurement)
230239
:factorial)
231240
SpecialFunctions.gamma(a + one(a))
232241
end
242+
end

src/unitful.jl renamed to ext/MeasurementsUnitfulExt.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,19 @@
1515
#
1616
### Code:
1717

18-
import .Unitful: AbstractQuantity, unit, ustrip
18+
module MeasurementsUnitfulExt
19+
20+
if isdefined(Base, :get_extension)
21+
using Measurements
22+
using Measurements: value, uncertainty
23+
import Unitful: AbstractQuantity, unit, ustrip
24+
else
25+
using ..Measurements
26+
using ..Measurements: value, uncertainty
27+
import ..Unitful: AbstractQuantity, unit, ustrip
28+
end
29+
30+
1931
function Measurements.measurement(a::T, b::T) where {T<:AbstractQuantity}
2032
u = unit(a)
2133
return measurement(ustrip(u, a), ustrip(u, b)) * u
@@ -33,3 +45,5 @@ function Measurements.uncertainty(x::AbstractQuantity)
3345
u = unit(x)
3446
return uncertainty(ustrip(u, x)) * u
3547
end
48+
49+
end

src/Measurements.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ using Statistics
2727

2828
import Statistics: cor, cov
2929

30-
using Requires
31-
3230
# Functions provided by this package and exposed to users
3331
export Measurement, measurement, ±
3432
# Re-export from Statistics
@@ -85,11 +83,6 @@ end
8583
# Start from 1, 0 is reserved to derived quantities
8684
const tag_counter = Threads.Atomic{UInt64}(1)
8785

88-
function __init__()
89-
@require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("unitful.jl")
90-
@require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("special-functions.jl")
91-
end
92-
9386
measurement(x::Measurement) = x
9487
measurement(val::T) where {T<:AbstractFloat} = Measurement(val, zero(T), UInt64(0), empty_der2(val))
9588
measurement(val::Real) = measurement(float(val))
@@ -128,6 +121,19 @@ include("math.jl")
128121
include("linear_algebra.jl")
129122
include("show.jl")
130123
include("parsing.jl")
131-
include("plot-recipes.jl")
124+
125+
if !isdefined(Base,:get_extension)
126+
using Requires
127+
using RecipesBase
128+
include("../ext/MeasurementsRecipesBaseExt.jl")
129+
end
130+
131+
@static if !isdefined(Base, :get_extension)
132+
function __init__()
133+
@require Unitful="1986cc42-f94f-5a68-af5c-568840ba703d" include("../ext/MeasurementsUnitfulExt.jl")
134+
@require SpecialFunctions="276daf66-3868-5448-9aa4-cd146d93841b" include("../ext/MeasurementsSpecialFunctionsExt.jl")
135+
@require Juno="e5e0dc1b-0480-54bc-9374-aad01c23163d" include("../ext/MeasurementsJunoExt.jl")
136+
end
137+
end
132138

133139
end # module

src/show.jl

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,3 @@ end
9393
if VERSION >= v"1.6.0-rc1"
9494
Printf.tofloat(measure::Measurement) = Printf.tofloat(measure.val)
9595
end
96-
97-
### Juno pretty printing
98-
@require Juno="e5e0dc1b-0480-54bc-9374-aad01c23163d" begin
99-
Juno.render(i::Juno.Inline, measure::Measurement) =
100-
Juno.render(i, Juno.Row(measure.val, Text(" ± "), measure.err))
101-
102-
Juno.Row(measure.val, Text(" ± "), measure.err)
103-
104-
function Juno.render(ji::Juno.Inline, cm::Complex{<:Measurement})
105-
r, i = reim(cm)
106-
if signbit(i) && !isnan(i)
107-
i = -i
108-
sss = " - "
109-
else
110-
sss = " + "
111-
end
112-
Juno.render(ji, Juno.Row("(", Juno.render(ji, r), ")", sss,
113-
"(", Juno.render(ji, i), ")im"))
114-
end
115-
end

test/runtests.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
using Measurements, SpecialFunctions, QuadGK, Calculus
22
using Test, LinearAlgebra, Statistics, Unitful, Printf, Aqua
3-
Aqua.test_all(Measurements)
3+
4+
if !isdefined(Base,:get_extension)
5+
Aqua.test_all(Measurements)
6+
else
7+
Aqua.test_all(Measurements; stale_deps=false)
8+
Aqua.test_stale_deps(Measurements; ignore=[:RecipesBase, :Requires])
9+
end
410

511
import Base: isapprox
612
import Measurements: value, uncertainty

0 commit comments

Comments
 (0)