-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintrospection.jl
86 lines (70 loc) · 1.84 KB
/
introspection.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## Module for Introspection
module Introspection
import SymPyPythonCall: Sym, asSymbolic
using PythonCall
import PythonCall: Py
export args, func, funcname, class, classname, getmembers
# utilities
"""
funcname(x)
Return name or ""
"""
function funcname(x::Union{Sym, Py})
y = Py(x)
func = pygetattr(y, "func", nothing)
func == nothing && return ""
return string(func.__name__)
end
"""
func(x)
Return function head from an expression
[Invariant:](http://docs.sympy.org/dev/tutorial/manipulation.html)
Every well-formed SymPy expression `ex` must either have `length(args(ex)) == 0` or
`func(ex)(args(ex)...) = ex`.
"""
func(ex::Sym) = return Py(ex).func
"""
args(x)
Return arguments of `x`, as a tuple. (Empty if no `:args` property.)
"""
function args(x::Union{Sym, Py})
asSymbolic(pygetattr(Py(x), "args", ()))
end
# return class or nothing
class(x::T) where {T <: Union{Sym, Py}} = getattr(x, "__class__", nothing)
classname(x::T) where {T <: Union{Sym, Py}} = (cls = class(x); isnothing(cls) ? "" : cls.__name__)
#function getmembers(x::T) where {T <: Union{Sym, PyObject}}
# Dict(u=>v for (u,v) in inspect.getmembers(x))
#end
## Map to get function object from type information
const funcname2function = (
Add = +,
Sub = -,
Mul = *,
Div = /,
Pow = ^,
re = real,
im = imag,
Abs = abs,
Min = min,
Max = max,
Poly = identity,
Piecewise = error, # replace
Order = (as...) -> 0,
And = (as...) -> all(as),
Or = (as...) -> any(as),
Less = <,
LessThan = <=,
StrictLessThan = <,
Equal = ==,
Equality = ==,
Unequality = !==,
StrictGreaterThan = >,
GreaterThan = >=,
Greater = >,
conjugate = conj,
atan2 = atan,
TupleArg = tuple,
Heaviside = (a...) -> (a[1] < 0 ? 0 : (a[1] > 0 ? 1 : (length(a) > 1 ? a[2] : NaN))),
)
end