Skip to content

Commit a624ea7

Browse files
authored
Merge pull request #40 from jverzani/bool
code cleanup
2 parents b58aeb2 + a1db21f commit a624ea7

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SymPyPythonCall"
22
uuid = "bc8888f7-b21e-4b7c-a06a-5d9c9496438c"
33
authors = ["jverzani <jverzani@gmail.com> and contributors"]
4-
version = "0.2.1"
4+
version = "0.2.2"
55

66
[deps]
77
CommonEq = "3709ef60-1bee-4518-9f2f-acd86f176c50"
@@ -27,7 +27,7 @@ PythonCall = "0.9"
2727
SpecialFunctions = "0.8, 0.9, 0.10, 1.0, 2"
2828
Symbolics = "5"
2929
SymbolicUtils = "1"
30-
SymPyCore = "0.1"
30+
SymPyCore = "0.1.3, 1"
3131
julia = "1.6.1"
3232

3333

src/python_connection.jl

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ Base.convert(::Type{S}, x::Sym{T}) where {T <: PythonCall.Py, S<:Sym} = x
66
Base.convert(::Type{S}, x::T) where {T<:PythonCall.Py, S <: SymbolicObject} = Sym(x)
77

88
SymPyCore._convert(::Type{T}, x) where {T} = pyconvert(T, x)
9-
SymPyCore._convert(::Type{Bool}, x) = pyconvert(Bool, pybool(x))
9+
function SymPyCore._convert(::Type{Bool}, x::Py)
10+
pyconvert(Bool, x == _sympy_.logic.boolalg.BooleanTrue) && return true
11+
pyconvert(Bool, x == _sympy_.logic.boolalg.BooleanFalse) && return false
12+
pyconvert(Bool, pybool(x))
13+
end
1014

1115

1216
## Modifications for ↓, ↑
@@ -53,42 +57,39 @@ function Base.getproperty(x::SymbolicObject{T}, a::Symbol) where {T <: PythonCal
5357
Base.depwarn("The field `.py` has been renamed `.o`", :getproperty)
5458
return getfield(x,:o)
5559
end
60+
5661
val = (x)
57-
if hasproperty(val, a)
58-
meth = getproperty(val, a)
62+
!hasproperty(val, a) && return nothing # not a property
5963

60-
pyconvert(Bool, meth == pybuiltins.None) && return nothing
64+
meth = getproperty(val, a)
6165

62-
if hasproperty(meth, "is_Boolean")
63-
o = Sym(getproperty(meth, "is_Boolean"))
64-
o == Sym(true) && return true
65-
a == :is_Boolean && return o == Sym(False) ? false : nothing
66+
pyconvert(Bool, meth == pybuiltins.None) && return nothing
67+
68+
if hasproperty(meth, "is_Boolean")
69+
if pyconvert(Bool, meth.is_Boolean == true)
70+
return meth == _sympy_.logic.boolalg.BooleanFalse
6671
end
72+
end
6773

68-
# __class__ dispath
69-
if hasproperty(meth, :__class__)
70-
cnm = string(meth.__class__.__name__)
71-
if cnm == "bool"
72-
a = Sym(meth)
73-
return a == Sym(true) ? true :
74-
a == Sym(false) ? false : nothing
75-
end
76-
if cnm == "module"
77-
# treat modules, callsm others differently
78-
return Sym(meth)
79-
end
74+
# __class__ dispatch
75+
if hasproperty(meth, :__class__)
76+
cnm = string(meth.__class__.__name__)
77+
if cnm == "bool"
78+
return pyconvert(Bool, meth)
8079
end
81-
## __function__
82-
if hasproperty(meth, "__call__")
83-
#meth = getproperty(meth, "__call__")
84-
return SymPyCore.SymbolicCallable(meth)
80+
if cnm == "module"
81+
# treat modules, callsm others differently
82+
return Sym(meth)
8583
end
84+
end
8685

87-
return (convert(PythonCall.Py, meth))
88-
86+
## __call__
87+
if hasproperty(meth, "__call__")
88+
return SymPyCore.SymbolicCallable(meth)
8989
end
90-
# not a property; should this error
91-
return nothing
90+
91+
return (convert(PythonCall.Py, meth))
92+
9293
end
9394

9495

0 commit comments

Comments
 (0)