入坑特性
- 宏: Expr -> Expr
- @less, @code_native
- ÷ ≠ ∈
- Condition + wait
有的时候你写了一个函数,它需要很多参数
"Align two sequences locally"
function align(a::String, b::String; mismatch::Int=7, gap_open::Int=11,
gap_extend::Int=2, threshold::Int=20)::Void
...
end
如果希望在命令行调用,你需要写很长一段代码 (ArgParse.jl)
using ArgParse
s = ArgParseSettings("Align two sequences locally")
@add_arg_table s begin
"a"
required = true
"b"
required = true
"--mismatch"
arg_type = Int
default = 7
"--gap_open"
arg_type = Int
default = 11
"--gap_extend"
arg_type = Int
default = 2
"--threshold"
arg_type = Int
default = 20
end
parsed_args = parse_args(s)
align(parsed_args["a"], parsed_args["b"]; parsed_args["mismatch"],
parsed_args["gap_open"], parsed_args["gap_extend"], parsed_args["threshold"])
Fire.jl 的全部用法只需要2秒即可完全掌握
using Fire
"Align two sequences locally"
@main function align(a::String, b::String; mismatch::Int=7, gap_open::Int=11,
gap_extend::Int=2, threshold::Int=20)::Void
...
end
然后就会自动生成 --help,自动转换参数类型,智能处理 Vector, Bool 型关键字参数
➜ ~ julia align.jl
Need 2 positional arguments, see --help for what are them
➜ ~ julia align.jl --help
Align two sequences locally
Positional Arguments:
a: String
b: String
Optional Arguments:
--mismatch: Int64 (default: 7)
--gap_open: Int64 (default: 11)
--gap_extend: Int64 (default: 2)
--threshold: Int64 (default: 20)
➜ ~ julia align.jl --threshold 15 \
> AAAGCGCGAAGCTACGTAGCTGACTGATCGTACGTACGTCGTAGC \
> GCGCGAAGCTACGTAGAAACTGACTGATCGTACCTACGTCGTAGC
GCGCGAAGCTACGTAG---CTGACTGATCGTACGTACGTCGTAGC
|||||||||||||||| |||||||||||||| |||||||||||
GCGCGAAGCTACGTAGAAACTGACTGATCGTACCTACGTCGTAGC
安装方法: Pkg.clone("https://github.com/ylxdzsw/Fire.jl")
如果需要生成一个具有固定字段的 JSON,很容易想到两个方案:
time = ["2017-01-17", "2017-01-18", "2017-01-19", ...]
open = [118.339996, 119.400002, 120.449997, ...]
top = [120.239998, 120.5, 120.089996, ...]
bottom = [118.220001, 119.709999, 119.370003, ...]
close = [120, 119.989998, 119.779999, ...]
approch1 = """{
"x": [$(join(map(x->"\"$x\"", time), ","))],
"open": [$(join(open, ","))],
"close": [$(join(close, ","))],
"high": [$(join(top, ","))],
"low": [$(join(bottom, ","))],
"decreasing": {"line": {"color": "#7F7F7F"}},
"increasing": {"line": {"color": "#17BECF"}},
"line": {"color": "rgba(31,119,180,1)"},
"type": "ohlc",
"xaxis": "x",
"yaxis": "y"
}"""
approch2 = JSON.json(Dict(
"x" => time,
"open" => open,
"close" => close,
"high" => top,
"low" => bottom,
"decreasing" => Dict("line" => Dict("color" => "#7F7F7F")),
"increasing" => Dict("line" => Dict("color" => "#17BECF")),
"line" => Dict("color" => "rgba(31,119,180,1)"),
"type" => "ohlc",
"xaxis" => "x",
"yaxis" => "y"
))
JsonBuilder.jl 使用类似 JavaScript 的高兼容性语法,采取特殊的插值规则,并且在编译期完成格式解析
approch3 = @json """{
x: $time,
open: $open,
close: $close,
high: $top,
low: $bottom,
decreasing: {line: {color: "#7F7F7F"}},
increasing: {line: {color: "#17BECF"}},
line: {color: "rgba(31,119,180,1)"},
type: "ohlc",
xaxis: "x",
yaxis: "y"
}"""
安装方法: Pkg.clone("https://github.com/ylxdzsw/JsonBuilder.jl")
使用 Redis 来(伪)持久化应用数据,或者协调多进程并行计算,而不需要学习 Redis
using RedisAlchemy
conn = RedisConnectionPool(10)
list = RedisList{String}("task")
push!(list, 2)
unshift!(list, 3)
length(list)
list[1:2]
sort!(list)
list[:]
安装方法: Pkg.clone("https://github.com/ylxdzsw/RedisAlchemy.jl")