wraps-parse


Namewraps-parse JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryParsing feature of wraps.
upload_time2024-08-05 18:35:35
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024-present, Nikita Tikhonov (nekitdev) 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 parse python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # `wraps-parse`

[![License][License Badge]][License]
[![Version][Version Badge]][Package]
[![Downloads][Downloads Badge]][Package]
[![Discord][Discord Badge]][Discord]

[![Documentation][Documentation Badge]][Documentation]
[![Check][Check Badge]][Actions]
[![Test][Test Badge]][Actions]
[![Coverage][Coverage Badge]][Coverage]

> *Parsing feature of wraps.*

## Installing

**Python 3.8 or above is required.**

### `pip`

Installing the library with `pip` is quite simple:

```console
$ pip install wraps-parse
```

Alternatively, the library can be installed from the source:

```console
$ pip install git+https://github.com/nekitdev/wraps-parse.git
```

Or via cloning the repository:

```console
$ git clone https://github.com/nekitdev/wraps-parse.git
$ cd wraps-parse
$ pip install .
```

### `uv`

You can add `wraps-parse` as a dependency with the following command:

```console
$ uv add wraps-parse
```

## Examples

### Format

Implementing [`ToString`][wraps_parse.format.ToString] requires defining
the [`to_string`][wraps_parse.format.ToString.to_string]
(and optionally [`to_short_string`][wraps_parse.format.ToString.to_short_string]) method:

```python
# format.py

from attrs import field, frozen
from attrs.validators import ge
from wraps_parse import ToString

is_digits = str.isdigit  # will be used later


@frozen()
class Integer(ToString):
    value: int = field(validator=ge(0))

    def to_string(self) -> str:
        return str(self.value)
```

### Simple

Implementing [`SimpleFromString`][wraps_parse.simple.SimpleFromString] requires defining
the [`from_string`][wraps_parse.simple.SimpleFromString.from_string] method returning an
[`Option[Self]`][wraps_core.option.Option]:

```python
# simple.py

from attrs import frozen
from typing_extensions import Self
from wraps_core import NULL, Option, Some
from wraps_parse import SimpleFromString

from format import Integer, is_digits


@frozen()
class SimpleInteger(SimpleFromString, Integer):
    @classmethod
    def from_string(cls, string: str) -> Option[Self]:
        return Some(cls(int(string))) if is_digits(string) else NULL
```

And using it:

```python
>>> from simple import SimpleInteger
>>> SimpleInteger.from_string("42")
Some(SimpleInteger(value=42))
>>> SimpleInteger.from_string("foo")
Null()
```

The [`parse`][wraps_parse.simple.SimpleFromString.parse] method is provided to raise
[`SimpleParseError`][wraps_parse.simple.SimpleParseError] if parsing fails:

```python
>>> from simple import SimpleInteger
>>> SimpleInteger.parse("42")
SimpleInteger(value=42)
>>> SimpleInteger.parse("foo")
Traceback (most recent call last):
  ...
wraps_parse.simple.SimpleParseError: parsing `foo` into `SimpleInteger` failed
```

### Normal

The [`FromString`][wraps_parse.normal.FromString] protocol is similar to
[`SimpleFromString`][wraps_parse.simple.SimpleFromString], except
[`Result[Self, E]`][wraps_core.result.Result] (where `E` is the error type)
is returned from [`from_string`][wraps_parse.normal.FromString.from_string] instead.

Below is the enhanced version of the code above:

```python
# normal.py

from attrs import frozen
from typing_extensions import Self
from wraps_parse import FromString
from wraps_core import Error, Ok, Result

from format import Integer, is_digits

NON_DIGIT = "non-digit character found"


@frozen()
class NormalInteger(FromString[str], Integer):  # `E` is `str`
    @classmethod
    def from_string(cls, string: str) -> Result[Self, str]:
        return Ok(cls(int(string))) if is_digits(string) else Error(NON_DIGIT)
```

And using it:

```python
>>> from normal import NormalInteger
>>> NormalInteger.from_string("13")
Ok(NormalInteger(value=13))
>>> NormalInteger.from_string("bar")
Error("non-digit character found")
```

