detects encodings of raw files, or the system default encoding
Project description
lib_detect_encoding
Version v1.0.1 as of 2023-10-14 see Changelog
detects the encoding of textfiles
detects the system preferred encoding
returns the language (if possible) for a given encoding
automated tests, Github Actions, Documentation, Badges, etc. are managed with PizzaCutter (cookiecutter on steroids)
Python version required: 3.8.0 or newer
tested on recent linux with python 3.8, 3.9, 3.10, 3.11, 3.12-dev, pypy-3.9, pypy-3.10 - architectures: amd64
100% code coverage, flake8 style checking ,mypy static type checking ,tested under Linux, macOS, Windows, automatic daily builds and monitoring
Try it Online
You might try it right away in Jupyter Notebook by using the “launch binder” badge, or click here
Usage
def get_system_preferred_encoding() -> str:
""" returns the system preferred encoding in lowercase. Works on posix, windows and WINE
On windows, the python default function "locale.getpreferredencoding" sometimes reports falsely cp1252 instead of cp850,
therefore we check also with windows command "chcp" for the correct preferred codepage
Note that the python codec name will be returned, such as : utf_8, utf_8_sig etc.
see: https://docs.python.org/3/library/codecs.html#standard-encodings
"""
def get_file_encoding(raw_bytes: bytes) -> str:
""" returns the encoding for the raw_bytes passed.
if the confidence of the detection is below 95 percent, the system default encoding will be returned
Note that the python codec name will be returned, such as : utf_8, utf_8_sig etc.
see: https://docs.python.org/3/library/codecs.html#standard-encodings
>>> # Setup
>>> import pathlib
>>> path_testfile_utf8 = pathlib.Path(__file__).parent.parent / "tests/testfile_utf8.txt"
>>> raw_utf8_bytes = path_testfile_utf8.read_bytes()
>>> # Test get encoding from bytes
>>> assert get_file_encoding(raw_utf8_bytes) == 'utf_8'
>>> # test get encoding with low confidence (returning system default encoding)
>>> assert get_file_encoding(b'') is not None
>>> assert get_file_encoding(b'x') is not None
>>> assert len(get_file_encoding(b'x')) > 0
"""
def get_language_by_codec_name(codec_name: str) -> str:
""" get the language by python codec name
>>> # Test OK
>>> assert get_language_by_codec_name('utf-8') == "all languages"
>>> assert get_language_by_codec_name('utf-8') == "all languages"
>>> # Test unknown encoding
>>> get_language_by_codec_name('unknown')
Traceback (most recent call last):
...
KeyError: 'codec "unknown" not found'
>>> # Test if language is present for all codepage_aliases
>>> for codec_alias in codec_aliases: \
codec_language = get_language_by_codec_name(codec_alias)
"""
Usage from Commandline
Usage: lib_detect_encoding [OPTIONS] COMMAND [ARGS]...
detects encodings of raw files, or the system default encoding
Options:
--version Show the version and exit.
--traceback / --no-traceback return traceback information on cli
-h, --help Show this message and exit.
Commands:
get_file_encoding get encoding from a (text)file
get_language get the language from a codec name
get_system_preferred_encoding get the system preferred encoding
info get program informations
Installation and Upgrade
Before You start, its highly recommended to update pip and setup tools:
python -m pip --upgrade pip
python -m pip --upgrade setuptools
to install the latest release from PyPi via pip (recommended):
python -m pip install --upgrade lib_detect_encoding
to install the latest release from PyPi via pip, including test dependencies:
python -m pip install --upgrade lib_detect_encoding[test]
to install the latest version from github via pip:
python -m pip install --upgrade git+https://github.com/bitranox/lib_detect_encoding.git
include it into Your requirements.txt:
# Insert following line in Your requirements.txt:
# for the latest Release on pypi:
lib_detect_encoding
# for the latest development version :
lib_detect_encoding @ git+https://github.com/bitranox/lib_detect_encoding.git
# to install and upgrade all modules mentioned in requirements.txt:
python -m pip install --upgrade -r /<path>/requirements.txt
to install the latest development version, including test dependencies from source code:
# cd ~
$ git clone https://github.com/bitranox/lib_detect_encoding.git
$ cd lib_detect_encoding
python -m pip install -e .[test]
via makefile: makefiles are a very convenient way to install. Here we can do much more, like installing virtual environments, clean caches and so on.
# from Your shell's homedirectory:
$ git clone https://github.com/bitranox/lib_detect_encoding.git
$ cd lib_detect_encoding
# to run the tests:
$ make test
# to install the package
$ make install
# to clean the package
$ make clean
# uninstall the package
$ make uninstall
Requirements
following modules will be automatically installed :
## Project Requirements
click
cli_exit_tools
chardet
lib_log_utils
lib_platform
Acknowledgements
special thanks to “uncle bob” Robert C. Martin, especially for his books on “clean code” and “clean architecture”
Contribute
I would love for you to fork and send me pull request for this project. - please Contribute
License
This software is licensed under the MIT license
—
Changelog
v1.0.1
- 2023-10-14:
update documentation
v1.0.0
- 2023-10-14:
create mypy cache dir ‘.mypy_cache’
require minimum python 3.8
remove python 3.7 tests
introduce PEP517 packaging standard
introduce pyproject.toml build-system
remove mypy.ini
remove pytest.ini
remove setup.cfg
remove setup.py
remove .bettercodehub.yml
remove .travis.yml
update black config
clean ./tests/test_cli.py
add codeql badge
move 3rd_party_stubs outside the src directory to ./.3rd_party_stubs
add pypy 3.10 tests
add python 3.12-dev tests
0.0.1
2019-07-22: Initial public release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file lib_detect_encoding-1.0.1.tar.gz
.
File metadata
- Download URL: lib_detect_encoding-1.0.1.tar.gz
- Upload date:
- Size: 37.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60ea9a7584d95d600cb62016707a67b1b2cf4d9cba71e2905db401342596ddd8 |
|
MD5 | 4a87d0e280f555178b0e783efb5ebbc5 |
|
BLAKE2b-256 | 1d62e916c429c0c1b5c37ba0b9f820adc0bfc7b5fdfb0276dbb488c638ad6ddc |
File details
Details for the file lib_detect_encoding-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: lib_detect_encoding-1.0.1-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b6b7d14bb924ffbb48e04a9a26b586bf928f6408a8cf087076f69a770fd01a2 |
|
MD5 | 754724438e27a15afab06a0746b5e0cd |
|
BLAKE2b-256 | 703eedf9712736798d6d4fbe6253ce74c55590f2783df759167feafce957ac9e |