Skip to content

Commit ae76127

Browse files
authored
Merge pull request #44 from jverzani/uparrow
Uparrow
2 parents ca9ea65 + 79cfb36 commit ae76127

3 files changed

Lines changed: 30 additions & 31 deletions

File tree

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ uuid = "bc8888f7-b21e-4b7c-a06a-5d9c9496438c"
33
authors = ["jverzani <[email protected]> and contributors"]
44
version = "0.2.3"
55

6+
67
[deps]
78
CommonEq = "3709ef60-1bee-4518-9f2f-acd86f176c50"
89
CommonSolve = "38540f10-b2f7-11e9-35d8-d573e4eb0ff2"
@@ -19,15 +20,15 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1920
SymPyPythonCallSymbolicsExt = "Symbolics"
2021

2122
[compat]
22-
CommonEq = "0.2"
23+
CommonEq = "0.2.1"
2324
CommonSolve = "0.2"
2425
CondaPkg = "0.2"
2526
LinearAlgebra = "1.6.1"
2627
PythonCall = "0.9"
2728
SpecialFunctions = "0.8, 0.9, 0.10, 1.0, 2"
2829
Symbolics = "5"
2930
SymbolicUtils = "1"
30-
SymPyCore = "=0.1.5, 1"
31+
SymPyCore = "0.1.6, 1"
3132
julia = "1.6.1"
3233

3334

src/SymPyPythonCall.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ _pyimport(a) = PythonCall.pyimport(a)
1515
_pyimport_conda(a,b) = PythonCall.pyimport(a) # XXX lose things
1616
_pyobject(x) = PythonCall.pyconvert(Py, x)
1717
_pytype_mapping(typ,a) = nothing
18+
const _pybuiltin = PythonCall.pybuiltins
1819

1920
core_src_path = joinpath(pathof(SymPyCore), "../../src/SymPy")
2021
include(joinpath(core_src_path, "sympy.jl"))

src/python_connection.jl

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ end
1515

1616
## Modifications for ↓, ↑
1717
Sym(x::Nothing) = Sym(pybuiltins.None)
18+
1819
SymPyCore.:(x::PythonCall.Py) = x
1920
SymPyCore.:(d::Dict) = pydict(((k) => (v) for (k,v) pairs(d)))
2021
SymPyCore.:(x::Set) = _sympy_.sympify(pyset((sᵢ) for sᵢ x))
2122

22-
SymPyCore.:(::Type{<:AbstractString}, x) = Py(x)
23+
SymPyCore.:(::Type{<:AbstractString}, x) = Sym(Py(x))
2324
function SymPyCore.:(::Type{PythonCall.Py}, x)
24-
class_nm = SymPyCore.classname(x)
25-
class_nm == "set" && return Set(Sym.(collect(x)))
26-
class_nm == "tuple" && return Tuple((xᵢ) for xᵢ x)
27-
class_nm == "list" && return [(xᵢ) for xᵢ x]
28-
class_nm == "dict" && return Dict((k) => (x[k]) for k x)
29-
30-
class_nm == "FiniteSet" && return Set(Sym.(collect(x)))
31-
class_nm == "MutableDenseMatrix" && return _up_matrix(x) #map(↑, x.tolist())
25+
# this lower level approach shouldn't allocate
26+
pyisinstance(x, pybuiltins.set) && return Set(Sym.(x)) #Set(↑(xᵢ) for xᵢ ∈ x)
27+
pyisinstance(x, pybuiltins.tuple) && return Tuple((xᵢ) for xᵢ x)
28+
pyisinstance(x, pybuiltins.list) && return [(xᵢ) for xᵢ x]
29+
pyisinstance(x, pybuiltins.dict) && return Dict((k) => (x[k]) for k x)
3230

33-
# others ... more hands on than pytype_mapping
31+
# add more sympy containers in sympy.jl and here
32+
pyisinstance(x, _FiniteSet_) && return Set(Sym.(x))
33+
pyisinstance(x, _MutableDenseMatrix_) && return _up_matrix(x) #map(↑, x.tolist())
3434

35+
# fallback
3536
Sym(x)
3637
end
3738

@@ -52,42 +53,38 @@ end
5253

5354
# should we also have different code path for a::String like PyCall?
5455
function Base.getproperty(x::SymbolicObject{T}, a::Symbol) where {T <: PythonCall.Py}
56+
5557
a == :o && return getfield(x, a)
58+
5659
if a == :py
5760
Base.depwarn("The field `.py` has been renamed `.o`", :getproperty)
5861
return getfield(x,:o)
5962
end
6063

6164
val = (x)
62-
!hasproperty(val, a) && return nothing # not a property
65+
𝑎 = string(a)
6366

64-
meth = getproperty(val, a)
67+
hasproperty(val, 𝑎) || return nothing # not a property
68+
meth = getproperty(val, 𝑎)
6569

66-
pyconvert(Bool, meth == pybuiltins.None) && return nothing
70+
pyis(meth, pybuiltins.None) && return nothing
6771

68-
if hasproperty(meth, "is_Boolean")
69-
if pyconvert(Bool, meth.is_Boolean == true)
70-
return meth == _sympy_.logic.boolalg.BooleanFalse
71-
end
72+
## __call__
73+
if pycallable(meth) # "__call__") #hasproperty(meth, "__call__")
74+
return SymPyCore.SymbolicCallable(meth)
7275
end
7376

7477
# __class__ dispatch
75-
if hasproperty(meth, :__class__)
76-
cnm = string(meth.__class__.__name__)
77-
if cnm == "bool"
78-
return pyconvert(Bool, meth)
79-
end
80-
if cnm == "module"
81-
# treat modules, callsm others differently
82-
return Sym(meth)
83-
end
78+
if pyisinstance(meth, _bool_)
79+
return pyconvert(Bool, meth)
8480
end
8581

86-
## __call__
87-
if hasproperty(meth, "__call__")
88-
return SymPyCore.SymbolicCallable(meth)
82+
if pyisinstance(meth, _ModuleType_)
83+
return Sym(meth)
8984
end
9085

86+
87+
# just convert
9188
return (convert(PythonCall.Py, meth))
9289

9390
end

0 commit comments

Comments
 (0)