strto


Namestrto JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummaryType-guided parsing library for converting strings to Python objects
upload_time2024-07-21 12:44:31
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT License Copyright (c) 2023 Žiga Ivanšek Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords strto python object parser python string to object parser python type hint parser python string parser string parsing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # strto
[![Tests](https://github.com/zigai/strto/actions/workflows/tests.yml/badge.svg)](https://github.com/zigai/strto/actions/workflows/tests.yml)
[![PyPI version](https://badge.fury.io/py/strto.svg)](https://badge.fury.io/py/strto)
![Supported versions](https://img.shields.io/badge/python-3.10+-blue.svg)
[![Downloads](https://static.pepy.tech/badge/strto)](https://pepy.tech/project/strto)
[![license](https://img.shields.io/github/license/zigai/strto.svg)](https://github.com/zigai/strto/blob/master/LICENSE)

`strto` is a Python library for parsing strings into Python objects based on types and type annotations.

## Installation
#### From PyPi
```
pip install strto
```
#### From source
```
pip install git+https://github.com/zigai/strto.git
```

## Examples
```python
>>> from strto import get_parser
>>> parser = get_parser()

>>> parser.parse("5", int)
5
>>> parser.parse("1.5", int | float)
1.5
>>> parser.parse("1,2,3,4,5", list[int])
[1, 2, 3, 4, 5]
>>> parser.parse('{"a":1,"b":2,"c":3}', dict[str, int])
{'a': 1, 'b': 2, 'c': 3}

import datetime
>>> parser.parse("2022.07.19", datetime.date)
datetime.date(2022, 7, 19)

>>> parser.parse("0:5:1", range)
range(0, 5, 1)

>>> import enum
>>> class Color(enum.Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
>>> parser.parse("RED", Color)
Color.RED

```
### Add custom parser
```python
from dataclasses import dataclass
from strto import ParserBase, get_parser

@dataclass
class NetworkAddress:
    host: str
    port: int

class NetworkAddressParser(ParserBase):
    def parse(self, value: str) -> NetworkAddress:
        host, port = value.rsplit(":")
        return NetworkAddress(host=host, port=int(port))

parser = get_parser()
parser.add(NetworkAddress, NetworkAddressParser())
result = parser.parse("example.com:8080", NetworkAddress)
print(result)  # NetworkAddress(host='example.com', port=8080)

# You can also use a function
def parse_network_address(value: str) -> NetworkAddress:
    host, port = value.rsplit(":")
    return NetworkAddress(host=host, port=int(port))

parser = get_parser()
parser.add(NetworkAddress, parse_network_address)
result = parser.parse("example.com:8080", NetworkAddress)
print(result)  # NetworkAddress(host='example.com', port=8080)

```

## License
[MIT License](https://github.com/zigai/strto/blob/master/LICENSE)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "strto",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "strto, python object parser, python string to object, parser, python type hint parser, python string parser, string parsing",
    "author": null,
    "author_email": "\u017diga Ivan\u0161ek <ziga.ivansek@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/13/2b/024bc9d6ee0fd64e7f81bb0a5409576102091c09ed30e2298fb7ade5771d/strto-0.1.4.tar.gz",
    "platform": null,
    "description": "# strto\n[![Tests](https://github.com/zigai/strto/actions/workflows/tests.yml/badge.svg)](https://github.com/zigai/strto/actions/workflows/tests.yml)\n[![PyPI version](https://badge.fury.io/py/strto.svg)](https://badge.fury.io/py/strto)\n![Supported versions](https://img.shields.io/badge/python-3.10+-blue.svg)\n[![Downloads](https://static.pepy.tech/badge/strto)](https://pepy.tech/project/strto)\n[![license](https://img.shields.io/github/license/zigai/strto.svg)](https://github.com/zigai/strto/blob/master/LICENSE)\n\n`strto` is a Python library for parsing strings into Python objects based on types and type annotations.\n\n## Installation\n#### From PyPi\n```\npip install strto\n```\n#### From source\n```\npip install git+https://github.com/zigai/strto.git\n```\n\n## Examples\n```python\n>>> from strto import get_parser\n>>> parser = get_parser()\n\n>>> parser.parse(\"5\", int)\n5\n>>> parser.parse(\"1.5\", int | float)\n1.5\n>>> parser.parse(\"1,2,3,4,5\", list[int])\n[1, 2, 3, 4, 5]\n>>> parser.parse('{\"a\":1,\"b\":2,\"c\":3}', dict[str, int])\n{'a': 1, 'b': 2, 'c': 3}\n\nimport datetime\n>>> parser.parse(\"2022.07.19\", datetime.date)\ndatetime.date(2022, 7, 19)\n\n>>> parser.parse(\"0:5:1\", range)\nrange(0, 5, 1)\n\n>>> import enum\n>>> class Color(enum.Enum):\n...     RED = 1\n...     GREEN = 2\n...     BLUE = 3\n>>> parser.parse(\"RED\", Color)\nColor.RED\n\n```\n### Add custom parser\n```python\nfrom dataclasses import dataclass\nfrom strto import ParserBase, get_parser\n\n@dataclass\nclass NetworkAddress:\n    host: str\n    port: int\n\nclass NetworkAddressParser(ParserBase):\n    def parse(self, value: str) -> NetworkAddress:\n        host, port = value.rsplit(\":\")\n        return NetworkAddress(host=host, port=int(port))\n\nparser = get_parser()\nparser.add(NetworkAddress, NetworkAddressParser())\nresult = parser.parse(\"example.com:8080\", NetworkAddress)\nprint(result)  # NetworkAddress(host='example.com', port=8080)\n\n# You can also use a function\ndef parse_network_address(value: str) -> NetworkAddress:\n    host, port = value.rsplit(\":\")\n    return NetworkAddress(host=host, port=int(port))\n\nparser = get_parser()\nparser.add(NetworkAddress, parse_network_address)\nresult = parser.parse(\"example.com:8080\", NetworkAddress)\nprint(result)  # NetworkAddress(host='example.com', port=8080)\n\n```\n\n## License\n[MIT License](https://github.com/zigai/strto/blob/master/LICENSE)\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2023 \u017diga Ivan\u0161ek  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Type-guided parsing library for converting strings to Python objects",
    "version": "0.1.4",
    "project_urls": {
        "Repository": "https://github.com/zigai/strto"
    },
    "split_keywords": [
        "strto",
        " python object parser",
        " python string to object",
        " parser",
        " python type hint parser",
        " python string parser",
        " string parsing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f669aeaeea972d475f93f9f8d50879f88e4ab19ba267c082c52a36fd33029169",
                "md5": "fde2da14e18b66db6c14c91456197adb",
                "sha256": "0ea5614822576b9192e049b1340cadf68b35729ec7699aa66c25e54a0ddfc484"
            },
            "downloads": -1,
            "filename": "strto-0.1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fde2da14e18b66db6c14c91456197adb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 7143,
            "upload_time": "2024-07-21T12:44:29",
            "upload_time_iso_8601": "2024-07-21T12:44:29.832580Z",
            "url": "https://files.pythonhosted.org/packages/f6/69/aeaeea972d475f93f9f8d50879f88e4ab19ba267c082c52a36fd33029169/strto-0.1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "132b024bc9d6ee0fd64e7f81bb0a5409576102091c09ed30e2298fb7ade5771d",
                "md5": "dbcadfaba077494278f30f8d3f530740",
                "sha256": "964adfb7633a2adfdbf8a47be724f35ef731b1103765adf2352bda0582d2d821"
            },
            "downloads": -1,
            "filename": "strto-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "dbcadfaba077494278f30f8d3f530740",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 8851,
            "upload_time": "2024-07-21T12:44:31",
            "upload_time_iso_8601": "2024-07-21T12:44:31.060367Z",
            "url": "https://files.pythonhosted.org/packages/13/2b/024bc9d6ee0fd64e7f81bb0a5409576102091c09ed30e2298fb7ade5771d/strto-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-21 12:44:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "zigai",
    "github_project": "strto",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "strto"
}
        
Elapsed time: 0.32504s