Skip to content

Commit 073a887

Browse files
committed
Recording how to call super.init().
1 parent b25f366 commit 073a887

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

docs/libpython-clj2.python.class.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
will be presented as instance methods.
1111
Things in the cls hashmap had better be either atoms or already converted
1212
python objects. You may get surprised otherwise; you have been warned.
13-
See the classes-test file in test/libpython-clj</p>
13+
See the classes-test file in test/libpython-clj.</p>
14+
<p>Calling <code>super.init()</code> may be done in a non-obvious way:</p>
15+
<pre><code class="language-clojure">(py. (py/get-item (py.. self -__class__ -__mro__) 1) __init__ self)
16+
</code></pre>
17+
<p>More feedback/research in this area is needed to integrated deeper into
18+
the python class hierarchies.</p>
1419
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/python/class.clj#L70">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 clj-fn &amp; [{:keys [arg-converter result-converter], :or {arg-converter py-base/as-jvm}, :as options}])</code></div><div class="doc"><div class="markdown"><p>Make an instance function - a function which will be passed the 'this' object as
1520
it's first argument. In this case the default behavior is to
1621
pass as-jvm bridged python object ptr args and kw dict args to the clojure function without

docs/libpython-clj2.python.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
<p>A :pre-initialize-fn could for example shell out and setup a python
124124
virtual enviornment.</p>
125125
<p>The :post-initialize-fn can use all functions from ns <code>libpython-clj2.python</code>
126-
as libpython-clj is initialised alreday andc ould for example be used to validate
126+
as libpython-clj is initialised alreday and could for example be used to validate
127127
that later needed libraries can be loaded via calling <code>import-module</code>.</p>
128128
<p>The file MUST be named <code>python.edn</code> and be in the root of the classpath.
129129
With a <code>python.edn</code> file in place, the <code>initialize!</code> function may be called

docs/libpython-clj2.require.html

+49-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
scanning the module for metadata and dynamically building the Clojure namespace.</p>
99
</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,
1010
and python.frozenset.</p>
11-
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/require.clj#L284">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 &amp; reqs)</code></div><div class="doc"><div class="markdown"><h2>Basic usage</h2>
11+
</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 &amp; reqs)</code></div><div class="doc"><div class="markdown"><h2>Basic usage</h2>
1212
<p>(require-python 'math)
1313
(math/sin 1.0) ;;=&gt; 0.8414709848078965</p>
1414
<p>(require-python '<a href="math :as maaaath">math :as maaaath</a>)</p>
@@ -40,8 +40,53 @@ <h2>Use with custom modules</h2>
4040
..: the behavior mimics importlib.reload</p>
4141
<h2>Setting up classpath for custom modules</h2>
4242
<p>Note: you may need to setup your PYTHONPATH correctly.
43-
One technique to do this is, if your foo.py lives at
44-
/path/to/foodir/foo.py:</p>
43+
<strong>WARNING</strong>: This is very handy for local REPL development,
44+
..: if you are going to AOT classes,
45+
..: refer to the documentation on codegen
46+
..: or your AOT compilation will fail.
47+
If your foo.py lives at /path/to/foodir/foo.py, the easiest
48+
way to do it is:</p>
49+
<p>(require-python :from "/path/to/foodir"
50+
'foo) ;; or
51+
(require-python "/path/to/foodir"
52+
'foo) ;; or
53+
(require-python {:from "/path/to/foodir"}
54+
'foo)</p>
55+
<p>as you prefer.</p>
56+
<p>Additionally, if you want to keep the namespacing as you have
57+
it in Python, you may prefer to use a relative import
58+
starting from a location of your choosing. If your
59+
os.getcwd() =&gt; /some/path/foo,
60+
and your directory structure looks something like:</p>
61+
<p>/some $ tree
62+
.
63+
└── path
64+
├── baz
65+
│ └── quux.py
66+
└── foo
67+
└── bar.py</p>
68+
<p>(require-python :from "path"
69+
'<a href="baz.quux :as quux">baz.quux :as quux</a>
70+
:from "path/foo"
71+
'bar)</p>
72+
<p>is perfectly acceptable. It probably makes the most
73+
sense to keep you style consistent, but you can mix
74+
and match as you see fit between <path>, :from <path>,
75+
and {:from <path>}. <path> can either be a file or a
76+
directory. If it is a file, the Python path will be
77+
set to the directory containing that file.</path></path></path></path></p>
78+
<p>You may also stack several require-pythons under one path:</p>
79+
<p>(require-python {:from "dir-a"}
80+
'a
81+
'b
82+
'c
83+
{:from "dir-b"}
84+
'e.f
85+
'g
86+
{:from "dir-c}
87+
'hi.there)</p>
88+
<p>Other options more in keeping with traditional PYTHONPATH
89+
management include:</p>
4590
<p>(require-python 'sys)
4691
(py/call-attr (py/get-attr sys "path")
4792
"append"
@@ -68,4 +113,4 @@ <h2>For library developers</h2>
68113
those things designated by the module under the <strong>all</strong> attribute,
69114
you can do</p>
70115
<p>(require-python '<a href="operators :refer :*">operators :refer :*</a>)</p>
71-
</div></div><div class="src-link"><a href="https://github.com/clj-python/libpython-clj/blob/master/src/libpython_clj2/require.clj#L173">view source</a></div></div></div></body></html>
116+
</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>

src/libpython_clj2/python/class.clj

+10-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@
7272
will be presented as instance methods.
7373
Things in the cls hashmap had better be either atoms or already converted
7474
python objects. You may get surprised otherwise; you have been warned.
75-
See the classes-test file in test/libpython-clj"
75+
See the classes-test file in test/libpython-clj.
76+
77+
78+
Calling `super.init()` may be done in a non-obvious way:
79+
80+
```clojure
81+
(py. (py/get-item (py.. self -__class__ -__mro__) 1) __init__ self)
82+
```
83+
More feedback/research in this area is needed to integrated deeper into
84+
the python class hierarchies."
7685
[name bases cls-hashmap]
7786
(py-ffi/with-gil
7887
(py-ffi/with-decref

0 commit comments

Comments
 (0)