NumExpr.jl
Julia library for parsing and evaluating arithmetic expressions
Install / Use
/learn @bhftbootcamp/NumExpr.jlREADME
NumExpr.jl
The NumExpr library is designed to handle and evaluate arithmetic expressions. It enables parsing and analyzing expressions, as well as performing calculations with user-defined functions.
Installation
If you haven't installed our local registry yet, do that first:
] registry add https://github.com/bhftbootcamp/Green.git
Then, to install NumExpr, simply use the Julia package manager:
] add NumExpr
Usage
Here is an example usage of NumExpr:
using NumExpr
using NumExpr: Func, Variable
const local_vars = Dict{String,Float64}(
"my_var" => 1,
"my_var{tag1='x'}" => 2,
)
const global_vars = Dict{String,Float64}(
"my_var" => 3,
"my_var[tag1='x']" => 4,
)
function NumExpr.eval_expr(var::Variable)
return get(isglobal_scope(var) ? global_vars : local_vars, var[], NaN)
end
function NumExpr.call(::Func{:maximum}, x::Number...)
return maximum(x)
end
function NumExpr.call(::Func{:sin}, x::Number)
return sin(x)
end
expr = parse_expr("sin(maximum({my_var}, [my_var], my_var{tag1='x'}, my_var[tag1='x'])) + 10");
julia> eval_expr(expr)
9.243197504692072
The package lets you set up an expression and then calculate it using data from anywhere, like databases or APIs.
using Serde
using EasyCurl
using NumExpr
using NumExpr: Func, Variable, NumVal, StrVal, ExprNode, GlobalScope, LocalScope
struct VarCtx
base_url::String
end
struct avgPrice
price::Float64
end
function my_eval(ctx::VarCtx, var::Variable{GlobalScope})
http_request = http_get(ctx.base_url, query = "symbol=" * var[], verbose = true)
return deser_json(avgPrice, http_body(http_request)).price
end
my_eval(::VarCtx, x::NumVal) = x[]
my_eval(::VarCtx, x::StrVal) = x[]
function my_eval(ctx::VarCtx, node::ExprNode)
args = map(x -> my_eval(ctx, x), node.args)
return call(ctx, node.head, args...)
end
call(::VarCtx, x...) = NumExpr.call(x...)
const local_parameters = Dict{String,Float64}(
"rtol" => 1e-3,
"atol" => 1e-2
)
function my_eval(::VarCtx, var::Variable{LocalScope})
return get(local_parameters, var[], NaN)
end
function NumExpr.call(::Func{:isapprox}, x::Number, y::Number, atol::Number, rtol::Number)
return isapprox(x, y; atol, rtol)
end
vars_context = VarCtx("https://api.binance.com/api/v3/avgPrice")
node = parse_expr("isapprox([ADABTC] * [BTCUSDT], [ADAUSDT], atol, rtol)")
julia> my_eval(vars_context, node)
true
Contributing
Contributions to NumExpr are welcome! If you encounter a bug, have a feature request, or would like to contribute code, please open an issue or a pull request on GitHub.
Related Skills
node-connect
347.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
347.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
