Skip to content

Commit 7554240

Browse files
Merge pull request #1455 from datajoint/docs/fix-codecs-docstring-formatting
docs: fix code formatting on rendered codecs API page
2 parents ada2ee4 + 97dce3a commit 7554240

1 file changed

Lines changed: 56 additions & 41 deletions

File tree

src/datajoint/codecs.py

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,32 @@
88
Codecs auto-register when subclassed - no decorator needed (Python 3.10+).
99
1010
Example:
11-
class GraphCodec(dj.Codec):
12-
name = "graph"
1311
14-
def get_dtype(self, is_store: bool) -> str:
15-
return "<blob>"
12+
```python
13+
class GraphCodec(dj.Codec):
14+
name = "graph"
1615
17-
def encode(self, graph, *, key=None, store_name=None):
18-
return {'nodes': list(graph.nodes()), 'edges': list(graph.edges())}
19-
20-
def decode(self, stored, *, key=None):
21-
import networkx as nx
22-
G = nx.Graph()
23-
G.add_nodes_from(stored['nodes'])
24-
G.add_edges_from(stored['edges'])
25-
return G
26-
27-
# Then use in table definitions:
28-
class MyTable(dj.Manual):
29-
definition = '''
30-
id : uint16
31-
---
32-
data : <graph>
33-
'''
16+
def get_dtype(self, is_store: bool) -> str:
17+
return "<blob>"
18+
19+
def encode(self, graph, *, key=None, store_name=None):
20+
return {'nodes': list(graph.nodes()), 'edges': list(graph.edges())}
21+
22+
def decode(self, stored, *, key=None):
23+
import networkx as nx
24+
G = nx.Graph()
25+
G.add_nodes_from(stored['nodes'])
26+
G.add_edges_from(stored['edges'])
27+
return G
28+
29+
# Then use in table definitions:
30+
class MyTable(dj.Manual):
31+
definition = '''
32+
id : uint16
33+
---
34+
data : <graph>
35+
'''
36+
```
3437
"""
3538

3639
from __future__ import annotations
@@ -85,20 +88,24 @@ class Codec(ABC):
8588
... G.add_edges_from(stored['edges'])
8689
... return G
8790
88-
Use in table definitions::
91+
Use in table definitions:
8992
90-
class Connectivity(dj.Manual):
91-
definition = '''
92-
id : uint16
93-
---
94-
graph_data : <graph>
95-
'''
93+
```python
94+
class Connectivity(dj.Manual):
95+
definition = '''
96+
id : uint16
97+
---
98+
graph_data : <graph>
99+
'''
100+
```
96101
97-
Skip auto-registration for abstract base classes::
102+
Skip auto-registration for abstract base classes:
98103
99-
class ExternalOnlyCodec(dj.Codec, register=False):
100-
'''Abstract base - not registered.'''
101-
...
104+
```python
105+
class ExternalOnlyCodec(dj.Codec, register=False):
106+
'''Abstract base - not registered.'''
107+
...
108+
```
102109
"""
103110

104111
name: str | None = None # Must be set by concrete subclasses
@@ -520,18 +527,26 @@ def decode_attribute(attr, data, squeeze: bool = False, connection=None):
520527
Decode raw database value using attribute's codec or native type handling.
521528
522529
This is the central decode function used by all fetch methods. It handles:
523-
- Codec chains (e.g., <blob@store> → <hash> → bytes)
530+
531+
- Codec chains (e.g., ``<blob@store>`` → ``<hash>`` → ``bytes``)
524532
- Native type conversions (JSON, UUID)
525-
- Object storage downloads (via config["download_path"])
533+
- Object storage downloads (via ``config["download_path"]``)
526534
527-
Args:
528-
attr: Attribute from the table's heading.
529-
data: Raw value fetched from the database.
530-
squeeze: If True, remove singleton dimensions from numpy arrays.
531-
connection: Connection instance for config access. If provided,
532-
``connection._config`` is passed to codecs via the key dict.
535+
Parameters
536+
----------
537+
attr : Attribute
538+
Attribute from the table's heading.
539+
data : any
540+
Raw value fetched from the database.
541+
squeeze : bool, optional
542+
If True, remove singleton dimensions from numpy arrays.
543+
connection : Connection, optional
544+
Connection instance for config access. If provided,
545+
``connection._config`` is passed to codecs via the key dict.
533546
534-
Returns:
547+
Returns
548+
-------
549+
any
535550
Decoded Python value.
536551
"""
537552
import json

0 commit comments

Comments
 (0)