Skip to content

Commit 98a2df2

Browse files
author
Christopher Doris
committed
better docs, juliaaa items now searchable
1 parent 6bf7575 commit 98a2df2

File tree

4 files changed

+151
-179
lines changed

4 files changed

+151
-179
lines changed

docs/make.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
using Documenter, Python
1+
using Documenter, Python, Markdown
2+
3+
struct CustomCat{cat} end
4+
Documenter.Utilities.doccat(::Base.Docs.Binding, ::Type{CustomCat{cat}}) where {cat} = string(cat)
5+
struct CustomDocBlocks <: Documenter.Expanders.ExpanderPipeline end
6+
Documenter.Expanders.Selectors.order(::Type{CustomDocBlocks}) = 20.0
7+
Documenter.Expanders.Selectors.matcher(::Type{CustomDocBlocks}, node, page, doc) = Documenter.Expanders.iscode(node, "@customdoc")
8+
Documenter.Expanders.Selectors.runner(::Type{CustomDocBlocks}, x, page, doc) = begin
9+
header, rest = split(x.code, "\n", limit=2)
10+
docstr = Markdown.parse(rest)
11+
name, cat = split(header, "-", limit=2)
12+
binding = Docs.Binding(Main, Symbol(strip(name)))
13+
object = Documenter.Utilities.Object(binding, CustomCat{Symbol(strip(cat))})
14+
slug = Documenter.Utilities.slugify(strip(name))
15+
anchor = Documenter.Anchors.add!(doc.internal.docs, object, slug, page.build)
16+
node = Documenter.Documents.DocsNode(docstr, anchor, object, page)
17+
page.mapping[x] = node
18+
end
219

