Skip to main content

detects encodings of raw files, or the system default encoding

Project description

lib_detect_encoding

Version v1.0.0 as of 2023-10-14 see Changelog

build_badge codeql license jupyter pypi PyPI - Downloads black codecov Maintainability Maintainability Code Coverage snyk

put your description of the project under .docs/description.rst


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.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lib_detect_encoding-1.0.0.tar.gz (37.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lib_detect_encoding-1.0.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file lib_detect_encoding-1.0.0.tar.gz.

File metadata

  • Download URL: lib_detect_encoding-1.0.0.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

Hashes for lib_detect_encoding-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1e29003a3c1d61c385d343e429adfabc0834fb95bcbd5007c9b6eebbaa21a94a
MD5 828f10abfbe17de09731f00ecce4b75d
BLAKE2b-256 f8b51ab5539c62c53ad5990db8311bd266c5f92414d748aefc4531383080a518

See more details on using hashes here.

File details

Details for the file lib_detect_encoding-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for lib_detect_encoding-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6c716d57e34cab8d1d30619457c9a674f20380cda5513029f21e73cffa56b14
MD5 03f0ec8cdb3b3245a82df8db056c8553
BLAKE2b-256 ffee894dbe90eea239223aca9d60b801b7ea4bccf051de0db68e1b28116fc14c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page