|
8 | 8 | Codecs auto-register when subclassed - no decorator needed (Python 3.10+). |
9 | 9 |
|
10 | 10 | Example: |
11 | | - class GraphCodec(dj.Codec): |
12 | | - name = "graph" |
13 | 11 |
|
14 | | - def get_dtype(self, is_store: bool) -> str: |
15 | | - return "<blob>" |
| 12 | +```python |
| 13 | +class GraphCodec(dj.Codec): |
| 14 | + name = "graph" |
16 | 15 |
|
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 | +``` |
34 | 37 | """ |
35 | 38 |
|
36 | 39 | from __future__ import annotations |
@@ -85,20 +88,24 @@ class Codec(ABC): |
85 | 88 | ... G.add_edges_from(stored['edges']) |
86 | 89 | ... return G |
87 | 90 |
|
88 | | - Use in table definitions:: |
| 91 | + Use in table definitions: |
89 | 92 |
|
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 | + ``` |
96 | 101 |
|
97 | | - Skip auto-registration for abstract base classes:: |
| 102 | + Skip auto-registration for abstract base classes: |
98 | 103 |
|
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 | + ``` |
102 | 109 | """ |
103 | 110 |
|
104 | 111 | name: str | None = None # Must be set by concrete subclasses |
@@ -520,18 +527,26 @@ def decode_attribute(attr, data, squeeze: bool = False, connection=None): |
520 | 527 | Decode raw database value using attribute's codec or native type handling. |
521 | 528 |
|
522 | 529 | 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``) |
524 | 532 | - Native type conversions (JSON, UUID) |
525 | | - - Object storage downloads (via config["download_path"]) |
| 533 | + - Object storage downloads (via ``config["download_path"]``) |
526 | 534 |
|
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. |
533 | 546 |
|
534 | | - Returns: |
| 547 | + Returns |
| 548 | + ------- |
| 549 | + any |
535 | 550 | Decoded Python value. |
536 | 551 | """ |
537 | 552 | import json |
|
0 commit comments