What is in your startup.jl?

My startup.jl has become a place for tools that are too simple to be behind an import, and probably too opinionated for Base

"""
    @subprocess ex
    @subprocess ex wait=false

Execute `ex` in a subprocess that mimics the current process configuration.
Returns the constructed `Process`.
See [`Base.julia_cmd`](@ref) for the subprocess configuration forwarding that is used.

```julia-repl
julia> println("hello from $(Base.getpid())")
hello from 35640

julia> @subprocess println("hello from $(Base.getpid())")
hello from 43056
Process(`/home/user/julia/julia -Cnative -J/home/user/julia/usr/lib/julia/sys.dylib -g1 -e 'println("hello from $(Base.getpid())")'`, ProcessExited(0))
```
"""
macro subprocess(ex, wait=true)
    quote
        local ex_str = $(esc(sprint(Base.show_unquoted,ex)))
        run(`$(Base.julia_cmd()) -e "$(ex_str)"`, wait = $(wait))
    end
end
# See: https://github.com/IanButterworth/julia/tree/ib/subprocess_macro
"""
    @repeat call
    @repeat n call
    @repeat expr call

Repeat `call` until interrupt, or `n` times, and discard the output.
If an expression is given that returns an Integer, repeat that many times.
If an expression is given that returns a Bool, repeat until false.

```julia
julia> @repeat println("hello")
hello
hello
^ChelloERROR: InterruptException:
...

julia> @repeat 3 println("hello")
hello
hello
hello

julia> @repeat 2 + 1 println("hello")
hello
hello
hello

julia> @repeat rand() > 0.5 println("hello")
hello
hello
```
"""
macro repeat(exs...)
    if length(exs) == 1
        quote
            while true
                $(esc(first(exs)))
            end
        end
    elseif length(exs) == 2
        terms = first(exs)
        ex = last(exs)
        if terms <: Integer
            quote
                for _ = 1:$(esc(terms))
                    $(esc(ex))
                end
            end
        elseif terms isa Expr
            quote
                local terms_eval = $(esc(terms))
                if terms_eval isa Bool
                    if terms_eval
                        $(esc(ex)) # do once given that terms has been evaled once already
                        while $(esc(terms))
                            $(esc(ex))
                        end
                    end
                elseif terms_eval isa Integer
                    for _ in 1:terms_eval
                        $(esc(ex))
                    end
                else
                    throw(ArgumentError("@repeat first argument must return an Integer or a Bool"))
                end
            end
        else
            throw(ArgumentError("@repeat first argument must be an Integer literal, or an expression that returns an Integer or Bool"))
        end
    else
        throw(ArgumentError("Too many arguments passed to @repeat"))
    end
end
# See: https://github.com/JuliaLang/julia/pull/41455
8 Likes

@ianshmean, if you have a minute, could you please add a little preamble of explanation for non-experts.
Thank you!

Happy to. Do you mean about the macros themself, beyond the examples in the docstrings?
Or my rationale for not/abandoning PR-ing them to Base?

1 Like

Basically, kindly give some hints on: why a lambda user would like to add your code to his startup.jl? What does it accomplish?

Sorry if it is so obvious to you.

For @repeat I’ve found the occasional need to quickly run some functions n times, or until something is false, or timedout, particularly when developing hardware controls.

For instance

julia> @repeat 3 foo()

is a little simpler to write than

julia> [foo() for _ in 1:3];

More examples in https://github.com/JuliaLang/julia/pull/41455


For @subprocess if I wanted to do something in a clean julia session quickly I added that largely because I never set up julia symlinks to julia binaries, so never have a simple julia pointing to the right place.

So

julia> @subprocess println("hello from a new session")

is easier than

