-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
libpython-clj2.require.html
116 lines (115 loc) · 9.74 KB
/
libpython-clj2.require.html
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
<!DOCTYPE html PUBLIC ""
"">
<html><head><meta charset="UTF-8" /><title>libpython-clj2.require 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.025</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"><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 current"><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.require.html#var-import-python"><div class="inner"><span>import-python</span></div></a></li><li class="depth-1"><a href="libpython-clj2.require.html#var-require-python"><div class="inner"><span>require-python</span></div></a></li></ul></div><div class="namespace-docs" id="content"><h1 class="anchor" id="top">libpython-clj2.require</h1><div class="doc"><div class="markdown"><p>Namespace implementing requiring python modules as Clojure namespaces. This works via
scanning the module for metadata and dynamically building the Clojure namespace.</p>
</div></div><div class="public anchor" id="var-import-python"><h3>import-python</h3><div class="usage"><code>(import-python)</code></div><div class="doc"><div class="markdown"><p>Loads python, python.list, python.dict, python.set, python.tuple,
and python.frozenset.</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/require.clj#L371">view source</a></div></div><div class="public anchor" id="var-require-python"><h3>require-python</h3><div class="usage"><code>(require-python req)</code><code>(require-python req & reqs)</code></div><div class="doc"><div class="markdown"><h2>Basic usage</h2>
<p>(require-python 'math)
(math/sin 1.0) ;;=> 0.8414709848078965</p>
<p>(require-python '<a href="math :as maaaath">math :as maaaath</a>)</p>
<p>(maaaath/sin 1.0) ;;=> 0.8414709848078965</p>
<p>(require-python 'math 'csv)
(require-python '<a href="math :as pymath">math :as pymath</a> 'csv))
(require-python '<a href="math :as pymath">math :as pymath</a> '<a href="csv :as py-csv">csv :as py-csv</a>)
(require-python 'concurrent.futures)
(require-python '<a href="concurrent.futures :as fs">concurrent.futures :as fs</a>)
(require-python '(concurrent <a href="futures :as fs">futures :as fs</a>))</p>
<p>(require-python '[requests :refer <a href="get post">get post</a>])</p>
<p>(requests/get "https//<a href="http://www.google.com">www.google.com</a>") ;;=> <Response <a href="200">200</a>>
(get "https//<a href="http://www.google.com">www.google.com</a>") ;;=> <Response <a href="200">200</a>></p>
<p>In some cases we may generate invalid arglists metadata for the clojure compiler.
In those cases we have a flag, :no-arglists that will disable adding arglists to
the generated metadata for the vars. Use the reload flag below if you need to
force reload a namespace where invalid arglists have been generated.</p>
<p>(require-python '[numpy :refer <a href="linspace">linspace</a> :no-arglists :as np])</p>
<p>If you would like to bind the Python module to the namespace, use
the :bind-ns flag.</p>
<p>(require-python '<a href="requests :bind-ns true">requests :bind-ns true</a>) or
(require-python '<a href="requests :bind-ns">requests :bind-ns</a>)</p>
<h2>Use with custom modules</h2>
<p>For use with a custom namespace foo.py while developing, you can
use:</p>
<p>(require-python '<a href="foo :reload">foo :reload</a>)</p>
<p>NOTE: unless you specify the :reload flag,
..: the module will NOT reload. If the :reload flag is set,
..: the behavior mimics importlib.reload</p>
<h2>Setting up classpath for custom modules</h2>
<p>Note: you may need to setup your PYTHONPATH correctly.
<strong>WARNING</strong>: This is very handy for local REPL development,
..: if you are going to AOT classes,
..: refer to the documentation on codegen
..: or your AOT compilation will fail.
If your foo.py lives at /path/to/foodir/foo.py, the easiest
way to do it is:</p>
<p>(require-python :from "/path/to/foodir"
'foo) ;; or
(require-python "/path/to/foodir"
'foo) ;; or
(require-python {:from "/path/to/foodir"}
'foo)</p>
<p>as you prefer.</p>
<p>Additionally, if you want to keep the namespacing as you have
it in Python, you may prefer to use a relative import
starting from a location of your choosing. If your
os.getcwd() => /some/path/foo,
and your directory structure looks something like:</p>
<p>/some $ tree
.
└── path
├── baz
│ └── quux.py
└── foo
└── bar.py</p>
<p>(require-python :from "path"
'<a href="baz.quux :as quux">baz.quux :as quux</a>
:from "path/foo"
'bar)</p>
<p>is perfectly acceptable. It probably makes the most
sense to keep you style consistent, but you can mix
and match as you see fit between <path>, :from <path>,
and {:from <path>}. <path> can either be a file or a
directory. If it is a file, the Python path will be
set to the directory containing that file.</path></path></path></path></p>
<p>You may also stack several require-pythons under one path:</p>
<p>(require-python {:from "dir-a"}
'a
'b
'c
{:from "dir-b"}
'e.f
'g
{:from "dir-c}
'hi.there)</p>
<p>Other options more in keeping with traditional PYTHONPATH
management include:</p>
<p>(require-python 'sys)
(py/call-attr (py/get-attr sys "path")
"append"
"/path/to/foodir")</p>
<p>Another option is</p>
<p>(require-python 'os)
(os/chdir "/path/to/foodir")</p>
<h2>prefix lists</h2>
<p>For convenience, if you are loading multiple Python modules
with the same prefix, you can use the following notation:</p>
<p>(require-python '(a b c))
is equivalent to
(require-python 'a.b 'a.c)</p>
<p>(require-python '(foo [bar :as baz :refer <a href="qux">qux</a>]))
is equivalent to
(require-python '[foo.bar :as baz :refer <a href="qux">qux</a>])</p>
<p>(require-python '(foo [bar :as baz :refer <a href="qux">qux</a>] buster))
(require-python '[foo.bar :as baz :refer <a href="qux">qux</a>] 'foo.buster))</p>
<h2>For library developers</h2>
<p>If you want to intern all symbols to your current namespace,
you can do the following --</p>
<p>(require-python '<a href="math :refer :all">math :refer :all</a>)</p>
<p>However, if you only want to use
those things designated by the module under the <strong>all</strong> attribute,
you can do</p>
<p>(require-python '<a href="operators :refer :*">operators :refer :*</a>)</p>
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/require.clj#L182">view source</a></div></div></div></body></html>