clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.06k stars 69 forks source link

Operator overloading #122

Closed zendevil closed 2 years ago

zendevil commented 3 years ago

Basically, many python libraries have overloaded operators. For example, in Pandas, you get the time delta between two times by doing:

dataframe.to_datetime("01/02/2020") - dataframe.to_datetime("01/01/2020")

Unfortunately, doing the following:

(- (to_datetime etc.) (to_datetime etc.))

doesn't yet work, gives:

. Caused by java.lang.ClassCastException
   libpython_clj.python.bridge$generic_python_as_jvm$reify__32765 cannot be cast
   to java.lang.Number

On the other hand, doing this:

((py/->python -) (to_datetime etc.) (to_datetime etc.))

gives:

Caused by java.lang.ClassCastException
   com.sun.jna.Pointer cannot be cast to clojure.lang.IFn

Is there anyway to overload the negative sign? The problem is that whatever the actual implemented function is that overloads the minus is buried somewhere in the source and isn't well known or meant to be exposed, it seems to me. So how to do these basic operations?

cnuernber commented 3 years ago

We would need a namespace that redefines '-' in terms of the python "minus" attribute (and "div", etc). Definitely doable.

zendevil commented 3 years ago

How can you please elaborate I don’t get it?

sogaiu commented 3 years ago

Perhaps some bits of what's here are relevant?

cnuernber commented 2 years ago

Closing. This isn't a bug with libpython-clj but rather just needs someone to implement a namespace calling into the python functions themselves.