Generate multiple temp name in once

This is a feature request. I’m not sure whether this request suit, but as for my personal needs, this method is convenient.

Expected:

julia> tempnames(3)
3-element Vector{String}:
 "C:\\Users\\Xlin0\\AppData\\Local\\Temp\\jl_PCTu9WLIx2"
 "C:\\Users\\Xlin0\\AppData\\Local\\Temp\\jl_zS70TUB0d6"
 "C:\\Users\\Xlin0\\AppData\\Local\\Temp\\jl_TalcLuHKLI"

Possible implementation:

function tempnames(amount::Int, parent::AbstractString=tempdir(); max_tries::Int=100, cleanup::Bool=true)
    amount > 0 || throw(ArgumentError("amount should be positive($(repr(amount)))"))
    isdir(parent) || throw(ArgumentError("$(repr(parent)) is not a directory"))

    prefix = joinpath(parent, temp_prefix)
    filenames = Vector{Union{String,Nothing}}(nothing, amount)

    for i in 1:amount
        tried_name = string(prefix, _rand_filename())
        if !ispath(tried_name)
            filenames[i] = tried_name
        end
    end

    for i in 1:max_tries
        inds = findall(isnothing.(filenames))
        if length(inds) > 0
            for j in inds
                tried_name = string(prefix, _rand_filename())
                if !ispath(tried_name)
                    filenames[j] = tried_name
                end
            end
        else
            break
        end
    end

    if in(x -> isnothing(x), filenames)
        error("filenames: max_tries exhausted")
    end

    filenames = Vector{String}(filenames)

    cleanup && temp_cleanup_later(filenames)
    return filenames
end

Thanks, @XLin0mu, for contributing to improve Julia. This is in general very welcomed.

As you already considered, this is the wrong place to contribute code. Code contribution happens at github. Either for Julia itself (to end in Base) or in a suitable package (I haven’t checked if there are any).

There you’d typically open an issue and describe your problem. This would include describing why for the occasional use [tempname() for _ in 1:3] is too inconvenient and why for a frequent use in a single code base defining it yourself with something like tempnames(n) = [tempname() for _ in 1:n] is not a good solution.

Then, after there is agreement that the problem should be fixed in the corresponding code base, you’d typically commit a PR (pull request) using git. There you’d argue why tempnames should be independent of tempname (if you want to propose exactly your current implementation) instead of a repeated call to tempname.

This is no strict “it always needs to happen exactly like this” thing, but I hope it provides you some guidance.

My personal guess (I am not a Julia maintainer): The bar to add tempnames to Base is probably too high. If you really need it frequently across several code bases, your best chance in my opinion is to find a suitable package or create a new package.

2 Likes

Thanks for advising, I’ll go there.

Just in case:
I created this topic after checking the contributing-guideline, where I found:

“Library feature requests are generally not accepted on this issue tracker. New libraries should be developed as packages. Discuss ideas for libraries at the Julia Discourse forum. Doing so will often lead to pointers to existing projects and bring together collaborators with common interests.”

I actually stopped filling an issue and turn to here because “Library feature” may include Standard Library(and Base) which I thought.

The “Library” there only refering thrid-part packages, is that understanding right?

“Library” or “library feature request” typically mean a bundle of types/macros/methods that belong together. You can look at some of the most starred packages to get a feeling for what is meant.
They are discussed in discourse, but typically not in the Internals & Design category.

Improvements like a single method or some code lines typically go to Github.

Questions about specific code parts of Julia would fit Internals & Design, but questions about packages should typically be in the General Usage category.

Again: No black and white, only general guidance.

Note that julia already provides tempname, which can be used for this purpose & is cross platform.

Also keep in mind that you may run into Time-of-check to time-of-use - Wikipedia if you don’t let the OS already create the files/dirs for you.

1 Like