The [`parse`][wraps_parse.normal.FromString.parse] method is provided to raise
[`ParseError`][wraps_parse.normal.ParseError] if parsing fails:

```python
>>> from normal import NormalInteger
>>> NormalInteger.parse("13")
NormalInteger(value=13)
>>> NormalInteger.parse("bar")
Traceback (most recent call last):
  ...
wraps_parse.normal.ParseError: parsing `bar` into `NormalInteger` failed (non-digit character found)
```

### Note

Please note that [`SimpleFromString`][wraps_parse.simple.SimpleFromString] and
[`FromString`][wraps_parse.normal.FromString] are intentionally incompatible with each other.

One can implement either, not both.

Also, when implementing both parsing and formatting, make sure the methods implemented are
inverses of each other.

## Documentation

You can find the documentation [here][Documentation].

## Support

If you need support with the library, you can send us an [email][Email]
or refer to the official [Discord server][Discord].

## Changelog

You can find the changelog [here][Changelog].

## Security Policy

You can find the Security Policy of `wraps-parse` [here][Security].

## Contributing

If you are interested in contributing to `wraps-parse`, make sure to take a look at the
[Contributing Guide][Contributing Guide], as well as the [Code of Conduct][Code of Conduct].

## License

`wraps-parse` is licensed under the MIT License terms. See [License][License] for details.

[Email]: mailto:support@nekit.dev

[Discord]: https://nekit.dev/chat

[Actions]: https://github.com/nekitdev/wraps-parse/actions

[Changelog]: https://github.com/nekitdev/wraps-parse/blob/main/CHANGELOG.md
[Code of Conduct]: https://github.com/nekitdev/wraps-parse/blob/main/CODE_OF_CONDUCT.md
[Contributing Guide]: https://github.com/nekitdev/wraps-parse/blob/main/CONTRIBUTING.md
[Security]: https://github.com/nekitdev/wraps-parse/blob/main/SECURITY.md

[License]: https://github.com/nekitdev/wraps-parse/blob/main/LICENSE

[Package]: https://pypi.org/project/wraps-parse
[Coverage]: https://codecov.io/gh/nekitdev/wraps-parse
[Documentation]: https://nekitdev.github.io/wraps-parse

[Discord Badge]: https://img.shields.io/discord/728012506899021874
[License Badge]: https://img.shields.io/pypi/l/wraps-parse
[Version Badge]: https://img.shields.io/pypi/v/wraps-parse
[Downloads Badge]: https://img.shields.io/pypi/dm/wraps-parse

[Documentation Badge]: https://github.com/nekitdev/wraps-parse/workflows/docs/badge.svg
[Check Badge]: https://github.com/nekitdev/wraps-parse/workflows/check/badge.svg
[Test Badge]: https://github.com/nekitdev/wraps-parse/workflows/test/badge.svg
[Coverage Badge]: https://codecov.io/gh/nekitdev/wraps-parse/branch/main/graph/badge.svg

[wraps_core.option.Option]: https://nekitdev.github.io/wraps-core/reference/option#wraps_core.option.Option
[wraps_core.result.Result]: https://nekitdev.github.io/wraps-core/reference/result#wraps_core.result.Result

[wraps_parse.simple.SimpleFromString]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleFromString
[wraps_parse.simple.SimpleParseError]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleParseError
[wraps_parse.simple.SimpleFromString.from_string]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleFromString.from_string
[wraps_parse.simple.SimpleFromString.parse]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleFromString.parse

[wraps_parse.normal.FromString]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.FromString
[wraps_parse.normal.ParseError]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.ParseError
[wraps_parse.normal.FromString.from_string]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.FromString.from_string
[wraps_parse.normal.FromString.parse]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.FromString.parse

