Checking file existence in Julia

Julia has the isfile() function to test for a regular file: julia> isfile(“foo.txt”) false shell> touch foo.txt julia> isfile(“foo.txt”) true As the documentation: Returns true if path (the parameter) is a regular file, false otherwise.

Search for files in a folder

In Julia, the equivalent to list.files() is readdir([path]) There is no built-in directory search that I know of, but it is a one-liner: searchdir(path,key) = filter(x->contains(x,key), readdir(path)) UPDATE: Since at least Julia v0.7, contains() has been deprecated for occursin(substring, string). So the above filter would now be: searchdir(path,key) = filter(x->occursin(key,x), readdir(path))

Julia: Flattening array of array/tuples

Iterators.flatten(x) creates a generator that iterates over each element of x. It can handle some of the cases you describe, eg julia> collect(Iterators.flatten([(1,2,3),[4,5],6])) 6-element Array{Any,1}: 1 2 3 4 5 6 If you have arrays of arrays of arrays and tuples, you should probably reconsider your data structure because it doesn’t sound type stable. However, … Read more

How to get fields of a Julia object

For v0.7+ Use fieldnames(x), where x is a DataType. For example, use fieldnames(Date), instead of fieldnames(today()), or else use fieldnames(typeof(today())). This returns Vector{Symbol} listing the field names in order. If a field name is myfield, then to retrieve the values in that field use either getfield(x, :myfield), or the shortcut syntax x.myfield. Another useful and … Read more

I have a high-performant function written in Julia, how can I use it from Python?

Assuming your Python and Julia are installed you need to take the following steps. Run Julia and install PyCall using Pkg pkg”add PyCall” Put your code into a Julia package using Pkg Pkg.generate(“MyPackage”) In the folder src you will find MyPackage.jl, edit it to look like this: module MyPackage f(x,y) = 2x.+y export f end … Read more

How to read a file line by line in Julia?

Reading a file into memory all at once as an array of lines is just a call to the readlines function: julia> words = readlines(“/usr/share/dict/words”) 235886-element Array{String,1}: “A” “a” “aa” ⋮ “zythum” “Zyzomys” “Zyzzogeton” By default this discards the newlines but if you want to keep them, you can pass the keyword argument keep=true: julia> … Read more

How and When to Use @async and @sync in Julia

According to the documentation under ?@async, “@async wraps an expression in a Task.” What this means is that for whatever falls within its scope, Julia will start this task running but then proceed to whatever comes next in the script without waiting for the task to complete. Thus, for instance, without the macro you will … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)