morphys


Namemorphys JSON
Version 1.0 PyPI version JSON
download
home_pagehttps://github.com/mkalinski/morphys
SummarySmart conversions between unicode and bytes types for common cases
upload_time2017-01-10 20:08:56
maintainer
docs_urlNone
authorMichał Kaliński
requires_python
licenseMIT
keywords string unicode bytes conversion
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            =======
Morphys
=======

**Morphys** is a simple, small library providing utilities for easy smart
conversions between ``unicode`` and ``bytes`` types.

In Python 2, the treatment of ``unicode`` and ``bytes`` objects is a potential
source of many surprising and annoying errors, should a non-ASCII character
appear anywhere in a string.

The two types are completely equivalent if only ASCII characters are used, and
many libraries treat them as such, while others are more vigilant about which
type they accept. Some can even return one or the other type of string from the
same function depending on the content (for example, the standard library
``json`` module).

**Morphys** is meant to help with cases where types of strings handled by
libraries are inconsistent or undocumented. Or where simply both types of
string can appear, but should be coerced to the same type before being handled.


Conversion functions
====================

**Morphys** provides two functions for smart conversions to ``unicode`` or
``bytes``. They just return their argument unchanged if it's already the right
type and can also convert any object defining the ``__unicode__`` (or
``__str__`` in Python 3) "magic" method.

``ensure_bytes(obj, encoding=None)``
    Return ``obj`` as a ``bytes`` object, if necessary encoding it using
    ``encoding``.

``ensure_unicode(obj, encoding=None)``
    Return ``obj`` as an ``unicode`` object, if necessary decoding it using
    ``encoding``.

See docstrings in the module for more detailed description of the functions.


Lazy conversion wrapper
=======================

In certain cases it may be more convenient to use an object that can be used
both as ``bytes`` or ``unicode`` as needed. The class ``StrMorpher`` is
provided for this case. Its constructor takes the same arguments as the
conversion functions. The object makes calls to the appropriate conversion
function when its "magic" methods that convert to ``bytes`` or ``unicode``
are invoked.

Note, that ``StrMorpher`` is not itself a subclass of any of the string
types and relies on the "magic" methods being called to produce the actual
string objects.

See docstrings in the module for more detailed description of the class.


Default encoding
================

The ``encoding`` parameter is optional in all places where it's accepted. Where
it is left as ``None``, the default encoding is used.

The default encoding is controlled by ``default_encoding`` global
variable in the ``morphys`` module. Initially it's set to "utf-8", but it's
allowed to set it to a different encoding name from user code.


Python 3
========

While **Morphys** is mostly meant to solve issues of string types handling in
Python 2, it's fully compatible with Python 3 and can be used with it.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mkalinski/morphys",
    "name": "morphys",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "string unicode bytes conversion",
    "author": "Micha\u0142 Kali\u0144ski",
    "author_email": "",
    "download_url": "",
    "platform": "",
    "description": "=======\nMorphys\n=======\n\n**Morphys** is a simple, small library providing utilities for easy smart\nconversions between ``unicode`` and ``bytes`` types.\n\nIn Python 2, the treatment of ``unicode`` and ``bytes`` objects is a potential\nsource of many surprising and annoying errors, should a non-ASCII character\nappear anywhere in a string.\n\nThe two types are completely equivalent if only ASCII characters are used, and\nmany libraries treat them as such, while others are more vigilant about which\ntype they accept. Some can even return one or the other type of string from the\nsame function depending on the content (for example, the standard library\n``json`` module).\n\n**Morphys** is meant to help with cases where types of strings handled by\nlibraries are inconsistent or undocumented. Or where simply both types of\nstring can appear, but should be coerced to the same type before being handled.\n\n\nConversion functions\n====================\n\n**Morphys** provides two functions for smart conversions to ``unicode`` or\n``bytes``. They just return their argument unchanged if it's already the right\ntype and can also convert any object defining the ``__unicode__`` (or\n``__str__`` in Python 3) \"magic\" method.\n\n``ensure_bytes(obj, encoding=None)``\n    Return ``obj`` as a ``bytes`` object, if necessary encoding it using\n    ``encoding``.\n\n``ensure_unicode(obj, encoding=None)``\n    Return ``obj`` as an ``unicode`` object, if necessary decoding it using\n    ``encoding``.\n\nSee docstrings in the module for more detailed description of the functions.\n\n\nLazy conversion wrapper\n=======================\n\nIn certain cases it may be more convenient to use an object that can be used\nboth as ``bytes`` or ``unicode`` as needed. The class ``StrMorpher`` is\nprovided for this case. Its constructor takes the same arguments as the\nconversion functions. The object makes calls to the appropriate conversion\nfunction when its \"magic\" methods that convert to ``bytes`` or ``unicode``\nare invoked.\n\nNote, that ``StrMorpher`` is not itself a subclass of any of the string\ntypes and relies on the \"magic\" methods being called to produce the actual\nstring objects.\n\nSee docstrings in the module for more detailed description of the class.\n\n\nDefault encoding\n================\n\nThe ``encoding`` parameter is optional in all places where it's accepted. Where\nit is left as ``None``, the default encoding is used.\n\nThe default encoding is controlled by ``default_encoding`` global\nvariable in the ``morphys`` module. Initially it's set to \"utf-8\", but it's\nallowed to set it to a different encoding name from user code.\n\n\nPython 3\n========\n\nWhile **Morphys** is mostly meant to solve issues of string types handling in\nPython 2, it's fully compatible with Python 3 and can be used with it.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Smart conversions between unicode and bytes types for common cases",
    "version": "1.0",
    "project_urls": {
        "Homepage": "https://github.com/mkalinski/morphys"
    },
    "split_keywords": [
        "string",
        "unicode",
        "bytes",
        "conversion"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f94fcb781d0ac5d079adabc77dc4f0bc99fc81c390029bd33c6e70552139e762",
                "md5": "1895a73507ad4c680a45c8f5cab24265",
                "sha256": "76d6dbaa4d65f597e59d332c81da786d83e4669387b9b2a750cfec74e7beec20"
            },
            "downloads": -1,
            "filename": "morphys-1.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1895a73507ad4c680a45c8f5cab24265",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 5618,
            "upload_time": "2017-01-10T20:08:56",
            "upload_time_iso_8601": "2017-01-10T20:08:56.872735Z",
            "url": "https://files.pythonhosted.org/packages/f9/4f/cb781d0ac5d079adabc77dc4f0bc99fc81c390029bd33c6e70552139e762/morphys-1.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2017-01-10 20:08:56",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "mkalinski",
    "github_project": "morphys",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "morphys"
}
        
Elapsed time: 0.07185s