-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from cchardet import _cchardet | ||
from .version import __version__ | ||
|
||
|
||
def detect(msg): | ||
""" | ||
Args: | ||
msg: str | ||
Returns: | ||
{ | ||
"encoding": str, | ||
"confidence": float | ||
} | ||
""" | ||
encoding, confidence = _cchardet.detect_with_confidence(msg) | ||
if isinstance(encoding, bytes): | ||
encoding = encoding.decode() | ||
return {"encoding": encoding, "confidence": confidence} | ||
|
||
|
||
class UniversalDetector(object): | ||
def __init__(self): | ||
self._detector = _cchardet.UniversalDetector() | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exception_type, exception_value, traceback): | ||
self.close() | ||
return False | ||
|
||
def reset(self): | ||
self._detector.reset() | ||
|
||
def feed(self, data): | ||
self._detector.feed(data) | ||
|
||
def close(self): | ||
self._detector.close() | ||
|
||
@property | ||
def done(self): | ||
return self._detector.done | ||
|
||
@property | ||
def result(self): | ||
encoding, confidence = self._detector.result | ||
if isinstance(encoding, bytes): | ||
encoding = encoding.decode() | ||
return {"encoding": encoding, "confidence": confidence} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = '2.1.5' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters