libpython-clj.python

Base low-level namespace for accessing Python via Clojure. Higher level interfaces are found in libpython-clj.require specifically require-python and import-python.

$.

macro

($. item attname)

Get the attribute of an object.

$..

macro

($.. item attname & args)

Get the attribute of an object. If there are extra args, apply successive get-attribute calls to the arguments.

$a

macro

($a item attr & args)

Call an attribute of an object. Similar calling conventions to afn except: Keywords must be compile time constants. So this won’t work with ‘apply’. On the other hand, building the positional and kw argmaps happens at compile time as opposed to at runtime. The attr name can be a symbol.

$c

macro

($c item & args)

Call an object. Similar calling conventions to cfn except: Keywords must be compile time constants. So this won’t work with ‘apply’. On the other hand, building the positional and kw argmaps happens at compile time as opposed to at runtime.

->jvm

(->jvm item & [options])

Copy an object into the jvm (if it wasn’t there already.)

->numpy

(->numpy item & [options])

Convert an object to numpy throwing an error if this isn’t possible.

->py-dict

(->py-dict item)

Create a python dictionary

->py-float

(->py-float item)

Convert an object into a python float

->py-fn

(->py-fn item)

Make a python function. If clojure function is passed in the arguments are marshalled from python to clojure, the function called, and the return value will be marshalled back.

->py-list

(->py-list item)

Create a python list

->py-long

(->py-long item)

Convert an object into a python long

->py-string

(->py-string item)

Copy an object into a python string

->py-tuple

(->py-tuple item)

Create a python tuple

->python

(->python item & [options])

Completely convert a jvm object to a python copy.

->python-incref

(->python-incref item)

Convert to python and add a reference. This is necessary for return values from functions as the ->python pathway adds a reference but it also tracks it and releases it when it is not in use any more. Thus python ends up holding onto something with fewer refcounts than it should have. If you are just messing around in the repl you only need ->python. There is an expectation that the return value of a function call is a new reference and not a borrowed reference hence this pathway.

a$

macro

(a$ item attr & args)

Call an attribute of an object. Similar calling conventions to afn except: Keywords must be compile time constants. So this won’t work with ‘apply’. On the other hand, building the positional and kw argmaps happens at compile time as opposed to at runtime. The attr name can be a symbol.

DEPRECATION POSSIBLE - use $a.

add-module

(add-module modname)

Add a python module. Returns a bridge

afn

(afn item attr & args)

Call an attribute of an object. Arguments are passed in positionally. Any keyword arguments are paired with the next arg, gathered, and passed into the system as *kwargs.

Not having an argument after a keyword argument is an error.

args->pos-kw-args

(args->pos-kw-args arglist)

Utility function that, given a list of arguments, separates them into positional and keyword arguments. Throws an exception if the keyword argument is not followed by any more arguments.

as-jvm

(as-jvm item & [options])

Bridge a python object into the jvm. Attempts to build a jvm bridge that ‘hides’ the python type. This bridge is lazy and noncaching so use it wisely; it may be better to just copy the type once into the JVM. Bridging is recursive so any subtypes are also bridged if possible or represented by a hashmap of {:type :value} if not.

as-list

(as-list item)

Return a List implementation using getitem, setitem.

as-map

(as-map item)

Return a Map implementation using getitem, setitem. Note that it may be incomplete especially if the object has no ‘keys’ attribute.

as-numpy

(as-numpy item & [options])

Bridge an object into numpy sharing the backing store. If it is not possible to do this without copying data then return nil.

as-python

(as-python item & [options])

Bridge a jvm object into python

as-python-incref

(as-python-incref item)

Convert to python and add a reference. Necessary for return values from functions as python expects a new reference and the as-python pathway ensures the jvm garbage collector also sees the reference.

att-type-map

(att-type-map item)

Get hashmap of att name to keyword datatype.

c$

macro

(c$ item & args)

Call an object. Similar calling conventions to cfn except: Keywords must be compile time constants. So this won’t work with ‘apply’. On the other hand, building the positional and kw argmaps happens at compile time as opposed to at runtime.

DEPRECATION POSSIBLE - use $c.

call

(call callable & args)