320
makedocs(
421
sitename = "Python.jl",

docs/src/conversion.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This page documents the rules used to convert values between Julia and Python.
44

55
In both directions, the default behaviour is to allow conversion between immutable values. Mutable values will be "wrapped" so that mutations on the wrapper affect the original object.
66

7-
## Julia to Python
7+
## [Julia to Python](@id jl2py)
88

99
When a Julia object is converted to a Python one (e.g. by calling `PyObject`, by interpolating it into a `@py` command, or passing it as an argument to a Python function) the following rules are used by default.
1010

@@ -33,11 +33,11 @@ The user can always explicitly choose a different conversion (e.g. by calling `p
3333
| `Type` | `juliaaa.TypeValue` |
3434
| Anything else | `juliaaa.AnyValue` |
3535

36-
The `juliaaa.*Value` types are all subtypes of `juliaaa.AnyValue`. They wrap a Julia value, providing access to Julia semantics: it can be called, indexed, and so on. Subtypes add additional Pythonic semantics. Read more [here](../juliapy/#Wrapper-types).
36+
The `juliaaa.*Value` types are all subtypes of `juliaaa.AnyValue`. They wrap a Julia value, providing access to Julia semantics: it can be called, indexed, and so on. Subtypes add additional Pythonic semantics. Read more [here](@ref julia-wrappers).
3737

3838
This conversion policy is defined/implemented by `Python.C.PyObject_From` and `Python.C.PyJuliaValue_From`. Package authors can (carefully) overload these with additional rules for custom types.
3939

40-
## Python to Julia
40+
## [Python to Julia](@id py2jl)
4141

4242
From Julia, one can convert Python objects to a desired type using `pyconvert(T, x)` for example, or ```@pyv `...`::T```.
4343

docs/src/juliapy.md

Lines changed: 125 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import juliaaa; jl = juliaaa.newmodule("SomeName");
1616

1717
Now you can do `jl.rand(jl.Bool, 5, 5)`, which is equivalent to `rand(Bool, 5, 5)` in Julia.
1818

19-
When a Python value is passed to Julia, then typically it will be converted according to [this table](../conversion/#Python-to-Julia) with `T=Any`. Sometimes a more specific type will be used, such as when assigning to an array whose element type is known.
19+
When a Python value is passed to Julia, then typically it will be converted according to [this table](@ref py2jl) with `T=Any`. Sometimes a more specific type will be used, such as when assigning to an array whose element type is known.
2020

21-
When a Julia value is returned to Python, it will normally be converted according to [this table](../conversion/#Julia-to-Python).
21+
When a Julia value is returned to Python, it will normally be converted according to [this table](@ref jl2py).
2222

23-
## Wrapper types
23+
## [Wrapper types](@id julia-wrappers)
2424

25-
Apart from a few fundamental immutable types (see [here](../conversion/#Julia-to-Python)), all Julia values are by default converted into Python to some [`AnyValue`](#juliaaa.AnyValue) object, which wraps the original value. Some types are converted to a subclass of [`AnyValue`](#juliaaa.AnyValue) which provides additional Python semantics --- e.g. Julia vectors are interpreted as Python sequences.
25+
Apart from a few fundamental immutable types (see [here](@ref jl2py)), all Julia values are by default converted into Python to some [`AnyValue`](#juliaaa.AnyValue) object, which wraps the original value. Some types are converted to a subclass of [`AnyValue`](#juliaaa.AnyValue) which provides additional Python semantics --- e.g. Julia vectors are interpreted as Python sequences.
2626

2727
There is also a [`RawValue`](#juliaaa.RawValue) object, which gives a stricter "Julia-only" interface, documented below. These types all inherit from `ValueBase`:
2828

@@ -45,174 +45,129 @@ There is also a [`RawValue`](#juliaaa.RawValue) object, which gives a stricter "
4545
- [`ModuleValue`](#juliaaa.ModuleValue)
4646
- [`TypeValue`](#juliaaa.TypeValue)
4747

48-
```@raw html
49-
<article class="docstring">
50-
<header>
51-
<a class="docstring-binding" id="juliaaa.AnyValue" href="#juliaaa.AnyValue">juliaaa.AnyValue</a>
52-
53-
<span class="docstring-category">Class</span>
54-
</header>
55-
<section>
56-
<p>Wraps any Julia object, giving it some basic Python semantics. Subtypes provide extra semantics.</p>
57-
<p>Supports <code>repr(x)</code>, <code>str(x)</code>, attributes (<code>x.attr</code>), calling (<code>x(a,b)</code>), iteration, comparisons, <code>len(x)</code>, <code>a in x</code>, <code>dir(x)</code>.</p>
58-
<p>Calling, indexing, attribute access, etc. will convert the result to a Python object according to <a href="../conversion/#Julia-to-Python">this table</a>. This is typically a builtin Python type (for immutables) or a subtype of <code>AnyValue</code>.</p>
59-
<p>Attribute access can be used to access Julia properties as well as normal class members. In the case of a name clash, the class member will take precedence. For convenience with Julia naming conventions, <code>_b</code> at the end of an attribute is replaced with <code>!</code> and <code>_bb</code> is replaced with <code>!!</code>.</p>
60-
<h6>Members</h6>
61-
<ul>
62-
<li><code>_jl_raw()</code>: Convert to a <a href="#juliaaa.RawValue"><code>RawValue</code></a></li>
63-
<li><code>_jl_display()</code>: Display the object using Julia's display mechanism.</li>
64-
<li><code>_jl_help()</code>: Display help for the object.</li>
65-
</ul>
66-
</section>
67-
</article>
68-
69-
<article class="docstring">
70-
<header>
71-
<a class="docstring-binding" id="juliaaa.NumberValue" href="#juliaaa.NumberValue">juliaaa.NumberValue</a>
72-
73-
<span class="docstring-category">Class</span>
74-
</header>
75-
<section>
76-
<p>This wraps any Julia <code>Number</code> value. It is a subclass of <code>numbers.Number</code> and behaves similar to other Python numbers.</p>
77-
<p>There are also subtypes <code>ComplexValue</code>, <code>RealValue</code>, <code>RationalValue</code>, <code>IntegerValue</code> which wrap values of the corresponding Julia types, and are subclasses of the corresponding <code>numbers</code> ABC.</p>
78-
</section>
79-
</article>
80-
81-
<article class="docstring">
82-
<header>
83-
<a class="docstring-binding" id="juliaaa.ArrayValue" href="#juliaaa.ArrayValue">juliaaa.ArrayValue</a>
84-
85-
<span class="docstring-category">Class</span>
86-
</header>
87-
<section>
88-
<p>This wraps any Julia <code>AbstractArray</code> value. It is a subclass of <code>collections.abc.Collection</code>.</p>
89-
<p>It supports zero-up indexing, and can be indexed with integers or slices. Slicing returns a view of the original array.</p>
90-
<p>There is also the subtype <code>VectorValue</code> which wraps any <code>AbstractVector</code>. It is a subclass of <code>collections.abc.Sequence</code> and behaves similar to a Python <code>list</code>.</p>
91-
<p>If the array is strided and its eltype is supported (i.e. <code>Bool</code>, <code>IntXX</code>, <code>UIntXX</code>, <code>FloatXX</code>, <code>Complex{FloatXX}</code>, <code>Ptr{Cvoid}</code> or <code>Tuple</code> or <code>NamedTuple</code> of these) then it supports the buffer protocol and the numpy array interface. This means that <code>numpy.asarray(this)</code> will yield a view of the original array, so mutations are visible on the original.</p>
92-
<p>Otherwise, the numpy <code>__array__</code> method is supported, and this returns an array of Python objects converted from the contents of the array. In this case, <code>numpy.asarray(this)</code> is a copy of the original array.</p>
93-
<h6>Members</h6>
94-
<ul>
95-
<li><code>ndim</code>: The number of dimensions.</li>
96-
<li><code>shape</code>: Tuple of lengths in each dimension.</li>
97-
<li><code>copy()</code>: A copy of the array.</li>
98-
<li><code>reshape(shape)</code>: A reshaped view of the array.</li>
99-
</ul>
100-
</section>
101-
</article>
102-
103-
<article class="docstring">
104-
<header>
105-
<a class="docstring-binding" id="juliaaa.DictValue" href="#juliaaa.DictValue">juliaaa.DictValue</a>
106-
107-
<span class="docstring-category">Class</span>
108-
</header>
109-
<section>
110-
<p>This wraps any Julia <code>AbstractDict</code> value. It is a subclass of <code>collections.abc.Mapping</code> and behaves similar to a Python <code>dict</code>.</p>
111-
</section>
112-
</article>
113-
114-
<article class="docstring">
115-
<header>
116-
<a class="docstring-binding" id="juliaaa.SetValue" href="#juliaaa.SetValue">juliaaa.SetValue</a>
117-
118-
<span class="docstring-category">Class</span>
119-
</header>
120-
<section>
121-
<p>This wraps any Julia <code>AbstractSet</code> value. It is a subclass of <code>collections.abc.Set</code> and behaves similar to a Python <code>set</code>.</p>
122-
</section>
123-
</article>
124-
125-
<article class="docstring">
126-
<header>
127-
<a class="docstring-binding" id="juliaaa.IOValue" href="#juliaaa.IOValue">juliaaa.IOValue</a>
128-
129-
<span class="docstring-category">Class</span>
130-
</header>
131-
<section>
132-
<p>This wraps any Julia <code>IO</code> value. It is a subclass of <code>io.IOBase</code> and behaves like Python files.</p>
133-
<p>There are also subtypes <code>RawIOValue</code>, <code>BufferedIOValue</code> and <code>TextIOValue</code>, which are subclasses of <code>io.RawIOBase</code> (unbuffered bytes), <code>io.BufferedIOBase</code> (buffered bytes) and <code>io.TextIOBase</code> (text).</p>
134-
<h6>Members</h6>
135-
<ul>
136-
<li><code>torawio()</code>: Convert to a <code>RawIOValue</code>, an un-buffered bytes file-like object. (See also <a href="../pythonjl/#Python.pyrawio"><code>pyrawio</code></a>.)
137-
<li><code>tobufferedio()</code>: Convert to a <code>BufferedIOValue</code>, an buffered bytes file-like object. Julia <code>IO</code> objects are converted to this by default. (See also <a href="../pythonjl/#Python.pybufferedio"><code>pybufferedio</code></a>.)
138-
<li><code>totextio()</code>: Convert to a <code>TextIOValue</code>, a text file-like object. (See also <a href="../pythonjl/#Python.pytextio"><code>pytextio</code></a>.)
139-
</ul>
140-
</section>
141-
</article>
142-
143-
<article class="docstring">
144-
<header>
145-
<a class="docstring-binding" id="juliaaa.ModuleValue" href="#juliaaa.ModuleValue">juliaaa.ModuleValue</a>
146-
147-
<span class="docstring-category">Class</span>
148-
</header>
149-
<section>
150-
<p>This wraps any Julia <code>Module</code> value.</p>
151-
<p>It is the same as <a href="#juliaaa.AnyValue"><code>AnyValue</code></a> except for one additional convenience method:</p>
152-
<ul>
153-
<li><code>seval([module=self], code)</code>: Evaluates the given code (a string) in the given module.</li>
154-
</ul>
155-
</section>
156-
</article>
157-
158-
<article class="docstring">
159-
<header>
160-
<a class="docstring-binding" id="juliaaa.TypeValue" href="#juliaaa.TypeValue">juliaaa.TypeValue</a>
161-
162-
<span class="docstring-category">Class</span>
163-
</header>
164-
<section>
165-
<p>This wraps any Julia <code>Type</code> value.</p>
166-
<p>It is the same as <a href="#juliaaa.AnyValue"><code>AnyValue</code></a> except that indexing is used to access Julia's "curly" syntax for specifying parametric types:</p>
167-
<pre><code class="language-python hljs"><span class="hljs-keyword">from</span> juliaaa <span class="hljs-keyword">import</span> Main <span class="hljs-keyword">as</span> jl
168-
<span class="hljs-comment"># equivalent to Vector{Int}() in Julia</span>
169-
jl.Vector[jl.Int]()</code></pre>
170-
</section>
171-
</article>
172-
173-
<article class="docstring">
174-
<header>
175-
<a class="docstring-binding" id="juliaaa.RawValue" href="#juliaaa.RawValue">juliaaa.RawValue</a>
176-
177-
<span class="docstring-category">Class</span>
178-
</header>
179-
<section>
180-
<p>Wraps any Julia value with a rigid interface suitable for generic programming.</p>
181-
<p>Supports <code>repr(x)</code>, <code>str(x)</code>, attributes (<code>x.attr</code>), calling (<code>x(a,b)</code>), <code>len(x)</code>, <code>dir(x)</code>.</p>
182-
<p>This is very similar to <a href="#juliaaa.AnyValue"><code>AnyValue</code></a> except that indexing, calling, etc. will always return a <code>RawValue</code>.</p>
183-
<p>Indexing with a tuple corresponds to indexing in Julia with multiple values. To index with a single tuple, it will need to be wrapped in another tuple.</p>
184-
<h6>Members</h6>
185-
<ul>
186-
<li><code>_jl_any()</code>: Convert to a <a href="#juliaaa.AnyValue"><code>AnyValue</code></a> (or subclass). (See also <a href="../pythonjl/#Python.pyjl">pyjl</a>.)</li>
187-
</ul>
188-
</section>
189-
</article>
48+
`````@customdoc
49+
juliaaa.AnyValue - Class
50+
51+
Wraps any Julia object, giving it some basic Python semantics. Subtypes provide extra semantics.
52+
53+
Supports `repr(x)`, `str(x)`, attributes (`x.attr`), calling (`x(a,b)`), iteration, comparisons, `len(x)`, `a in x`, `dir(x)`.
54+
55+
Calling, indexing, attribute access, etc. will convert the result to a Python object according to [this table](@ref jl2py). This is typically a builtin Python type (for immutables) or a subtype of `AnyValue`.
56+
57+
Attribute access can be used to access Julia properties as well as normal class members. In the case of a name clash, the class member will take precedence. For convenience with Julia naming conventions, `_b` at the end of an attribute is replaced with `!` and `_bb` is replaced with `!!`.
58+
59+
###### Members
60+
- `_jl_raw()`: Convert to a [`RawValue`](#juliaaa.RawValue). (See also [`pyjlraw`](@ref).)
61+
- `_jl_display()`: Display the object using Julia's display mechanism.
62+
- `_jl_help()`: Display help for the object.
63+
`````
64+
65+
`````@customdoc
66+
juliaaa.NumberValue - Class
67+
68+
This wraps any Julia `Number` value. It is a subclass of `numbers.Number` and behaves similar to other Python numbers.
69+
70+
There are also subtypes `ComplexValue`, `RealValue`, `RationalValue`, `IntegerValue` which wrap values of the corresponding Julia types, and are subclasses of the corresponding `numbers` ABC.
71+
`````
72+
73+
`````@customdoc
74+
juliaaa.ArrayValue - Class
75+
76+
This wraps any Julia `AbstractArray` value. It is a subclass of `collections.abc.Collection`.
77+
78+
It supports zero-up indexing, and can be indexed with integers or slices. Slicing returns a view of the original array.
79+
80+
There is also the subtype `VectorValue` which wraps any `AbstractVector`. It is a subclass of `collections.abc.Sequence` and behaves similar to a Python `list`.
81+
82+
If the array is strided and its eltype is supported (i.e. `Bool`, `IntXX`, `UIntXX`, `FloatXX`, `Complex{FloatXX}`, `Ptr{Cvoid}` or `Tuple` or `NamedTuple` of these) then it supports the buffer protocol and the numpy array interface. This means that `numpy.asarray(this)` will yield a view of the original array, so mutations are visible on the original.
83+
84+
Otherwise, the numpy `__array__` method is supported, and this returns an array of Python objects converted from the contents of the array. In this case, `numpy.asarray(this)` is a copy of the original array.
85+
86+
###### Members
87+
- `ndim`: The number of dimensions.
88+
- `shape`: Tuple of lengths in each dimension.
89+
- `copy()`: A copy of the array.
90+
- `reshape(shape)`: A reshaped view of the array.
91+
`````
92+
93+
`````@customdoc
94+
juliaaa.DictValue - Class
95+
This wraps any Julia `AbstractDict` value. It is a subclass of `collections.abc.Mapping` and behaves similar to a Python `dict`.
96+
`````
97+
98+
`````@customdoc
99+
juliaaa.SetValue - Class
100+
This wraps any Julia `AbstractSet` value. It is a subclass of `collections.abc.Set` and behaves similar to a Python `set`.
101+
`````
102+
103+
`````@customdoc
104+
juliaaa.IOValue - Class
105+
106+
This wraps any Julia `IO` value. It is a subclass of `io.IOBase` and behaves like Python files.
107+
108+
There are also subtypes `RawIOValue`, `BufferedIOValue` and `TextIOValue`, which are subclasses of `io.RawIOBase` (unbuffered bytes), `io.BufferedIOBase` (buffered bytes) and `io.TextIOBase` (text).
109+
110+
###### Members
111+
- `torawio()`: Convert to a `RawIOValue`, an un-buffered bytes file-like object. (See also [`pyrawio`](@ref).)
112+
- `tobufferedio()`: Convert to a `BufferedIOValue`, an buffered bytes file-like object. Julia `IO` objects are converted to this by default. (See also [`pybufferedio`](@ref).)
113+
- `totextio()`: Convert to a `TextIOValue`, a text file-like object. (See also [`pytextio`](@ref).)
114+
`````
115+
116+
`````@customdoc
117+
juliaaa.ModuleValue - Class
118+
This wraps any Julia `Module` value.
119+
120+
It is the same as [`AnyValue`](#juliaaa.AnyValue) except for one additional convenience method:
121+
- `seval([module=self], code)`: Evaluates the given code (a string) in the given module.
122+
`````
123+
124+
`````@customdoc
125+
juliaaa.TypeValue - Class
126+
127+
This wraps any Julia `Type` value.
128+
129+
It is the same as [`AnyValue`](#juliaaa.AnyValue) except that indexing is used to access Julia's "curly" syntax for specifying parametric types:
130+
131+
```python
132+
from juliaaa import Main as jl
133+
# equivalent to Vector{Int}() in Julia
134+
jl.Vector[jl.Int]()
190135
```
136+
`````
137+
138+
`````@customdoc
139+
juliaaa.RawValue - Class
140+
141+
Wraps any Julia value with a rigid interface suitable for generic programming.
142+
143+
Supports `repr(x)`, `str(x)`, attributes (`x.attr`), calling (`x(a,b)`), `len(x)`, `dir(x)`.
144+
145+
This is very similar to [`AnyValue`](#juliaaa.AnyValue) except that indexing, calling, etc. will always return a `RawValue`.
146+
147+
Indexing with a tuple corresponds to indexing in Julia with multiple values. To index with a single tuple, it will need to be wrapped in another tuple.
148+
149+
###### Members
150+
- `_jl_any()`: Convert to a [`AnyValue`](#juliaaa.AnyValue) (or subclass). (See also [`pyjl`](@ref).)
151+
`````
191152

192153
## Utilities
193154

194-
```@raw html
195-
<article class="docstring">
196-
<header>
197-
<a class="docstring-binding" id="juliaaa.newmodule" href="#juliaaa.newmodule">juliaaa.newmodule</a>
198-
199-
<span class="docstring-category">Function</span>
200-
</header>
201-
<section>
202-
<pre><code class="language-python hljs">newmodule(name)</code></pre>
203-
<p>A new module with the given name.</p>
204-
</section>
205-
</article>
206-
207-
<article class="docstring">
208-
<header>
209-
<a class="docstring-binding" id="juliaaa.As" href="#juliaaa.As">juliaaa.As</a>
210-
211-
<span class="docstring-category">Class</span>
212-
</header>
213-
<section>
214-
<pre><code class="language-python hljs">As(x, T)</code></pre>
215-
<p>When passed as an argument to a Julia function, is interpreted as <code>x</code> converted to Julia type <code>T</code>.</p>
216-
</section>
217-
</article>
155+
`````@customdoc
156+
juliaaa.newmodule - Function
157+
158+
```python
159+
newmodule(name)
160+
```
161+
162+
A new module with the given name.
163+
`````
164+
165+
`````@customdoc
166+
juliaaa.As - Class
167+
168+
```python
169+
As(x, T)
218170
```
171+
172+
When passed as an argument to a Julia function, is interpreted as `x` converted to Julia type `T`.
173+
`````

0 commit comments

Comments
 (0)