Encode Python data structure to TOON format. Args: data: Python object to encode (dict or list) options: Encoding options - delimiter: ',' (default), '\t', or '|' - indent: int (default 2) - key_folding: 'off' (default) or 'safe'
(data: Any, options: Optional[Dict[str, Any]] = None)
| 48 | |
| 49 | |
| 50 | def encode(data: Any, options: Optional[Dict[str, Any]] = None) -> str: |
| 51 | """ |
| 52 | Encode Python data structure to TOON format. |
| 53 | |
| 54 | Args: |
| 55 | data: Python object to encode (dict or list) |
| 56 | options: Encoding options |
| 57 | - delimiter: ',' (default), '\t', or '|' |
| 58 | - indent: int (default 2) |
| 59 | - key_folding: 'off' (default) or 'safe' |
| 60 | - flatten_depth: int or None |
| 61 | |
| 62 | Returns: |
| 63 | TOON formatted string |
| 64 | |
| 65 | Example: |
| 66 | >>> data = {'users': [{'id': 1, 'name': 'Alice'}]} |
| 67 | >>> print(encode(data)) |
| 68 | users[1]{id,name}: |
| 69 | 1,Alice |
| 70 | """ |
| 71 | if options is None: |
| 72 | options = {} |
| 73 | |
| 74 | opts = EncoderOptions( |
| 75 | delimiter=options.get('delimiter', DEFAULT_DELIMITER), |
| 76 | indent=options.get('indent', DEFAULT_INDENT), |
| 77 | key_folding=options.get('key_folding', KEY_FOLDING_OFF), |
| 78 | flatten_depth=options.get('flatten_depth') |
| 79 | ) |
| 80 | |
| 81 | return _encode_value(data, 0, opts) |
| 82 | |
| 83 | |
| 84 | def _encode_value(value: Any, level: int, opts: EncoderOptions) -> str: |
searching dependent graphs…