Call a python function with positional args. For keyword args, see call-kw.

call-attr

(call-attr item att-name & args)

Call an object attribute with positional arguments.

call-attr-kw

(call-attr-kw item att-name arglist kw-map)

Call an object attribute with a vector of positional args and a map of keyword args.

call-kw

(call-kw callable arglist kw-args)

Call a python function with a vector of positional args and a map of keyword args.

callable?

(callable? item)

Return true if object is a python callable object.

cfn

(cfn item & args)

Call an object. Arguments are passed in positionally. Any keyword arguments are paired with the next arg, gathered, and passed into the system as *kwargs.

Not having an argument after a keyword argument is an error.

create-bridge-from-att-map

(create-bridge-from-att-map src-item att-map)

create-class

(create-class name bases cls-hashmap)

Create a new class object. Any callable values in the cls-hashmap will be presented as instance methods. Things in the cls hashmap had better be either atoms or already converted python objects. You may get surprised otherwise; you have been warned. See the classes-test file in test/libpython-clj

dir

(dir item)

Get sorted list of all attribute names.

equals?

(equals? lhs rhs)

Returns true of the python equals operator returns 1.

finalize!

(finalize!)

Finalize the interpreter. You probably shouldn’t call this as it destroys the global interpreter and reinitialization is unsupported cpython.

from-import

macro

(from-import module-path item & args)

Support for the from a import b,c style of importing modules and symbols in python. Documentation is included.

gc!

(gc!)

Run the system garbage collection facility and then call the cooperative ‘cleanup python objects’ queue

get-attr

(get-attr item item-name)

Get attribute from object

get-item

(get-item item item-name)

Get an item of a given name from an object

has-attr?

(has-attr? item item-name)

Return true of object has attribute

has-item?

(has-item? item item-name)

Return true of object has item

hash-code

(hash-code py-inst)

import-as

macro

(import-as module-path varname)

Import a module and assign it to a var. Documentation is included.

import-module

(import-module modname)

Import a python module. Returns a bridge

initialize!

(initialize! & {:keys [program-name library-path python-home no-io-redirect? python-executable windows-anaconda-activate-bat]})

Initialize the python library. If library path is provided, then the python :library-path Library path of the python library to use. :program-name - optional but will show up in error messages from python. :no-io-redirect - there if you don’t want python stdout and stderr redirection to out and err.

is-instance?

(is-instance? py-inst py-type)

Returns true if inst is an instance of type. False otherwise.

len

(len item)

Call the len attribute.

libpython-clj-module-name

Module name of the libpython-clj python model. Used to find binding-level objects such as the type used for actual jvm bridging objects.

make-tuple-fn

(make-tuple-fn fn-obj & {:keys [arg-converter result-converter], :or {arg-converter ->jvm, result-converter ->python-incref}, :as options})

Given a clojure function, create a python tuple function. arg-convert is applied to arguments before the clojure function gets them and result-converter is applied to the outbound result. Exceptions are caught, logged, and propagated to python.

arg-converter: A function to be called on arguments before they get to clojure. Defaults to ->jvm. result-converter: A function to be called on the return value before it makes it back to python. Defaults to ->python-incref. method-name: Name of function exposed to python. documentation: Documentation of function exposed to python.

make-tuple-instance-fn

(make-tuple-instance-fn clj-fn & {:keys [arg-converter], :as options})

Make an instance function. In this case the default behavior is to pass raw python object ptr args to the clojure function without marshalling as that can add confusion and unnecessary overhead. Self will be the first argument. Callers can change this behavior by setting the ‘arg-converter’ option as in ‘make-tuple-fn’. Options are the same as make-tuple-fn.

module-dict

(module-dict module)

Get the module dictionary. Returns bridge.

ptr-refcnt

(ptr-refcnt item)

py*

macro

(py* x method args)(py* x method args kwargs)

Special syntax for passing along *args and **kwargs style arguments to methods.

Usage:

(py* obj method args kwargs)

Example:

(def d (python/dict)) d ;;=> {} (def iterable :a 1] [:b 2) (def kwargs {:cat “dog” :name “taco”}) (py* d update [iterable] kwargs) d ;;=> {“a”: 1, “b”: 2, “cat”: “dog”, “name”: “taco”}

