mo-dots


Namemo-dots JSON
Version 10.672.25036 PyPI version JSON
download
home_pagehttps://github.com/klahnakoski/mo-dots
SummaryMore Dots! Dot-access to Python dicts like Javascript
upload_time2025-02-05 23:11:45
maintainerNone
docs_urlNone
authorKyle Lahnakoski
requires_pythonNone
licenseMPL 2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# More Dots!

[![PyPI Latest Release](https://img.shields.io/pypi/v/mo-dots.svg)](https://pypi.org/project/mo-dots/)
[![Build Status](https://github.com/klahnakoski/mo-dots/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/klahnakoski/mo-dots/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/github/klahnakoski/mo-dots/badge.svg?branch=dev)](https://coveralls.io/github/klahnakoski/mo-dots?branch=dev)
[![Downloads](https://static.pepy.tech/badge/mo-dots/month)](https://pepy.tech/project/mo-dots)

## Overview

Box your JSON-like data in a `Data` object to get null-safe dot access.  This library is a replacement for `dict` that is more consistent and easier to use.

> See [full documentation](https://github.com/klahnakoski/mo-dots/tree/dev/docs) for all the features of `mo-dots`

> See [change log](https://github.com/klahnakoski/mo-dots/tree/dev/docs/CHANGELOG.md) to read about major changes

### Create instances

Define `Data` using named parameters, just like you would a `dict`

    >>> from mo_dots import Data
    >>> Data(b=42, c="hello world")
    Data({'b': 42, 'c': 'hello world'})

You can also box an existing `dict`s so they can be used like `Data`

    >>> from mo_dots import to_data
    >>> to_data({'b': 42, 'c': 'hello world'})
    Data({'b': 42, 'c': 'hello world'})

### Dot Access

Access properties with attribute dots: `a.b == a["b"]`. You have probably seen this before.

### Path Access

Access properties by dot-delimited path.

	>>> a = to_data({"b": {"c": 42}})
	>>> a["b.c"] == 42
	True

### Null-Safe Access

If a property does not exist then return `Null` rather than raising an error.

	>>> a = Data()
	>>> a.b == Null
	True
	>>> a.b.c == Null
	True
	>>> a[None] == Null
	True

### Path assignment

No need to make intermediate `dicts`

    >>> a = Data()
    >>> a["b.c"] = 42   # same as a.b.c = 42
    a == {"b": {"c": 42}}

### Path accumulation

Use `+=` to add to a property; default zero (`0`)

    >>> a = Data()
    a == {}
    >>> a.b.c += 1
    a == {"b": {"c": 1}}
    >>> a.b.c += 42
    a == {"b": {"c": 43}}

Use `+=` with a list (`[]`) to append to a list; default empty list (`[]`)

    >>> a = Data()
    a == {}
    >>> a.b.c += [1]
    a == {"b": {"c": [1]}}
    >>> a.b.c += [42]
    a == {"b": {"c": [1, 42]}}

## Serializing to JSON

The standard Python JSON library does not recognize `Data` as serializable.  You may overcome this by providing `default=from_data`; which converts the data structures in this module into Python primitives of the same. 

    from mo_dots import from_data, to_data
    
    s = to_data({"a": ["b", 1]})
    result = json.dumps(s, default=from_data)  

Alternatively, you may consider [mo-json](https://pypi.org/project/mo-json/) which has a function `value2json` that converts a larger number of data structures into JSON.


## Summary

This library is the basis for a data transformation algebra: We want a succinct way of transforming data in Python. We want operations on data to result in yet more data. We do not want data operations to raise exceptions. This library also solves Python's lack of consistency (lack of closure) under the dot (`.`) and slice `[::]` operators when operating on data objects. 

[Full documentation](https://github.com/klahnakoski/mo-dots/tree/dev/docs)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/klahnakoski/mo-dots",
    "name": "mo-dots",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "Kyle Lahnakoski",
    "author_email": "kyle@lahnakoski.com",
    "download_url": "https://files.pythonhosted.org/packages/4f/16/0fc786791ef0ec4e6111b3d0ffb3e072596ea4592d84296e5c34aa6472ac/mo_dots-10.672.25036.tar.gz",
    "platform": null,
    "description": "\r\n# More Dots!\r\n\r\n[![PyPI Latest Release](https://img.shields.io/pypi/v/mo-dots.svg)](https://pypi.org/project/mo-dots/)\r\n[![Build Status](https://github.com/klahnakoski/mo-dots/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/klahnakoski/mo-dots/actions/workflows/build.yml)\r\n[![Coverage Status](https://coveralls.io/repos/github/klahnakoski/mo-dots/badge.svg?branch=dev)](https://coveralls.io/github/klahnakoski/mo-dots?branch=dev)\r\n[![Downloads](https://static.pepy.tech/badge/mo-dots/month)](https://pepy.tech/project/mo-dots)\r\n\r\n## Overview\r\n\r\nBox your JSON-like data in a `Data` object to get null-safe dot access.  This library is a replacement for `dict` that is more consistent and easier to use.\r\n\r\n> See [full documentation](https://github.com/klahnakoski/mo-dots/tree/dev/docs) for all the features of `mo-dots`\r\n\r\n> See [change log](https://github.com/klahnakoski/mo-dots/tree/dev/docs/CHANGELOG.md) to read about major changes\r\n\r\n### Create instances\r\n\r\nDefine `Data` using named parameters, just like you would a `dict`\r\n\r\n    >>> from mo_dots import Data\r\n    >>> Data(b=42, c=\"hello world\")\r\n    Data({'b': 42, 'c': 'hello world'})\r\n\r\nYou can also box an existing `dict`s so they can be used like `Data`\r\n\r\n    >>> from mo_dots import to_data\r\n    >>> to_data({'b': 42, 'c': 'hello world'})\r\n    Data({'b': 42, 'c': 'hello world'})\r\n\r\n### Dot Access\r\n\r\nAccess properties with attribute dots: `a.b == a[\"b\"]`. You have probably seen this before.\r\n\r\n### Path Access\r\n\r\nAccess properties by dot-delimited path.\r\n\r\n\t>>> a = to_data({\"b\": {\"c\": 42}})\r\n\t>>> a[\"b.c\"] == 42\r\n\tTrue\r\n\r\n### Null-Safe Access\r\n\r\nIf a property does not exist then return `Null` rather than raising an error.\r\n\r\n\t>>> a = Data()\r\n\t>>> a.b == Null\r\n\tTrue\r\n\t>>> a.b.c == Null\r\n\tTrue\r\n\t>>> a[None] == Null\r\n\tTrue\r\n\r\n### Path assignment\r\n\r\nNo need to make intermediate `dicts`\r\n\r\n    >>> a = Data()\r\n    >>> a[\"b.c\"] = 42   # same as a.b.c = 42\r\n    a == {\"b\": {\"c\": 42}}\r\n\r\n### Path accumulation\r\n\r\nUse `+=` to add to a property; default zero (`0`)\r\n\r\n    >>> a = Data()\r\n    a == {}\r\n    >>> a.b.c += 1\r\n    a == {\"b\": {\"c\": 1}}\r\n    >>> a.b.c += 42\r\n    a == {\"b\": {\"c\": 43}}\r\n\r\nUse `+=` with a list (`[]`) to append to a list; default empty list (`[]`)\r\n\r\n    >>> a = Data()\r\n    a == {}\r\n    >>> a.b.c += [1]\r\n    a == {\"b\": {\"c\": [1]}}\r\n    >>> a.b.c += [42]\r\n    a == {\"b\": {\"c\": [1, 42]}}\r\n\r\n## Serializing to JSON\r\n\r\nThe standard Python JSON library does not recognize `Data` as serializable.  You may overcome this by providing `default=from_data`; which converts the data structures in this module into Python primitives of the same. \r\n\r\n    from mo_dots import from_data, to_data\r\n    \r\n    s = to_data({\"a\": [\"b\", 1]})\r\n    result = json.dumps(s, default=from_data)  \r\n\r\nAlternatively, you may consider [mo-json](https://pypi.org/project/mo-json/) which has a function `value2json` that converts a larger number of data structures into JSON.\r\n\r\n\r\n## Summary\r\n\r\nThis library is the basis for a data transformation algebra: We want a succinct way of transforming data in Python. We want operations on data to result in yet more data. We do not want data operations to raise exceptions. This library also solves Python's lack of consistency (lack of closure) under the dot (`.`) and slice `[::]` operators when operating on data objects. \r\n\r\n[Full documentation](https://github.com/klahnakoski/mo-dots/tree/dev/docs)\r\n",
    "bugtrack_url": null,
    "license": "MPL 2.0",
    "summary": "More Dots! Dot-access to Python dicts like Javascript",
    "version": "10.672.25036",
    "project_urls": {
        "Homepage": "https://github.com/klahnakoski/mo-dots"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "48f91d60c7222f8c88f0cc9f39b62efdc993b829fbfef0b7f66e0e6fe0fefa90",
                "md5": "412b40e73992d8db24320a89c58f7b50",
                "sha256": "8794f8ac6f9de3c2b4ab0c32c8871194479fae04c63eae1388d30f1016562d1d"
            },
            "downloads": -1,
            "filename": "mo_dots-10.672.25036-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "412b40e73992d8db24320a89c58f7b50",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 27284,
            "upload_time": "2025-02-05T23:11:43",
            "upload_time_iso_8601": "2025-02-05T23:11:43.943834Z",
            "url": "https://files.pythonhosted.org/packages/48/f9/1d60c7222f8c88f0cc9f39b62efdc993b829fbfef0b7f66e0e6fe0fefa90/mo_dots-10.672.25036-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4f160fc786791ef0ec4e6111b3d0ffb3e072596ea4592d84296e5c34aa6472ac",
                "md5": "497564666c816d3a1a8639ac517bad1e",
                "sha256": "4bb18ff81055f74f2032b31834a1812ba1dd3ef1c7fa73022bd8d3c72bacf183"
            },
            "downloads": -1,
            "filename": "mo_dots-10.672.25036.tar.gz",
            "has_sig": false,
            "md5_digest": "497564666c816d3a1a8639ac517bad1e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 25044,
            "upload_time": "2025-02-05T23:11:45",
            "upload_time_iso_8601": "2025-02-05T23:11:45.138953Z",
            "url": "https://files.pythonhosted.org/packages/4f/16/0fc786791ef0ec4e6111b3d0ffb3e072596ea4592d84296e5c34aa6472ac/mo_dots-10.672.25036.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-05 23:11:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "klahnakoski",
    "github_project": "mo-dots",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "mo-dots"
}
        
Elapsed time: 7.01694s