[wraps_parse.format.ToString]: https://nekitdev.github.io/wraps-parse/reference/format#wraps_parse.format.ToString
[wraps_parse.format.ToString.to_string]: https://nekitdev.github.io/wraps-parse/reference/format#wraps_parse.format.ToString.to_string
[wraps_parse.format.ToString.to_short_string]: https://nekitdev.github.io/wraps-parse/reference/format#wraps_parse.format.ToString.to_short_string

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "wraps-parse",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "parse, python",
    "author": null,
    "author_email": "nekitdev <nekit@nekit.dev>",
    "download_url": "https://files.pythonhosted.org/packages/d5/c1/9aa46118f888e4019a96ec959b6777ddd7a83fbb64648eee9cfcd364dab8/wraps_parse-0.1.0.tar.gz",
    "platform": null,
    "description": "# `wraps-parse`\n\n[![License][License Badge]][License]\n[![Version][Version Badge]][Package]\n[![Downloads][Downloads Badge]][Package]\n[![Discord][Discord Badge]][Discord]\n\n[![Documentation][Documentation Badge]][Documentation]\n[![Check][Check Badge]][Actions]\n[![Test][Test Badge]][Actions]\n[![Coverage][Coverage Badge]][Coverage]\n\n> *Parsing feature of wraps.*\n\n## Installing\n\n**Python 3.8 or above is required.**\n\n### `pip`\n\nInstalling the library with `pip` is quite simple:\n\n```console\n$ pip install wraps-parse\n```\n\nAlternatively, the library can be installed from the source:\n\n```console\n$ pip install git+https://github.com/nekitdev/wraps-parse.git\n```\n\nOr via cloning the repository:\n\n```console\n$ git clone https://github.com/nekitdev/wraps-parse.git\n$ cd wraps-parse\n$ pip install .\n```\n\n### `uv`\n\nYou can add `wraps-parse` as a dependency with the following command:\n\n```console\n$ uv add wraps-parse\n```\n\n## Examples\n\n### Format\n\nImplementing [`ToString`][wraps_parse.format.ToString] requires defining\nthe [`to_string`][wraps_parse.format.ToString.to_string]\n(and optionally [`to_short_string`][wraps_parse.format.ToString.to_short_string]) method:\n\n```python\n# format.py\n\nfrom attrs import field, frozen\nfrom attrs.validators import ge\nfrom wraps_parse import ToString\n\nis_digits = str.isdigit  # will be used later\n\n\n@frozen()\nclass Integer(ToString):\n    value: int = field(validator=ge(0))\n\n    def to_string(self) -> str:\n        return str(self.value)\n```\n\n### Simple\n\nImplementing [`SimpleFromString`][wraps_parse.simple.SimpleFromString] requires defining\nthe [`from_string`][wraps_parse.simple.SimpleFromString.from_string] method returning an\n[`Option[Self]`][wraps_core.option.Option]:\n\n```python\n# simple.py\n\nfrom attrs import frozen\nfrom typing_extensions import Self\nfrom wraps_core import NULL, Option, Some\nfrom wraps_parse import SimpleFromString\n\nfrom format import Integer, is_digits\n\n\n@frozen()\nclass SimpleInteger(SimpleFromString, Integer):\n    @classmethod\n    def from_string(cls, string: str) -> Option[Self]:\n        return Some(cls(int(string))) if is_digits(string) else NULL\n```\n\nAnd using it:\n\n```python\n>>> from simple import SimpleInteger\n>>> SimpleInteger.from_string(\"42\")\nSome(SimpleInteger(value=42))\n>>> SimpleInteger.from_string(\"foo\")\nNull()\n```\n\nThe [`parse`][wraps_parse.simple.SimpleFromString.parse] method is provided to raise\n[`SimpleParseError`][wraps_parse.simple.SimpleParseError] if parsing fails:\n\n```python\n>>> from simple import SimpleInteger\n>>> SimpleInteger.parse(\"42\")\nSimpleInteger(value=42)\n>>> SimpleInteger.parse(\"foo\")\nTraceback (most recent call last):\n  ...\nwraps_parse.simple.SimpleParseError: parsing `foo` into `SimpleInteger` failed\n```\n\n### Normal\n\nThe [`FromString`][wraps_parse.normal.FromString] protocol is similar to\n[`SimpleFromString`][wraps_parse.simple.SimpleFromString], except\n[`Result[Self, E]`][wraps_core.result.Result] (where `E` is the error type)\nis returned from [`from_string`][wraps_parse.normal.FromString.from_string] instead.\n\nBelow is the enhanced version of the code above:\n\n```python\n# normal.py\n\nfrom attrs import frozen\nfrom typing_extensions import Self\nfrom wraps_parse import FromString\nfrom wraps_core import Error, Ok, Result\n\nfrom format import Integer, is_digits\n\nNON_DIGIT = \"non-digit character found\"\n\n\n@frozen()\nclass NormalInteger(FromString[str], Integer):  # `E` is `str`\n    @classmethod\n    def from_string(cls, string: str) -> Result[Self, str]:\n        return Ok(cls(int(string))) if is_digits(string) else Error(NON_DIGIT)\n```\n\nAnd using it:\n\n```python\n>>> from normal import NormalInteger\n>>> NormalInteger.from_string(\"13\")\nOk(NormalInteger(value=13))\n>>> NormalInteger.from_string(\"bar\")\nError(\"non-digit character found\")\n```\n\nThe [`parse`][wraps_parse.normal.FromString.parse] method is provided to raise\n[`ParseError`][wraps_parse.normal.ParseError] if parsing fails:\n\n```python\n>>> from normal import NormalInteger\n>>> NormalInteger.parse(\"13\")\nNormalInteger(value=13)\n>>> NormalInteger.parse(\"bar\")\nTraceback (most recent call last):\n  ...\nwraps_parse.normal.ParseError: parsing `bar` into `NormalInteger` failed (non-digit character found)\n```\n\n### Note\n\nPlease note that [`SimpleFromString`][wraps_parse.simple.SimpleFromString] and\n[`FromString`][wraps_parse.normal.FromString] are intentionally incompatible with each other.\n\nOne can implement either, not both.\n\nAlso, when implementing both parsing and formatting, make sure the methods implemented are\ninverses of each other.\n\n## Documentation\n\nYou can find the documentation [here][Documentation].\n\n## Support\n\nIf you need support with the library, you can send us an [email][Email]\nor refer to the official [Discord server][Discord].\n\n## Changelog\n\nYou can find the changelog [here][Changelog].\n\n## Security Policy\n\nYou can find the Security Policy of `wraps-parse` [here][Security].\n\n## Contributing\n\nIf you are interested in contributing to `wraps-parse`, make sure to take a look at the\n[Contributing Guide][Contributing Guide], as well as the [Code of Conduct][Code of Conduct].\n\n## License\n\n`wraps-parse` is licensed under the MIT License terms. See [License][License] for details.\n\n[Email]: mailto:support@nekit.dev\n\n[Discord]: https://nekit.dev/chat\n\n[Actions]: https://github.com/nekitdev/wraps-parse/actions\n\n[Changelog]: https://github.com/nekitdev/wraps-parse/blob/main/CHANGELOG.md\n[Code of Conduct]: https://github.com/nekitdev/wraps-parse/blob/main/CODE_OF_CONDUCT.md\n[Contributing Guide]: https://github.com/nekitdev/wraps-parse/blob/main/CONTRIBUTING.md\n[Security]: https://github.com/nekitdev/wraps-parse/blob/main/SECURITY.md\n\n[License]: https://github.com/nekitdev/wraps-parse/blob/main/LICENSE\n\n[Package]: https://pypi.org/project/wraps-parse\n[Coverage]: https://codecov.io/gh/nekitdev/wraps-parse\n[Documentation]: https://nekitdev.github.io/wraps-parse\n\n[Discord Badge]: https://img.shields.io/discord/728012506899021874\n[License Badge]: https://img.shields.io/pypi/l/wraps-parse\n[Version Badge]: https://img.shields.io/pypi/v/wraps-parse\n[Downloads Badge]: https://img.shields.io/pypi/dm/wraps-parse\n\n[Documentation Badge]: https://github.com/nekitdev/wraps-parse/workflows/docs/badge.svg\n[Check Badge]: https://github.com/nekitdev/wraps-parse/workflows/check/badge.svg\n[Test Badge]: https://github.com/nekitdev/wraps-parse/workflows/test/badge.svg\n[Coverage Badge]: https://codecov.io/gh/nekitdev/wraps-parse/branch/main/graph/badge.svg\n\n[wraps_core.option.Option]: https://nekitdev.github.io/wraps-core/reference/option#wraps_core.option.Option\n[wraps_core.result.Result]: https://nekitdev.github.io/wraps-core/reference/result#wraps_core.result.Result\n\n[wraps_parse.simple.SimpleFromString]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleFromString\n[wraps_parse.simple.SimpleParseError]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleParseError\n[wraps_parse.simple.SimpleFromString.from_string]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleFromString.from_string\n[wraps_parse.simple.SimpleFromString.parse]: https://nekitdev.github.io/wraps-parse/reference/simple#wraps_parse.simple.SimpleFromString.parse\n\n[wraps_parse.normal.FromString]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.FromString\n[wraps_parse.normal.ParseError]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.ParseError\n[wraps_parse.normal.FromString.from_string]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.FromString.from_string\n[wraps_parse.normal.FromString.parse]: https://nekitdev.github.io/wraps-parse/reference/normal#wraps_parse.normal.FromString.parse\n\n[wraps_parse.format.ToString]: https://nekitdev.github.io/wraps-parse/reference/format#wraps_parse.format.ToString\n[wraps_parse.format.ToString.to_string]: https://nekitdev.github.io/wraps-parse/reference/format#wraps_parse.format.ToString.to_string\n[wraps_parse.format.ToString.to_short_string]: https://nekitdev.github.io/wraps-parse/reference/format#wraps_parse.format.ToString.to_short_string\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024-present, Nikita Tikhonov (nekitdev)  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": "Parsing feature of wraps.",
    "version": "0.1.0",
    "project_urls": {
        "Chat": "https://nekit.dev/chat",
        "Documentation": "https://nekitdev.github.io/wraps-parse",
        "Funding": "https://nekit.dev/funding",
        "Homepage": "https://github.com/nekitdev/wraps-parse",
        "Issues": "https://github.com/nekitdev/wraps-parse/issues",
        "Repository": "https://github.com/nekitdev/wraps-parse"
    },
    "split_keywords": [
        "parse",
        " python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "72d95fcc786c04924c0a01c74775369fe2d5eac542ba1312abf8141d82738412",
                "md5": "aeb5e61e0e3be3b187fec54e037f64f4",
                "sha256": "1d529826983ef1cad178440ee756558ee4c3eca6e0bf17b092dceec1746e6a07"
            },
            "downloads": -1,
            "filename": "wraps_parse-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "aeb5e61e0e3be3b187fec54e037f64f4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7854,
            "upload_time": "2024-08-05T18:35:34",
            "upload_time_iso_8601": "2024-08-05T18:35:34.185039Z",
            "url": "https://files.pythonhosted.org/packages/72/d9/5fcc786c04924c0a01c74775369fe2d5eac542ba1312abf8141d82738412/wraps_parse-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d5c19aa46118f888e4019a96ec959b6777ddd7a83fbb64648eee9cfcd364dab8",
                "md5": "510f4ab52848747454d589a6ae32f391",
                "sha256": "4e1a278eb718329406347a3806f15f658545704cd26c34f3b3ccb7f3ae59c0cd"
            },
            "downloads": -1,
            "filename": "wraps_parse-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "510f4ab52848747454d589a6ae32f391",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 15205,
            "upload_time": "2024-08-05T18:35:35",
            "upload_time_iso_8601": "2024-08-05T18:35:35.483803Z",
            "url": "https://files.pythonhosted.org/packages/d5/c1/9aa46118f888e4019a96ec959b6777ddd7a83fbb64648eee9cfcd364dab8/wraps_parse-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-05 18:35:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "nekitdev",
    "github_project": "wraps-parse",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "wraps-parse"
}
        
Elapsed time: 0.36258s