MCPcopy Index your code
hub / github.com/ScrapeGraphAI/toonify / _encode_value

Function _encode_value

toon/encoder.py:84–124  ·  view source on GitHub ↗

Encode a value at a given indentation level.

(value: Any, level: int, opts: EncoderOptions)

Source from the content-addressed store, hash-verified

82
83
84def _encode_value(value: Any, level: int, opts: EncoderOptions) -> str:
85 """Encode a value at a given indentation level."""
86 if value is None:
87 return 'null'
88 elif isinstance(value, bool):
89 return 'true' if value else 'false'
90 elif isinstance(value, datetime):
91 # Convert datetime to ISO 8601 string
92 iso_string = value.isoformat()
93 if needs_quoting(iso_string):
94 return quote_string(iso_string)
95 return iso_string
96 elif isinstance(value, date):
97 # Convert date to ISO 8601 date string
98 iso_string = value.isoformat()
99 if needs_quoting(iso_string):
100 return quote_string(iso_string)
101 return iso_string
102 elif isinstance(value, (int, float)):
103 # Handle special float values
104 if isinstance(value, float):
105 if value != value: # NaN
106 return 'null'
107 elif value == float('inf') or value == float('-inf'):
108 return 'null'
109 # Use format_float to suppress scientific notation
110 return format_float(value)
111 return str(value)
112 elif isinstance(value, str):
113 if needs_quoting(value):
114 return quote_string(value)
115 return value
116 elif isinstance(value, list):
117 return _encode_array(value, level, opts)
118 elif isinstance(value, dict):
119 return _encode_object(value, level, opts)
120 elif isinstance(value, tuple):
121 return _encode_tuple(value)
122 else:
123 # Handle other types with NotImplementedError
124 raise NotImplementedError(f'Encoding for type {type(value)} is not implemented.')
125
126
127def _encode_object(obj: dict, level: int, opts: EncoderOptions) -> str:

Callers 3

encodeFunction · 0.85
_encode_objectFunction · 0.85
_encode_list_arrayFunction · 0.85

Calls 6

needs_quotingFunction · 0.85
quote_stringFunction · 0.85
format_floatFunction · 0.85
_encode_arrayFunction · 0.85
_encode_objectFunction · 0.85
_encode_tupleFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…