-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathlibpython-clj2.python.html
More file actions
311 lines (306 loc) · 43.7 KB
/
libpython-clj2.python.html
File metadata and controls
311 lines (306 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html PUBLIC ""
"">
<html><head><meta charset="UTF-8" /><title>libpython-clj2.python documentation</title><script async="true" src="https://www.googletagmanager.com/gtag/js?id=G-LN7PG6FJ2D"></script><script>window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-LN7PG6FJ2D');</script><link rel="stylesheet" type="text/css" href="css/default.css" /><link rel="stylesheet" type="text/css" href="highlight/solarized-light.css" /><script type="text/javascript" src="highlight/highlight.min.js"></script><script type="text/javascript" src="js/jquery.min.js"></script><script type="text/javascript" src="js/page_effects.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div id="header"><h2>Generated by <a href="https://github.com/weavejester/codox">Codox</a> with <a href="https://github.com/xsc/codox-theme-rdash">RDash UI</a> theme</h2><h1><a href="index.html"><span class="project-title"><span class="project-name">libpython-clj</span> <span class="project-version">2.026</span></span></a></h1></div><div class="sidebar primary"><h3 class="no-link"><span class="inner">Project</span></h3><ul class="index-link"><li class="depth-1 "><a href="index.html"><div class="inner">Index</div></a></li></ul><h3 class="no-link"><span class="inner">Topics</span></h3><ul><li class="depth-1 "><a href="Usage.html"><div class="inner"><span>LibPython-CLJ Usage</span></div></a></li><li class="depth-1 "><a href="embedded.html"><div class="inner"><span>Embedding Clojure In Python</span></div></a></li><li class="depth-1 "><a href="environments.html"><div class="inner"><span>Python Environments</span></div></a></li><li class="depth-1 "><a href="new-to-clojure.html"><div class="inner"><span>So Many Parenthesis!</span></div></a></li><li class="depth-1 "><a href="scopes-and-gc.html"><div class="inner"><span>Scopes And Garbage Collection</span></div></a></li><li class="depth-1 "><a href="slicing.html"><div class="inner"><span>Slicing And Slices</span></div></a></li></ul><h3 class="no-link"><span class="inner">Namespaces</span></h3><ul><li class="depth-1"><div class="no-link"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>libpython-clj2</span></div></div></li><li class="depth-2 branch"><a href="libpython-clj2.codegen.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>codegen</span></div></a></li><li class="depth-2 branch"><a href="libpython-clj2.embedded.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>embedded</span></div></a></li><li class="depth-2 branch"><a href="libpython-clj2.java-api.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>java-api</span></div></a></li><li class="depth-2 current"><a href="libpython-clj2.python.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>python</span></div></a></li><li class="depth-3 branch"><a href="libpython-clj2.python.class.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>class</span></div></a></li><li class="depth-3"><a href="libpython-clj2.python.np-array.html"><div class="inner"><span class="tree"><span class="top"></span><span class="bottom"></span></span><span>np-array</span></div></a></li><li class="depth-2"><a href="libpython-clj2.require.html"><div class="inner"><span class="tree" style="top: -83px;"><span class="top" style="height: 92px;"></span><span class="bottom"></span></span><span>require</span></div></a></li></ul></div><div class="sidebar secondary"><h3><a href="#top"><span class="inner">Public Vars</span></a></h3><ul><li class="depth-1"><a href="libpython-clj2.python.html#var-.24a"><div class="inner"><span>$a</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-.24c"><div class="inner"><span>$c</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var--.3Ejvm"><div class="inner"><span>->jvm</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var--.3Epy-dict"><div class="inner"><span>->py-dict</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var--.3Epy-list"><div class="inner"><span>->py-list</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var--.3Epy-tuple"><div class="inner"><span>->py-tuple</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var--.3Epython"><div class="inner"><span>->python</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-add-module"><div class="inner"><span>add-module</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-afn"><div class="inner"><span>afn</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-as-jvm"><div class="inner"><span>as-jvm</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-as-list"><div class="inner"><span>as-list</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-as-map"><div class="inner"><span>as-map</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-as-python"><div class="inner"><span>as-python</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-attr-type-map"><div class="inner"><span>attr-type-map</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-call-attr"><div class="inner"><span>call-attr</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-call-attr-kw"><div class="inner"><span>call-attr-kw</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-callable.3F"><div class="inner"><span>callable?</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-cfn"><div class="inner"><span>cfn</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-create-class"><div class="inner"><span>create-class</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-def-unpack"><div class="inner"><span>def-unpack</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-dir"><div class="inner"><span>dir</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-from-import"><div class="inner"><span>from-import</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-get-attr"><div class="inner"><span>get-attr</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-get-item"><div class="inner"><span>get-item</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-has-attr.3F"><div class="inner"><span>has-attr?</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-has-item.3F"><div class="inner"><span>has-item?</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-import-as"><div class="inner"><span>import-as</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-import-module"><div class="inner"><span>import-module</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-initialize.21"><div class="inner"><span>initialize!</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-is-instance.3F"><div class="inner"><span>is-instance?</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-make-callable"><div class="inner"><span>make-callable</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-make-fastcallable"><div class="inner"><span>make-fastcallable</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-make-instance-fn"><div class="inner"><span>make-instance-fn</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-make-kw-instance-fn"><div class="inner"><span>make-kw-instance-fn</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-module-dict"><div class="inner"><span>module-dict</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-path-.3Epy-obj"><div class="inner"><span>path->py-obj</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-py*"><div class="inner"><span>py*</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-py**"><div class="inner"><span>py**</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-py."><div class="inner"><span>py.</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-py.-"><div class="inner"><span>py.-</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-py.."><div class="inner"><span>py..</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-python-type"><div class="inner"><span>python-type</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-run-simple-string"><div class="inner"><span>run-simple-string</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-set-attr.21"><div class="inner"><span>set-attr!</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-set-attrs.21"><div class="inner"><span>set-attrs!</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-set-item.21"><div class="inner"><span>set-item!</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-set-items.21"><div class="inner"><span>set-items!</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-stack-resource-context"><div class="inner"><span>stack-resource-context</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-with"><div class="inner"><span>with</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-with-gil"><div class="inner"><span>with-gil</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-with-gil-stack-rc-context"><div class="inner"><span>with-gil-stack-rc-context</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-with-manual-gil"><div class="inner"><span>with-manual-gil</span></div></a></li><li class="depth-1"><a href="libpython-clj2.python.html#var-with-manual-gil-stack-rc-context"><div class="inner"><span>with-manual-gil-stack-rc-context</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">libpython-clj2.python</h1><div class="doc"><div class="markdown"><p>Python bindings for Clojure. This library dynamically finds the installed
python, loads the shared library and allows Clojure users to use Python modules
as if they were Clojure namespaces.</p>
<p>Example:</p>
<pre><code class="language-clojure">user> (require '[libpython-clj2.python :as py])
nil
user> (py/initialize!)
;; ... (logging)
:ok
user> (def np (py/import-module "numpy"))
#'user/np
user> (py/py. np linspace 2 3 :num 10)
[2. 2.11111111 2.22222222 2.33333333 2.44444444 2.55555556
2.66666667 2.77777778 2.88888889 3. ]
</code></pre>
</div></div><div class="public anchor" id="var-.24a"><h3>$a</h3><h4 class="type">macro</h4><div class="usage"><code>($a item attr & args)</code></div><div class="doc"><div class="markdown"><p>Call an attribute of an object using automatic detection of the python kwargs.
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.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L630">view source</a></div></div><div class="public anchor" id="var-.24c"><h3>$c</h3><h4 class="type">macro</h4><div class="usage"><code>($c item & args)</code></div><div class="doc"><div class="markdown"><p>Call an object using automatic detection of the python kwargs.
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.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L641">view source</a></div></div><div class="public anchor" id="var--.3Ejvm"><h3>->jvm</h3><div class="usage"><code>(->jvm v & [opts])</code></div><div class="doc"><div class="markdown"><p>Copy a python value into java datastructures</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L335">view source</a></div></div><div class="public anchor" id="var--.3Epy-dict"><h3>->py-dict</h3><div class="usage"><code>(->py-dict v)</code></div><div class="doc"><div class="markdown"><p>Copy v into a python dict</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L439">view source</a></div></div><div class="public anchor" id="var--.3Epy-list"><h3>->py-list</h3><div class="usage"><code>(->py-list v)</code></div><div class="doc"><div class="markdown"><p>Copy the data into a python list</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L427">view source</a></div></div><div class="public anchor" id="var--.3Epy-tuple"><h3>->py-tuple</h3><div class="usage"><code>(->py-tuple v)</code></div><div class="doc"><div class="markdown"><p>Copy v into a python tuple</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L433">view source</a></div></div><div class="public anchor" id="var--.3Epython"><h3>->python</h3><div class="usage"><code>(->python v)</code></div><div class="doc"><div class="markdown"><p>Copy a jvm value into a python object</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L323">view source</a></div></div><div class="public anchor" id="var-add-module"><h3>add-module</h3><div class="usage"><code>(add-module modname)</code></div><div class="doc"><div class="markdown"><p>Add a python module. This can create a module if it doesn't exist.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L237">view source</a></div></div><div class="public anchor" id="var-afn"><h3>afn</h3><div class="usage"><code>(afn item attr & args)</code></div><div class="doc"><div class="markdown"><p>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.</p>
<p>Not having an argument after a keyword is an error.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L573">view source</a></div></div><div class="public anchor" id="var-as-jvm"><h3>as-jvm</h3><div class="usage"><code>(as-jvm v & [opts])</code></div><div class="doc"><div class="markdown"><p>Copy a python value into java datastructures</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L341">view source</a></div></div><div class="public anchor" id="var-as-list"><h3>as-list</h3><div class="usage"><code>(as-list pobj)</code></div><div class="doc"><div class="markdown"><p>Make a python object appear as a list</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L353">view source</a></div></div><div class="public anchor" id="var-as-map"><h3>as-map</h3><div class="usage"><code>(as-map pobj)</code></div><div class="doc"><div class="markdown"><p>Make a python object appear as a map of it's items</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L347">view source</a></div></div><div class="public anchor" id="var-as-python"><h3>as-python</h3><div class="usage"><code>(as-python v)</code></div><div class="doc"><div class="markdown"><p>Bridge a jvm value into a python object</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L329">view source</a></div></div><div class="public anchor" id="var-attr-type-map"><h3>attr-type-map</h3><div class="usage"><code>(attr-type-map pyobj)</code></div><div class="doc"><div class="markdown"><p>Return a map of attr name to python-type of the attribute</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L367">view source</a></div></div><div class="public anchor" id="var-call-attr"><h3>call-attr</h3><div class="usage"><code>(call-attr pyobj attname & args)</code></div><div class="doc"><div class="markdown"><p>Call an attribute on a python object using only positional arguments</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L260">view source</a></div></div><div class="public anchor" id="var-call-attr-kw"><h3>call-attr-kw</h3><div class="usage"><code>(call-attr-kw pyobj attname args kw-list)</code></div><div class="doc"><div class="markdown"><p>Call an attribute passing in both positional and keyword arguments.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L266">view source</a></div></div><div class="public anchor" id="var-callable.3F"><h3>callable?</h3><div class="usage"><code>(callable? pyobj)</code></div><div class="doc"><div class="markdown"><p>Return true if python object is callable.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L410">view source</a></div></div><div class="public anchor" id="var-cfn"><h3>cfn</h3><div class="usage"><code>(cfn item & args)</code></div><div class="doc"><div class="markdown"><p>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.</p>
<p>Not having an argument after a keyword argument is an error.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L562">view source</a></div></div><div class="public anchor" id="var-create-class"><h3>create-class</h3><div class="usage"><code>(create-class name bases cls-hashmap)</code></div><div class="doc"><div class="markdown"><p>Create a new class object. Any callable values in the cls-hashmap
will be presented as instance methods. If you want access to the
'this' object then you must use <code>make-instance-fn</code>.</p>
<p>Example:</p>
<pre><code class="language-clojure">user> (require '[libpython-clj2.python :as py])
nil
user> (def cls-obj (py/create-class
"myfancyclass"
nil
{"__init__" (py/make-instance-fn
(fn [this arg]
(py/set-attr! this "arg" arg)
;;If you don't return nil from __init__ that is an
;;error.
nil))
"addarg" (py/make-instance-fn
(fn [this otherarg]
(+ (py/get-attr this "arg")
otherarg)))}))
#'user/cls-obj
user> cls-obj
__no_module__.myfancyclass
user> (def inst (cls-obj 10))
#'user/inst
user> (py/call-attr inst "addarg" 10)
20
</code></pre>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L527">view source</a></div></div><div class="public anchor" id="var-def-unpack"><h3>def-unpack</h3><h4 class="type">macro</h4><div class="usage"><code>(def-unpack symbols input)</code></div><div class="doc"><div class="markdown"><p>Unpack a set of symbols into a set of defs. Useful when trying to match Python
idioms - this is definitely not idiomatic Clojure.</p>
<p>Example:</p>
<pre><code class="language-clojure">user> (py/def-unpack [a b c] (py/->py-tuple [1 2 3]))
#'user/c
user> a
1
user> b
2
user> c
3
</code></pre>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L803">view source</a></div></div><div class="public anchor" id="var-dir"><h3>dir</h3><div class="usage"><code>(dir pyobj)</code></div><div class="doc"><div class="markdown"></div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L255">view source</a></div></div><div class="public anchor" id="var-from-import"><h3>from-import</h3><h4 class="type">macro</h4><div class="usage"><code>(from-import module-path item & args)</code></div><div class="doc"><div class="markdown"><p>Support for the from a import b,c style of importing modules and symbols in python.
Documentation is included.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L384">view source</a></div></div><div class="public anchor" id="var-get-attr"><h3>get-attr</h3><div class="usage"><code>(get-attr pyobj attname)</code></div><div class="doc"><div class="markdown"><p>Get an attribute from a python object</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L271">view source</a></div></div><div class="public anchor" id="var-get-item"><h3>get-item</h3><div class="usage"><code>(get-item pyobj item-name)</code></div><div class="doc"><div class="markdown"><p>Get an item from a python object using <strong>getitem</strong></p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L297">view source</a></div></div><div class="public anchor" id="var-has-attr.3F"><h3>has-attr?</h3><div class="usage"><code>(has-attr? pyobj att-name)</code></div><div class="doc"><div class="markdown"><p>Return true if this python object has this attribute.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L291">view source</a></div></div><div class="public anchor" id="var-has-item.3F"><h3>has-item?</h3><div class="usage"><code>(has-item? pyobj item-name)</code></div><div class="doc"><div class="markdown"><p>Return true if the python object has an item. Calls <strong>hasitem</strong>.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L310">view source</a></div></div><div class="public anchor" id="var-import-as"><h3>import-as</h3><h4 class="type">macro</h4><div class="usage"><code>(import-as module-path varname)</code></div><div class="doc"><div class="markdown"><p>Import a module and assign it to a var. Documentation is included.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L375">view source</a></div></div><div class="public anchor" id="var-import-module"><h3>import-module</h3><div class="usage"><code>(import-module modname)</code></div><div class="doc"><div class="markdown"><p>Import a python module. Module entries can be accessed via get-attr.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L227">view source</a></div></div><div class="public anchor" id="var-initialize.21"><h3>initialize!</h3><div class="usage"><code>(initialize! & {:keys [windows-anaconda-activate-bat library-path no-io-redirect?], :as options})</code></div><div class="doc"><div class="markdown"><p>Initialize the python library. If library path is not provided, then the system
attempts to execute a simple python program and have python return system info.</p>
<p>Note: all of the options passed to <code>initialize!</code> may now be provided in
a root-level <code>python.edn</code> file. Example:</p>
<pre><code>;; python.edn
{:python-executable "/usr/bin/python3.7"
:python-library-path "/usr/lib/libpython3.7m.so"
:python-home "/usr/lib/python3.7"
:python-verbose true}
</code></pre>
<p>or, using a local virtual environment:</p>
<pre><code>;; python.edn
{:python-executable "env/bin/python"}
</code></pre>
<p>Additionaly the file can contain two keys which can can refer to custom hooks
to run code just before and just after python is initialised.
Typical use case for this is to setup / verify the python virtual enviornment
to be used.</p>
<pre><code>:pre-initialize-fn my-ns/my-venv-setup-fn!
:post-initialize-fn my-ns/my-venv-validate-fn!
</code></pre>
<p>A :pre-initialize-fn could for example shell out and setup a python
virtual enviornment.</p>
<p>The :post-initialize-fn can use all functions from ns <code>libpython-clj2.python</code>
as libpython-clj is initialised alreday and could for example be used to validate
that later needed libraries can be loaded via calling <code>import-module</code>.</p>
<p>The file MUST be named <code>python.edn</code> and be in the root of the classpath.
With a <code>python.edn</code> file in place, the <code>initialize!</code> function may be called
with no arguments and the options will be read from the file. If arguments are
passed to <code>initialize!</code> then they will override the values in the file.</p>
<p>Returns either <code>:ok</code> in which case the initialization completed successfully or
<code>:already-initialized</code> in which case we detected that python has already been
initialized via <code>Py_IsInitialized</code> and we do nothing more.</p>
<p>Options:</p>
<ul>
<li><code>:library-path</code> - Library path of the python library to use.</li>
<li><code>:program-name</code> - Optional -- will show up in error messages from python.</li>
<li><code>:no-io-redirect?</code> - True if you don't want python stdout and stderr redirection
to <em>out</em> and <em>err</em>.</li>
<li><code>:python-executable</code> - The python executable to use to find system information.</li>
<li><code>:python-home</code> - Python home directory. The system first uses this variable, then
the environment variable PYTHON_HOME, and finally information returned from
python system info.</li>
<li><code>:signals?</code> - defaults to false - true if you want python to initialized signals.
Be aware that the JVM itself uses quite a few signals - SIGSEGV, for instance -
during it's normal course of operation. For more information see:
<ul>
<li><a href="https://docs.oracle.com/javase/10/troubleshoot/handle-signals-and-exceptions.htm#JSTGD356">used signals</a></li>
<li><a href="https://docs.oracle.com/javase/8/docs/technotes/guides/vm/signal-chaining.html">signal-chaining</a></li>
</ul>
</li>
</ul>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L45">view source</a></div></div><div class="public anchor" id="var-is-instance.3F"><h3>is-instance?</h3><div class="usage"><code>(is-instance? py-inst py-cls)</code></div><div class="doc"><div class="markdown"><p>Return true if inst is an instance of cls. Note that arguments
are reversed as compared to <code>instance?</code></p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L398">view source</a></div></div><div class="public anchor" id="var-make-callable"><h3>make-callable</h3><div class="usage"><code>(make-callable ifn options)</code><code>(make-callable ifn)</code></div><div class="doc"><div class="markdown"><p>Make a python callable object from a clojure function. This is called for you
if you use <code>as-python</code> on an implementation of IFn.</p>
<p>Options:</p>
<ul>
<li><code>:arg-converter</code> - Function called for each function argument before your ifn
gets access to it. Defaults to <code>->jvm</code>.</li>
<li><code>:result-converter</code> - Function called on return value before it gets returned to
python. Must return a python object. Defaults to <code>->python</code>; the result will
get an extra incref before being returned to Python to account for the implied
tracking of <code>as-python</code> or <code>->python</code>.</li>
<li><code>:name</code> - Name of the python method. This will appear in stack traces.</li>
<li><code>:doc</code> - documentation for method.</li>
</ul>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L478">view source</a></div></div><div class="public anchor" id="var-make-fastcallable"><h3>make-fastcallable</h3><div class="usage"><code>(make-fastcallable item)</code></div><div class="doc"><div class="markdown"><p>Wrap a python callable such that calling it in a tight loop with purely positional
arguments is a bit (2x-3x) faster.</p>
<p>Example:</p>
<pre><code class="language-clojure">user> (def test-fn (-> (py/run-simple-string "def spread(bid,ask):
return bid-ask
")
(get :globals)
(get "spread")))
#'user/test-fn
user> test-fn
<function spread at 0x7f330c046040>
user> (py/with-gil (time (dotimes [iter 10000]
(test-fn 1 2))))
"Elapsed time: 85.140418 msecs"
nil
user> (py/with-gil (time (dotimes [iter 10000]
(test-fn 1 2))))
"Elapsed time: 70.894275 msecs"
nil
user> (with-open [test-fn (py/make-fastcallable test-fn)]
(py/with-gil (time (dotimes [iter 10000]
(test-fn 1 2)))))
"Elapsed time: 39.442622 msecs"
nil
user> (with-open [test-fn (py/make-fastcallable test-fn)]
(py/with-gil (time (dotimes [iter 10000]
(test-fn 1 2)))))
"Elapsed time: 35.492965 msecs"
nil
</code></pre>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L584">view source</a></div></div><div class="public anchor" id="var-make-instance-fn"><h3>make-instance-fn</h3><div class="usage"><code>(make-instance-fn ifn options)</code><code>(make-instance-fn ifn)</code></div><div class="doc"><div class="markdown"><p>Make an callable instance function - a function which will be passed the 'this'
object as it's first argument. In addition, this function calls <code>make-callable</code>
with a <code>arg-converter</code> defaulted to <code>as-jvm</code>. See documentation for
<a href="libpython-clj2.python.html#var-make-callable">make-callable</a> and <a href="libpython-clj2.python.class.html#var-make-tuple-instance-fn">libpython-clj2.python.class/make-tuple-instance-fn</a>.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L502">view source</a></div></div><div class="public anchor" id="var-make-kw-instance-fn"><h3>make-kw-instance-fn</h3><div class="usage"><code>(make-kw-instance-fn ifn options)</code><code>(make-kw-instance-fn ifn)</code></div><div class="doc"><div class="markdown"><p>Make an kw callable instance function - function by default is passed 2 arguments,
the positional argument vector and a map of keyword arguments. Results are marshalled
back to python using <a href="libpython-clj2.python.fn-bridged-fn-arg--python">libpython-clj2.python.fn/bridged-fn-arg->python</a> which is also
used when bridging an object into python. See documentation for <a href="libpython-clj2.python.html#var-make-callable">make-callable</a>
<a href="libpython-clj2.python.class.html#var-make-kw-instance-fn">libpython-clj2.python.class/make-kw-instance-fn</a>.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L511">view source</a></div></div><div class="public anchor" id="var-module-dict"><h3>module-dict</h3><div class="usage"><code>(module-dict mod)</code></div><div class="doc"><div class="markdown"><p>Get the module dictionary.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L246">view source</a></div></div><div class="public anchor" id="var-path-.3Epy-obj"><h3>path->py-obj</h3><div class="usage"><code>(path->py-obj item-path & {:keys [reload?]})</code></div><div class="doc"><div class="markdown"><p>Given a string such as "builtins" or "builtins.list", load the module or
the class object in the module.</p>
<p>Options:</p>
<ul>
<li><code>:reload</code> - Reload the module.</li>
</ul>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L778">view source</a></div></div><div class="public anchor" id="var-py*"><h3>py*</h3><h4 class="type">macro</h4><div class="usage"><code>(py* x method args)</code><code>(py* x method args kwargs)</code></div><div class="doc"><div class="markdown"><p>Special syntax for passing along *args and **kwargs style arguments
to methods.</p>
<p>Usage:</p>
<p>(py* obj method args kwargs)</p>
<p>Example:</p>
<p>(def d (python/dict))
d ;;=> {}
(def iterable [<a href=":a 1">:a 1</a> <a href=":b 2">:b 2</a>])
(def kwargs {:cat "dog" :name "taco"})
(py* d update <a href="iterable">iterable</a> kwargs)
d ;;=> {"a": 1, "b": 2, "cat": "dog", "name": "taco"}</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L667">view source</a></div></div><div class="public anchor" id="var-py**"><h3>py**</h3><h4 class="type">macro</h4><div class="usage"><code>(py** x method kwargs)</code><code>(py** x method arg & args)</code></div><div class="doc"><div class="markdown"><p>Like py*, but it is assumed that the LAST argument is kwargs.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L689">view source</a></div></div><div class="public anchor" id="var-py."><h3>py.</h3><h4 class="type">macro</h4><div class="usage"><code>(py. x method-name & args)</code></div><div class="doc"><div class="markdown"><p>Class/object method syntax. (py. obj method arg1 arg2 ... argN)
is equivalent to Python's obj.method(arg1, arg2, ..., argN) syntax.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L658">view source</a></div></div><div class="public anchor" id="var-py.-"><h3>py.-</h3><h4 class="type">macro</h4><div class="usage"><code>(py.- x arg)</code></div><div class="doc"><div class="markdown"><p>Class/object getter syntax. (py.- obj attr) is equivalent to
Python's obj.attr syntax.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L651">view source</a></div></div><div class="public anchor" id="var-py.."><h3>py..</h3><h4 class="type">macro</h4><div class="usage"><code>(py.. x & args)</code></div><div class="doc"><div class="markdown"><p>Extended accessor notation, similar to the <code>..</code> macro in Clojure.</p>
<p>(require-python 'sys)
(py.. sys -path (append "/home/user/bin"))</p>
<p>is equivalent to Python's</p>
<p>import sys
sys.path.append('/home/user/bin')</p>
<p>SPECIAL SYNTAX for programmatic *args and **kwargs</p>
<p>Special syntax is provided to meet the needs required by
Python's *args and **kwargs syntax programmatically.</p>
<p>(= (py.. obj (<em>method args))
(py</em> obj methods args))</p>
<p>(= (py.. obj (<em>method args kwargs))
(py</em> obj method args kwargs))</p>
<p>(= (py.. obj (<strong>method kwargs))
(py</strong> obj method kwargs))</p>
<p>(= (py.. obj (<strong>method arg1 arg2 arg3 ... argN kwargs))
(py</strong> obj method arg1 arg2 arg3 ... argN kwargs)
(py* obj method <a href="arg1 arg2 arg3 ... argN">arg1 arg2 arg3 ... argN</a> kwargs))</p>
<p>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.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L724">view source</a></div></div><div class="public anchor" id="var-python-type"><h3>python-type</h3><div class="usage"><code>(python-type v)</code></div><div class="doc"><div class="markdown"><p>Get the type (as a keyword) of a python object</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L359">view source</a></div></div><div class="public anchor" id="var-run-simple-string"><h3>run-simple-string</h3><div class="usage"><code>(run-simple-string program & {:keys [globals locals]})</code></div><div class="doc"><div class="markdown"><p>Run a string expression returning a map of
{:globals :locals}.
This uses the global <strong>main</strong> dict under the covers so it matches the behavior
of the cpython implementation with the exception of returning the various maps
used.</p>
<p>Note this will never return the result of the expression:
<a href="https://mail.python.org/pipermail/python-list/1999-April/018011.html">https://mail.python.org/pipermail/python-list/1999-April/018011.html</a></p>
<p>Globals, locals may be provided but are not necessary.</p>
<p>Implemented in cpython as:</p>
<p>PyObject *m, *d, *v;
m = PyImport_AddModule("<strong>main</strong>");
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;</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L445">view source</a></div></div><div class="public anchor" id="var-set-attr.21"><h3>set-attr!</h3><div class="usage"><code>(set-attr! pyobj attname attval)</code></div><div class="doc"><div class="markdown"><p>Set an attribute on a python object. Returns pyobj.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L277">view source</a></div></div><div class="public anchor" id="var-set-attrs.21"><h3>set-attrs!</h3><div class="usage"><code>(set-attrs! pyobj att-seq)</code></div><div class="doc"><div class="markdown"><p>Set a sequence of <a href="name value">name value</a> attributes. Returns pyobj.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L284">view source</a></div></div><div class="public anchor" id="var-set-item.21"><h3>set-item!</h3><div class="usage"><code>(set-item! pyobj item-name item-val)</code></div><div class="doc"><div class="markdown"><p>Set an item on a python object using <strong>setitem</strong></p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L303">view source</a></div></div><div class="public anchor" id="var-set-items.21"><h3>set-items!</h3><div class="usage"><code>(set-items! pyobj item-seq)</code></div><div class="doc"><div class="markdown"><p>Set a sequence of <a href="name value">name value</a>. Returns pyobj</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L316">view source</a></div></div><div class="public anchor" id="var-stack-resource-context"><h3>stack-resource-context</h3><h4 class="type">macro</h4><div class="usage"><code>(stack-resource-context & body)</code></div><div class="doc"><div class="markdown"><p>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.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L156">view source</a></div></div><div class="public anchor" id="var-with"><h3>with</h3><h4 class="type">macro</h4><div class="usage"><code>(with bind-vec & body)</code></div><div class="doc"><div class="markdown"><p>Support for the 'with' statement in python:
(py/with <a href="item (py/call-attr testcode-module "WithObjClass" true fn-list)">item (py/call-attr testcode-module "WithObjClass" true fn-list)</a>
(py/call-attr item "doit_err"))</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L622">view source</a></div></div><div class="public anchor" id="var-with-gil"><h3>with-gil</h3><h4 class="type">macro</h4><div class="usage"><code>(with-gil & body)</code></div><div class="doc"><div class="markdown"><p>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 fine grained
grabbing/releasing of the mutex.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L169">view source</a></div></div><div class="public anchor" id="var-with-gil-stack-rc-context"><h3>with-gil-stack-rc-context</h3><h4 class="type">macro</h4><div class="usage"><code>(with-gil-stack-rc-context & body)</code></div><div class="doc"><div class="markdown"><p>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 <code>stack-resource-context</code> 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.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L178">view source</a></div></div><div class="public anchor" id="var-with-manual-gil"><h3>with-manual-gil</h3><h4 class="type">macro</h4><div class="usage"><code>(with-manual-gil & body)</code></div><div class="doc"><div class="markdown"><p>When running with -Dlibpython_clj.manual_gil=true, you need to wrap all accesses to
the python runtime with this locker. This includes calls to require-python or any other
pathways.</p>
<pre><code class="language-clojure"> (with-manual-gil
...)
</code></pre>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L192">view source</a></div></div><div class="public anchor" id="var-with-manual-gil-stack-rc-context"><h3>with-manual-gil-stack-rc-context</h3><h4 class="type">macro</h4><div class="usage"><code>(with-manual-gil-stack-rc-context & body)</code></div><div class="doc"><div class="markdown"><p>When running with -Dlibpython_clj.manual_gil=true, you need to wrap all accesses to
the python runtime with this locker. This includes calls to require-python or any other
pathways. This macro furthermore defines a stack-based gc context to immediately release
objects when the stack frame exits.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python.clj#L207">view source</a></div></div></div></body></html>