@@ -24,21 +24,24 @@ def serialize_bytes(data: bytes | bytearray) -> dict[str, str]:
2424 except UnicodeDecodeError :
2525 pass
2626 return {key : _base64 .b64encode (data ).decode ("utf-8" ), "encoding" : "base64" }
27- raise TypeError ("Unsupported data type" )
27+ raise TypeError (f "Unsupported data type ' { type ( data ) } ' " )
2828
2929 @staticmethod
3030 def deserialize_bytes (obj : dict [str , str ]) -> bytes | bytearray :
31- """Converts a JSON-compatible bytes/bytearray format (dictionary) back to its original type."""
31+ """Tries to converts a JSON-compatible bytes/bytearray format (dictionary) back to its original type.\n
32+ --------------------------------------------------------------------------------------------------------
33+ If the serialized object was created with `Data.serialize_bytes()`, it will work.
34+ If it fails to decode the data, it will raise a `ValueError`."""
3235 for key in ("bytes" , "bytearray" ):
3336 if key in obj and "encoding" in obj :
3437 if obj ["encoding" ] == "utf-8" :
3538 data = obj [key ].encode ("utf-8" )
3639 elif obj ["encoding" ] == "base64" :
3740 data = _base64 .b64decode (obj [key ].encode ("utf-8" ))
3841 else :
39- raise ValueError ("Unknown encoding method" )
42+ raise ValueError (f "Unknown encoding method ' { obj [ 'encoding' ] } ' " )
4043 return bytearray (data ) if key == "bytearray" else data
41- raise ValueError ("Invalid serialized data" )
44+ raise ValueError (f "Invalid serialized data: { obj } " )
4245
4346 @staticmethod
4447 def chars_count (data : DataStructure ) -> int :
@@ -387,7 +390,7 @@ def update_nested(data: DataStructure, path: list[int], value: Any) -> DataStruc
387390
388391 valid_entries = [(path_id , new_val ) for path_id , new_val in update_values .items ()]
389392 if not valid_entries :
390- raise ValueError (f"No valid update_values found in dictionary: { update_values } " )
393+ raise ValueError (f"No valid ' update_values' found in dictionary: { update_values } " )
391394 for path_id , new_val in valid_entries :
392395 path = Data .__sep_path_id (path_id )
393396 data = update_nested (data , path , new_val )
@@ -581,7 +584,7 @@ def print(
581584 @staticmethod
582585 def __sep_path_id (path_id : str ) -> list [int ]:
583586 if path_id .count (">" ) != 1 :
584- raise ValueError (f"Invalid path ID: { path_id } " )
587+ raise ValueError (f"Invalid path ID ' { path_id } ' " )
585588 id_part_len = int (path_id .split (">" )[0 ])
586589 path_ids_str = path_id .split (">" )[1 ]
587590 return [int (path_ids_str [i :i + id_part_len ]) for i in range (0 , len (path_ids_str ), id_part_len )]
0 commit comments