Pkg.status() is well-known. However, it outputs to stdout. I need the package names in an iterable, like a list or a vector or so. It would be silly to use Suppressor. How can this be achieved?
Based on the answer below:
for v in values(Pkg.dependencies())
println(v.name)
end
What you are looking for is dependencies()
julia> Pkg.dependencies()
Dict{Base.UUID, Pkg.API.PackageInfo} with 399 entries:
UUID("49dc2e85-a5d0-5ad3-a950-438e2897f1b9") => PackageInfo("Calculus", v"0.5.1", "f641eb0a4f00c343bbc32346e1217b86f3ce9da…
UUID("efcefdf7-47ab-520b-bdef-62a2eaa19f15") => PackageInfo("PCRE2_jll", v"10.40.0+0", nothing, false, false, false, false…
...
This returns an iterator of pars. The value element of the pair contains a PackageInfo element that has the following fields:
julia> fieldnames(Pkg.API.PackageInfo)
(:name, :version, :tree_hash, :is_direct_dep, :is_pinned, :is_tracking_path, :is_tracking_repo, :is_tracking_registry, :git_revision, :git_source, :source, :dependencies)
And here is a sample usage:
julia> for (uuid, dep) in Pkg.dependencies()
dep.is_direct_dep || continue
dep.version === nothing && continue
println("$(dep.name) $(dep.version)")
end
ZipFile 0.10.0
DataFrames 1.4.1
Revise 3.4.0
Symbolics 4.11.1
BenchmarkTools 1.3.1
IJulia 1.23.3
...
Related
Wondering why can't use "in" when using "subset". "filter" could use it.
using DataFrames
df=DataFrame( a=["cat","dog","tiger","parrot","cat","parrot"],
r=[2,5,1,3,6,3])
subset(df, :a => a->a in["cat", "parrot"], :r => r->r.>1) ##get an error
#but
subset(df, :a => ByRow(a->a in ["cat", "parrot"]), :r => r->r.>1)
#or
filter([:a,:r]=>(a,r) -> a in ["cat", "parrot"] && r.>1, df)
#Thanks.
Another simple answer using InMemoryDatasets package
using InMemoryDatasets
ds=Dataset(df)
filter(ds, [:a, :r], by=[in(["cat", "parrot"]), >(1)])
The documentation reads:
help?> subset
... All transformations must produce vectors containing true or false.
Hence you can do
julia> subset(df, :a => a -> in.(a, Ref(["cat", "parrot"])), :r => r -> r .> 1 )
or
julia> subset(df, [:a, :r] => (a,r) -> (in.(a, Ref(["cat", "parrot"]))) .& (r .> 1) )
Note that if the list is long you might want to wrap ["cat", "parrot"] to a set.
I already have output some content in the REPL. Is there any function to print all this content to a file?
If those outputs have already been printed in the REPL, I guess the only way to save them to a file is copy-pasting manually. But if you would like to save the REPL output history for future use, one way is to overload display:
shell> touch repl_history.txt
julia> using REPL
julia> function REPL.display(d::REPL.REPLDisplay, mime::MIME"text/plain", x)
io = REPL.outstream(d.repl)
get(io, :color, false) && write(io, REPL.answer_color(d.repl))
if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext)
# this can override the :limit property set initially
io = foldl(IOContext, d.repl.options.iocontext,
init=IOContext(io, :limit => true, :module => Main))
end
show(io, mime, x)
println(io)
open("repl_history.txt", "a") do f
show(f, mime, x)
println(f)
end
nothing
end
then, let's print something random in the REPL:
julia> rand(10)
10-element Array{Float64,1}:
0.37537591915616497
0.9478991508737484
0.32628512501942475
0.8888960925262224
0.9967927432272801
0.4910769590205608
0.7624517049991089
0.26310423494973545
0.5117608425961135
0.0762255311602309
help?> gcd
search: gcd gcdx significand
gcd(x,y)
Greatest common (positive) divisor (or zero if x and y are both zero).
Examples
≡≡≡≡≡≡≡≡≡≡
julia> gcd(6,9)
3
julia> gcd(6,-9)
3
And here is what the file content looks like:
shell> cat repl_history.txt
10-element Array{Float64,1}:
0.37537591915616497
0.9478991508737484
0.32628512501942475
0.8888960925262224
0.9967927432272801
0.4910769590205608
0.7624517049991089
0.26310423494973545
0.5117608425961135
0.0762255311602309
gcd(x,y)
Greatest common (positive) divisor (or zero if x and y are both zero).
Examples
≡≡≡≡≡≡≡≡≡≡
julia> gcd(6,9)
3
julia> gcd(6,-9)
3
If there is no need to work with REPL interactively, simply use julia script.jl > output.txt might also do the trick.
What you've printed isn't saved anywhere, so no, there's no way to do that. It may be that there's something easy to do but without more details it's impossible to way really.
If you want to save variables to file, you can you JLD2 package. Then you can save each variable as below:
using JLD2, FileIO
hello = "world"
foo = :bar
#save "example.jld2" hello foo
When I have many elements in an array, Julia REPL only shows some part of it. For example:
julia> x = rand(100,2);
julia> x
100×2 Array{Float64,2}:
0.277023 0.0826133
0.186201 0.76946
0.534247 0.777725
0.942698 0.0239694
0.498693 0.0285596
⋮
0.383858 0.959607
0.987775 0.20382
0.319679 0.69348
0.491127 0.976363
Is there any way to show all elements in the vertical form as above? print(x) or showall(x) put it in an ugly form without line changes.
NOTE: in 0.7, Base.STDOUT has been renamed to Base.stdout. The rest should work unchanged.
---
There are a lot of internally used methods in base/arrayshow.jl doing stuff related to this. I found
Base.print_matrix(STDOUT, x)
to work. The limiting behaviour can be restored by using an IOContext:
Base.print_matrix(IOContext(STDOUT, :limit => true), x)
However, this method only prints the values, not the header information containing the type. But we can retrieve that header using summary (which I found out looking at this).
Combining both:
function myshowall(io, x, limit = false)
println(io, summary(x), ":")
Base.print_matrix(IOContext(io, :limit => limit), x)
end
Example:
julia> myshowall(STDOUT, x[1:30, :], true)
30×2 Array{Float64,2}:
0.21730681784436 0.5737060668051441
0.6266216317547848 0.47625168078991886
0.9726153326748859 0.8015583406422266
0.2025063774372835 0.8980835847636988
0.5915731785584124 0.14211295083173403
0.8697483851126573 0.10711267862191032
0.2806684748462547 0.1663862576894135
0.87125664767098 0.1927759597335088
0.8106696671235174 0.8771542319415393
0.14276026457365587 0.23869679483621642
0.987513511756988 0.38605250840302996
⋮
0.9587892008777128 0.9823155299532416
0.893979917305394 0.40184945077330836
0.6248799650411605 0.5002213828574473
0.13922016844193186 0.2697416140839628
0.9614124092388507 0.2506075363030087
0.8403420376444073 0.6834231190218074
0.9141176587557365 0.4300133583400858
0.3728064777779758 0.17772360447862634
0.47579213503909745 0.46906998919124576
0.2576800028360562 0.9045669936804894
julia> myshowall(STDOUT, x[1:30, :], false)
30×2 Array{Float64,2}:
0.21730681784436 0.5737060668051441
0.6266216317547848 0.47625168078991886
0.9726153326748859 0.8015583406422266
0.2025063774372835 0.8980835847636988
0.5915731785584124 0.14211295083173403
0.8697483851126573 0.10711267862191032
0.2806684748462547 0.1663862576894135
0.87125664767098 0.1927759597335088
0.8106696671235174 0.8771542319415393
0.14276026457365587 0.23869679483621642
0.987513511756988 0.38605250840302996
0.8230271471019499 0.37242899586931943
0.9138200958138099 0.8068913133278408
0.8525161103718151 0.5975492199077801
0.20865490007184317 0.7176626477090138
0.708988887470049 0.8600690517032243
0.5858885634109547 0.9900228746877875
0.4207526577539027 0.4509115980616851
0.26721679563705836 0.38795692270409465
0.5627701589178917 0.5191793105440308
0.9587892008777128 0.9823155299532416
0.893979917305394 0.40184945077330836
0.6248799650411605 0.5002213828574473
0.13922016844193186 0.2697416140839628
0.9614124092388507 0.2506075363030087
0.8403420376444073 0.6834231190218074
0.9141176587557365 0.4300133583400858
0.3728064777779758 0.17772360447862634
0.47579213503909745 0.46906998919124576
0.2576800028360562 0.9045669936804894
However, I would wait for some opinions about whether print_matrix can be relied upon, given that it is not exported from Base...
A short/clean solution is
show(stdout, "text/plain", rand(100,2))
Hello i trying create converter method from Disct to Vector in Julia language.
But i receive error, with i can't understand
ERROR: TypeError: Tuple: in parameter, expected Type{T}, got Dict{AbstractString,Int64}
My code
type Family
name::UTF8String
value::Int
end
function convertToVector(a1::Dict{AbstractString, Int64}())
A::Vector{Node}
for k in sort(collect(keys(a1)))
push!(A, Family(a1[k] , k))
end
return A
end
Any idea hot to change convertToVector method ?
There were several typos in the above code, but I think this should work:
# No () after the type of a1
# Also, see comment, better to parameterize function, use concrete type for Dict
function convertToVector{T<:AbstractString}(a1::Dict{T, Int64})
# This is how you create an empty vector to hold Family objects
A = Vector{Family}()
for k in sort(collect(keys(a1)))
# The values passed to the Family constructor were backwards
push!(A, Family(k, a1[k]))
end
A
end
Another way (probably not very quick):
julia> dict = Dict("fred" => 3, "jim" => 4)
Dict{ASCIIString,Int64} with 2 entries:
"fred" => 3
"jim" => 4
julia> Vector{Family}(map(f -> Family(f...), map(x -> collect(x), dict)))
2-element Array{Family,1}:
Family("fred",3)
Family("jim",4)
Perhaps I've been using too much Lisp recently...
I am trying to create vertex and i found some example in Graphs.jl documentation but i can not figure out why it is not working.
using Graphs
V1 = ExVertex(1, "V1");
V1.attributes["size"] = 5.0
But it says that ExVertex has no method matching ExVertex(Int64, ASCIIString). Any help ?
First let me check argument types of the ExVertex() function, using ? command to get help:
help?> ExVertex
search: ExVertex
No documentation found.
Summary:
type Graphs.ExVertex <: Any
Fields:
index :: Int32
label :: UTF8String
attributes :: Dict{UTF8String,Any}
So on my machine index must be Int32, now we will check the actual type of 1: typeof(1) # => Int32, therefore if I call that function as you have called it ,I will get no error:
V1 = ExVertex(1, "V1") # => vertex [1] "V1"
This test raise another question: "Why type of number 1 is differ in our machines?"
To get the right answer we must check the Julia manual section about integer types:
The default type for an integer literal depends on whether the target
system has a 32-bit architecture or a 64-bit architecture:
# 32-bit system: julia> typeof(1) Int32
# 64-bit system: julia> typeof(1) Int64
The Julia internal variable WORD_SIZE indicates whether the target system is >32-bit or 64-bit.:
# 32-bit system: julia> WORD_SIZE 32
# 64-bit system: julia> WORD_SIZE 64
Tip: You can type cast 1 as UInt32 like this:
V1 = ExVertex(1%UInt32, "V1") # => vertex [1] "V1"