julia> run(`$(Base.julia_cmd()) -e "println(\"hello from a new session\")"`

or

shell> /long/path/to/julia -e "println(\"hello from a new session\")"

The first PR wasn’t very popular. The second doesn’t feel like it would be used widely enough for inclusion in Base (?), so I just kept them local

4 Likes

As seen on twitter, a few people have been adding Term.jl to their startup to style logging, errors and repr. Leaving this here in case anyone finds it useful.

try
    import Term: install_term_stacktrace, install_term_logger, install_term_repr
    install_term_stacktrace()
    install_term_logger()
    install_term_repr()
catch e
    @warn "Error initializing term" exception=(e, catch_backtrace())
end

Enjoy!

18 Likes

So many great little findings in this thread! :smiley:

I’ve also recently added a package template as per the docs of PkgTemplates.

# Package templates
function template()
    @eval begin
        using PkgTemplates
        Template(;
            user="my-username",
            dir="~/code",
            authors="Me",
            julia=v"1.7",
            plugins=[
                Git(; manifest=true),
                GitHubActions(),
                Codecov(),
                Documenter{GitHubActions}(),
                Citation(),
                RegisterAction(),
                BlueStyleBadge(),
                ColPracBadge(),
            ],
        )
    end
end

Then just proceed as always:

t = template()
t("MyPkg")

Works like a charm!

9 Likes

I’ve added this package, which makes my life a lot easier.

6 Likes

Since this thread seems to still be alive, I might as well toss what I’ve currently got going on.

import Pkg as var"#Pkg" # Don't import globally

isnothing(Base.find_package("Setup")) &&
    var"#Pkg".develop(path=joinpath(@__DIR__, "Setup"))

using Setup

@async let
    ESSENTIAL_PACKAGES =
        ["Revise", "DataFrames", "CSV", "BenchmarkTools"]
    EAGER_PACKAGES =
        ["DataFrames", "Statistics", "StatsBase", "BenchmarkTools"]

    for pkg in ESSENTIAL_PACKAGES
        if isnothing(Base.find_package(pkg))
            var"#Pkg".add(pkg)
        end
    end

    @eval using Revise

    for pkg in EAGER_PACKAGES
        if !isnothing(Base.find_package(pkg))
            Core.eval(Main, :(using $(Symbol(pkg))))
        end
    end

    Setup.termtitle(pwd())
end

where Setup is a whole bunch of setup bundled as a module.

.julia/config
β”œβ”€β”€ Setup/
β”‚  β”œβ”€β”€ ext/
β”‚  β”‚  β”œβ”€β”€ cairomakie.jl
β”‚  β”‚  β”œβ”€β”€ gadfly.jl
β”‚  β”‚  β”œβ”€β”€ hdf5.jl
β”‚  β”‚  β”œβ”€β”€ ohmyrepl.jl
β”‚  β”‚  └── prettycolors.jl
β”‚  β”œβ”€β”€ src/
β”‚  β”‚  β”œβ”€β”€ about.jl
β”‚  β”‚  β”œβ”€β”€ cmdpuns.jl
β”‚  β”‚  β”œβ”€β”€ colorfulmethods.jl
β”‚  β”‚  β”œβ”€β”€ pkgstack.jl
β”‚  β”‚  β”œβ”€β”€ Setup.jl
β”‚  β”‚  └── termsetup.jl
β”‚  β”œβ”€β”€ Manifest.toml
β”‚  └── Project.toml
β”œβ”€β”€ sysimages/
β”‚  └── <a collection of build scripts>
β”œβ”€β”€ Faces.toml
└── startup.jl

All in all, it’s ~900 lines of setup.

This increaces the Julia REPL startup time by ~30%, from 70ms to 90ms.

4 Likes

Do you have auto-updating system images in Setup/sysimages ?
Is it possible for you to give access to your Setup package code ? It looks inspiring !

I always use project specific environments. If there does not yet exist one, I create one. If there already exists one, I activate and instantiate it.

using Pkg
working_directory = dirname(@__FILE__)
cd(working_directory)

if Base.active_project() != dirname(@__FILE__) * "/Project.toml"
    pkg"activate ."
    pkg"instantiate"
end
1 Like

you might find @__DIR__ useful rather than dirname(@__FILE__)

2 Likes

Also, there’s joinpath which is platform agnostic.

2 Likes

Post-1.9 I don’t have any sysimages, instead I have an environment stack and auto-loading (e.g. starting with nothing loaded, running CSV.read("some/path", DataFrame) in the REPL will automatically load CSV and DataFrames before execution).

I might make my setup public at some point, once I’ve tinkered on it a bit more.

6 Likes
β–·(x, f) = Base.:|>(x, f)

Because I like the pipe operation, but lament the fact that it doesn’t have a pretty unicode operator. Can be typed at the REPL with \triangleright.

julia> cos(3) β–· sin
-0.8360218615377305
1 Like