|
| 1 | +(ns libpython-clj2.java-api |
| 2 | + (:require [tech.v3.datatype :as dtype] |
| 3 | + [tech.v3.tensor :as dtt] |
| 4 | + [tech.v3.datatype.jvm-map :as jvm-map] |
| 5 | + [libpython-clj2.python :as py] |
| 6 | + [libpython-clj2.python.fn :as py-fn] |
| 7 | + ;; numpy support |
| 8 | + [libpython-clj2.python.np-array]) |
| 9 | + (:import [java.util Map] |
| 10 | + [java.util.function Supplier]) |
| 11 | + (:gen-class |
| 12 | + :name libpython_clj2.java_api |
| 13 | + :main false |
| 14 | + :methods [#^{:static true} [initialize [java.util.Map] Object] |
| 15 | + #^{:static true} [runString [String] java.util.Map] |
| 16 | + #^{:static true} [inPyContext [java.util.function.Supplier] Object] |
| 17 | + #^{:static true} [hasAttr [Object String] Boolean] |
| 18 | + #^{:static true} [getAttr [Object String] Object] |
| 19 | + #^{:static true} [setAttr [Object String Object] Object] |
| 20 | + #^{:static true} [hasItem [Object Object] Boolean] |
| 21 | + #^{:static true} [getItem [Object Object] Object] |
| 22 | + #^{:static true} [setItem [Object Object Object] Object] |
| 23 | + #^{:static true} [importModule [String] Object] |
| 24 | + #^{:static true} [callKw [Object java.util.List java.util.Map] Object] |
| 25 | + #^{:static true} [copyToPy [Object] Object] |
| 26 | + #^{:static true} [copyToJVM [Object] Object] |
| 27 | + #^{:static true} [createArray [String Object Object] Object] |
| 28 | + #^{:static true} [arrayToJVM [Object] java.util.Map]])) |
| 29 | + |
| 30 | + |
| 31 | +(set! *warn-on-reflection* true) |
| 32 | + |
| 33 | + |
| 34 | +(defn -initialize |
| 35 | + "See options for [[libpython-clj2.python/initialize!]]. Note that the keyword |
| 36 | + option arguments can be provided by using strings so `:library-path` becomes |
| 37 | + the key \"library-path\". Also note that there is still a GIL underlying |
| 38 | + all of the further operations so java should access python via single-threaded |
| 39 | + pathways." |
| 40 | + [options] |
| 41 | + (apply py/initialize! (->> options |
| 42 | + (mapcat (fn [entry] |
| 43 | + [(keyword (jvm-map/entry-key entry)) |
| 44 | + (jvm-map/entry-value entry)]))))) |
| 45 | + |
| 46 | + |
| 47 | +(defn -runString |
| 48 | + "Run a string returning a map of two keys -\"globals\" and \"locals\" which each point to |
| 49 | + a java.util.Map of the respective contexts. See documentation under |
| 50 | + [[libpython-clj2.python/run-simple-string]] - specifically this will never return |
| 51 | + the result of the last statement ran - you need to set a value in the global or local |
| 52 | + context." |
| 53 | + ^java.util.Map [^String code] |
| 54 | + (->> (py/run-simple-string code) |
| 55 | + (map (fn [[k v]] |
| 56 | + [(name k) v])) |
| 57 | + (into {}))) |
| 58 | + |
| 59 | + |
| 60 | +(defn -inPyContext |
| 61 | + "Run some code in a python context where all python objects allocated within the context |
| 62 | + will be deallocated once the context is released. The code must not return references |
| 63 | + to live python objects." |
| 64 | + [^Supplier fn] |
| 65 | + (py/with-gil-stack-rc-context |
| 66 | + (-> (.get fn) |
| 67 | + (py/->jvm)))) |
| 68 | + |
| 69 | + |
| 70 | +(defn -hasAttr |
| 71 | + "Returns true if this python object has this attribute." |
| 72 | + [pyobj attname] |
| 73 | + (boolean (py/has-attr? pyobj attname))) |
| 74 | + |
| 75 | + |
| 76 | +(defn -getAttr |
| 77 | + "Get a python attribute. This corresponds to the python '.' operator." |
| 78 | + [pyobj attname] |
| 79 | + (py/get-attr pyobj attname)) |
| 80 | + |
| 81 | + |
| 82 | +(defn -setAttr |
| 83 | + "Set an attribute on a python object. This corresponds to the `__setattr` python call." |
| 84 | + [pyobj attname objval] |
| 85 | + (py/set-attr! pyobj attname objval)) |
| 86 | + |
| 87 | + |
| 88 | +(defn -hasItem |
| 89 | + "Return true if this pyobj has this item" |
| 90 | + [pyobj itemName] |
| 91 | + (boolean (py/has-item? pyobj itemName))) |
| 92 | + |
| 93 | + |
| 94 | +(defn -getItem |
| 95 | + [pyobj itemName] |
| 96 | + (py/get-item pyobj itemName)) |
| 97 | + |
| 98 | + |
| 99 | +(defn -getItem |
| 100 | + [pyobj itemName] |
| 101 | + (py/get-item pyobj itemName)) |
| 102 | + |
| 103 | + |
| 104 | +(defn -setItem |
| 105 | + [pyobj itemName itemVal] |
| 106 | + (py/get-item pyobj itemName itemVal)) |
| 107 | + |
| 108 | + |
| 109 | +(defn -importModule |
| 110 | + "Import a python module. Module entries can be accessed via `getAttr`." |
| 111 | + [modname] |
| 112 | + (py/import-module modname)) |
| 113 | + |
| 114 | + |
| 115 | +(defn -callKw |
| 116 | + "Call a python callable with keyword arguments. Note that you don't need this pathway |
| 117 | + to call python methods if you do not need keyword arguments; if the python object is |
| 118 | + callable then it will implement `clojure.lang.IFn` and you can use `invoke`." |
| 119 | + [pyobj pos-args kw-args] |
| 120 | + (py/with-gil (py-fn/call-kw pyobj pos-args (->> kw-args |
| 121 | + (map (fn [entry] |
| 122 | + [(jvm-map/entry-key entry) |
| 123 | + (jvm-map/entry-value entry)])) |
| 124 | + (into {}))))) |
| 125 | + |
| 126 | +(defn -copyToPy |
| 127 | + "Copy a basic jvm object, such as an implementation of java.util.Map or java.util.List to |
| 128 | + a python object." |
| 129 | + [object] |
| 130 | + (py/->python object)) |
| 131 | + |
| 132 | + |
| 133 | +(defn -copyToJVM |
| 134 | + "Copy a python object such as a dict or a list into a comparable JVM object." |
| 135 | + [object] |
| 136 | + (py/->jvm object)) |
| 137 | + |
| 138 | + |
| 139 | +(defn -createArray |
| 140 | + "Create a numpy array from a tuple of string datatype, shape and data. |
| 141 | + * `datatype` - One of \"int8\" \"uint8\" " |
| 142 | + [datatype shape data] |
| 143 | + (-> (dtt/->tensor data :datatype (keyword datatype)) |
| 144 | + (dtt/reshape shape) |
| 145 | + (py/->python))) |
| 146 | + |
| 147 | + |
| 148 | +(defn -arrayToJVM |
| 149 | + "Copy (efficiently) a numeric numpy array into a jvm map containing keys \"datatype\", |
| 150 | + \"shape\", and \"data\"." |
| 151 | + [pyobj] |
| 152 | + (let [tens-data (py/as-jvm pyobj)] |
| 153 | + {"datatype" (name (dtype/elemwise-datatype tens-data)) |
| 154 | + "shape" (int-array (dtype/shape tens-data)) |
| 155 | + "data" (dtype/->array tens-data)})) |
0 commit comments