Here is a collection of useful code that I've either found or made myself
Most of these quirks are very useful so it is good to know about them.
- When creating a function if you put a
!
after the function name but before the inputs the function will be able to change the inputs otherwise they will be treated as immutable
Path management can be a pain to figure out so I'm adding everything I've learned about path management in Julia here.
-
@__DIR__
anddirname(@__FILE__)
both return anAbstractString
-
The following for loop is kind of like globbing for files that end in a certain extension:
for f in filter(x -> endswith(x, "jl"), readdir()) println(f) end
-
I find it very common to put a script in a folder and want to have the program "search" for a folder in parent folders until that folder is found. This is how I do that in a function.
using FilePathsBase using Glob function find_edgelist_files(target_folder) """finds the directory with all of the edgelist files""" path = Path(dirname(@__FILE__)) n = 0 while true listDirs = [path_1 for path_1 in readdir(path) if isdir(path)] listDirsStem = [string(basename(path_1)) for path_1 in readdir(path) if isdir(path)] if target_folder in listDirsStem path = path / target_folder files = glob("*.csv", string(path)) # break return files, path else path = parent(path) end end end target_folder = "author_subreddit_edgelists" find_edgelist_files(target_folder)
-
Similar to the isin function in pandas where you filter a dataframe based on what is in another array or list of items
df[ [x in [3,4] for x in df[:A]] ,:]
- An easy way to time some function is to use the
@elapsed
macro, this will print the output in seconds that it took to run whatever you're evaluating