py**

macro

(py** x method kwargs)(py** x method arg & args)

Like py*, but it is assumed that the LAST argument is kwargs.

py.

macro

(py. x & args)

Class/object method syntax. (py. obj method arg1 arg2 … argN) is equivalent to Python’s obj.method(arg1, arg2, …, argN) syntax.

py.-

macro

(py.- x arg)

Class/object getter syntax. (py.- obj attr) is equivalent to Python’s obj.attr syntax.

py..

macro

(py.. x & args)

Extended accessor notation, similar to the .. macro in Clojure.

(require-python ’sys) (py.. sys -path (append “/home/user/bin”))

is equivalent to Python’s

import sys sys.path.append(‘/home/user/bin’)

SPECIAL SYNTAX for programmatic *args and **kwargs

Special syntax is provided to meet the needs required by Python’s *args and **kwargs syntax programmatically.

(= (py.. obj (*method args)) (py* obj methods args))

(= (py.. obj (*methods args kwargs)) (py* obj method args kwargs))

(= (py.. obj (**method kwargs)) (py** obj kwargs))

(= (py.. obj (**method arg1 arg2 arg3 … argN kwargs)) (py** obj method arg1 arg2 arg3 … argN kwargs) (py* obj method [arg1 arg2 arg3 … argN] kwargs))

These forms exist for when you need to pass in a map of options in the same way you would use the f(*args, **kwargs) forms in Python.

python-pyerr-fetch-error-handler

(python-pyerr-fetch-error-handler)

Utility code used in with macro

python-type

(python-type item)

Return a keyword that describes the python datatype of this object.

run-simple-string

(run-simple-string program & {:keys [globals locals]})

Run a string expression returning a map of {:globals :locals}. This uses the global main dict under the covers so it matches the behavior of the cpython implementation with the exception of returning the various maps used.

Note this will never return the result of the expression: https://mail.python.org/pipermail/python-list/1999-April/018011.html

Globals, locals may be provided but are not necessary.

Implemented in cpython as:

PyObject *m, *d, *v; m = PyImport_AddModule(“main”); if (m == NULL) return -1; d = PyModule_GetDict(m); v = PyRun_StringFlags(command, Py_file_input, d, d, flags); if (v == NULL) { PyErr_Print(); return -1; } Py_DECREF(v); return 0;

run-string

(run-string program & {:keys [globals locals]})

Wrapper around the python c runtime PyRun_String method. This requires you to understand what needs to be in the globals and locals dict in order for everything to work out right and for this reason we recommend run-simple-string.

set-attr!

(set-attr! item item-name item-value)

Set attribute on object

set-attrs!

(set-attrs! item att-seq)

Set a sequence of [name value] attributes. Returns item

set-item!

(set-item! item item-name item-value)

Set an item of to a value

set-items!

(set-items! item item-seq)

Set a sequence of [name value]. Returns item

stack-resource-context

macro

(stack-resource-context & body)

Create a stack-based resource context. All python objects allocated within this context will be released at the termination of this context. !!This means that no python objects can escape from this context!! You must use copy semantics (->jvm) for anything escaping this context. Furthermore, if you are returning generic python objects you may need to call (into {}) or something like that just to ensure that absolutely everything is copied into the jvm.

with

macro

(with bind-vec & body)

Support for the ‘with’ statement in python: (py/with [item (py/call-attr testcode-module “WithObjClass” true fn-list)] (py/call-attr item “doit_err”))

with-exit-error-handler

(with-exit-error-handler with-var error)

Utility code used in with macro

with-gil

macro

(with-gil & body)

Capture the gil for an extended amount of time. This can greatly speed up operations as the mutex is captured and held once as opposed to find grained grabbing/releasing of the mutex.

with-gil-stack-rc-context

macro

(with-gil-stack-rc-context & body)

Capture the gil, open a resource context. The resource context is released before the gil is leading to much faster resource collection. See documentation on stack-resource-context for multiple warnings; the most important one being that if a python object escapes this context your program will eventually, at some undefined point in the future crash. That being said, this is the recommended pathway to use in production contexts where you want defined behavior and timings related to use of python.