[docs]defdetect(byte_str:bytes)->Dict[str,Optional[Union[str,float]]]:""" chardet legacy method Detect the encoding of the given byte string. It should be mostly backward-compatible. Encoding name will match Chardet own writing whenever possible. (Not on encoding name unsupported by it) This function is deprecated and should be used to migrate your project easily, consult the documentation for further information. Not planned for removal. :param byte_str: The byte sequence to examine. """ifnotisinstance(byte_str,(bytearray,bytes)):raiseTypeError(# pragma: nocover"Expected object of type bytes or bytearray, got: ""{0}".format(type(byte_str)))ifisinstance(byte_str,bytearray):byte_str=bytes(byte_str)r=from_bytes(byte_str).best()encoding=r.encodingifrisnotNoneelseNonelanguage=r.languageifrisnotNoneandr.language!="Unknown"else""confidence=1.0-r.chaosifrisnotNoneelseNone# Note: CharsetNormalizer does not return 'UTF-8-SIG' as the sig get stripped in the detection/normalization process# but chardet does return 'utf-8-sig' and it is a valid codec name.ifrisnotNoneandencoding=="utf_8"andr.bom:encoding+="_sig"return{"encoding":encodingifencodingnotinCHARDET_CORRESPONDENCEelseCHARDET_CORRESPONDENCE[encoding],"language":language,"confidence":confidence,}
classCharsetNormalizerMatch(CharsetMatch):passclassCharsetNormalizerMatches(CharsetMatches):@staticmethoddeffrom_fp(*args,**kwargs):# type: ignorewarnings.warn(# pragma: nocover"staticmethod from_fp, from_bytes, from_path and normalize are deprecated ""and scheduled to be removed in 3.0",DeprecationWarning,)returnfrom_fp(*args,**kwargs)# pragma: nocover@staticmethoddeffrom_bytes(*args,**kwargs):# type: ignorewarnings.warn(# pragma: nocover"staticmethod from_fp, from_bytes, from_path and normalize are deprecated ""and scheduled to be removed in 3.0",DeprecationWarning,)returnfrom_bytes(*args,**kwargs)# pragma: nocover@staticmethoddeffrom_path(*args,**kwargs):# type: ignorewarnings.warn(# pragma: nocover"staticmethod from_fp, from_bytes, from_path and normalize are deprecated ""and scheduled to be removed in 3.0",DeprecationWarning,)returnfrom_path(*args,**kwargs)# pragma: nocover@staticmethoddefnormalize(*args,**kwargs):# type: ignorewarnings.warn(# pragma: nocover"staticmethod from_fp, from_bytes, from_path and normalize are deprecated ""and scheduled to be removed in 3.0",DeprecationWarning,)returnnormalize(*args,**kwargs)# pragma: nocoverclassCharsetDetector(CharsetNormalizerMatches):passclassCharsetDoctor(CharsetNormalizerMatches):pass