stringparser


Namestringparser JSON
Version 0.7 PyPI version JSON
download
home_page
SummaryEasy to use pattern matching and information extraction
upload_time2023-11-16 00:08:35
maintainer
docs_urlNone
author
requires_python>=3.9
licenseBSD-3-Clause
keywords string parsing pep3101 regex
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            [![Latest Version](https://img.shields.io/pypi/v/stringparser.svg)](https://pypi.python.org/pypi/stringparser)
[![image](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)
[![License](https://img.shields.io/pypi/l/stringparser.svg)](https://pypi.python.org/pypi/stringparser)
[![Python Versions](https://img.shields.io/pypi/pyversions/stringparser.svg)](https://pypi.python.org/pypi/stringparser)
[![CI](https://github.com/hgrecco/stringparser/workflows/CI/badge.svg)](https://github.com/hgrecco/stringparser/actions?query=workflow%3ACI)
[![LINTER](https://github.com/hgrecco/stringparser/workflows/Lint/badge.svg)](https://github.com/hgrecco/stringparser/actions?query=workflow%3ALint)
[![Coverage](https://coveralls.io/repos/github/hgrecco/stringparser/badge.svg?branch=master)](https://coveralls.io/github/hgrecco/stringparser?branch=master)

# Motivation

The `stringparser` module provides a simple way to match patterns and
extract information within strings. As patterns are given using the
familiar format string specification [3101](https://peps.python.org/pep-3101/)., writing them is much easier than writing regular
expressions (albeit less powerful).

Just install it using:

```bash
pip install stringparser
```

# Examples

You can build a reusable parser object:

```python
>>> from stringparser import Parser
>>> parser = Parser('The answer is {:d}')
>>> parser('The answer is 42')
42
>>> parser('The answer is 54')
54
```

Or directly:

```python
>>> Parser('The answer is {:d}')('The answer is 42')
42
```

You can retrieve many fields:

```python
>>> Parser('The {:s} is {:d}')('The answer is 42')
['answer', 42]
```

And you can use numbered fields to order the returned list:

```python
>>> Parser('The {1:s} is {0:d}')('The answer is 42')
[42, 'answer']
```

Or named fields to return an OrderedDict:

```python
>>> Parser('The {a:s} is {b:d}')('The answer is 42')
{'a': 'answer', 'b': 42}
```

You can ignore some fields using _ as a name:

```python
>>> Parser('The {_:s} is {:d}')('The answer is 42')
42
```

You can parse into an object attribute:

```python
>>> obj = Parser('The {0.name:s} is {0.value:d}')('The answer is 42')
>>> obj.name
'answer'
>>> obj.value
'42'
```

You can parse even parse into an nested structues:

```python
>>> obj = Parser('The {0.content[name]:s} is {0.content[value]:d}')('The answer is 42')
>>> obj.content
{'name': 'answer', 'value': 42}
```

# Limitations

- From the format string:
  `[[fill]align][sign][#][0][width][,][.precision][type]`
  only `[type]`, `[sign]` and `[#]` are
  currently implemented. This might cause trouble to match certain
  notation like: decimal `-4` written as `- 4`.
- Lines are matched from beginning to end. `{:d}` will NOT return all
  the numbers in the string. Use regex for that.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "stringparser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "string,parsing,PEP3101,regex",
    "author": "",
    "author_email": "\"Hernan E. Grecco\" <hernan.grecco@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/ac/c0/57c5e808f68ed9a822d83413a3516f06a7751dc35e7039252e25994bf654/stringparser-0.7.tar.gz",
    "platform": null,
    "description": "[![Latest Version](https://img.shields.io/pypi/v/stringparser.svg)](https://pypi.python.org/pypi/stringparser)\n[![image](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)\n[![License](https://img.shields.io/pypi/l/stringparser.svg)](https://pypi.python.org/pypi/stringparser)\n[![Python Versions](https://img.shields.io/pypi/pyversions/stringparser.svg)](https://pypi.python.org/pypi/stringparser)\n[![CI](https://github.com/hgrecco/stringparser/workflows/CI/badge.svg)](https://github.com/hgrecco/stringparser/actions?query=workflow%3ACI)\n[![LINTER](https://github.com/hgrecco/stringparser/workflows/Lint/badge.svg)](https://github.com/hgrecco/stringparser/actions?query=workflow%3ALint)\n[![Coverage](https://coveralls.io/repos/github/hgrecco/stringparser/badge.svg?branch=master)](https://coveralls.io/github/hgrecco/stringparser?branch=master)\n\n# Motivation\n\nThe `stringparser` module provides a simple way to match patterns and\nextract information within strings. As patterns are given using the\nfamiliar format string specification [3101](https://peps.python.org/pep-3101/)., writing them is much easier than writing regular\nexpressions (albeit less powerful).\n\nJust install it using:\n\n```bash\npip install stringparser\n```\n\n# Examples\n\nYou can build a reusable parser object:\n\n```python\n>>> from stringparser import Parser\n>>> parser = Parser('The answer is {:d}')\n>>> parser('The answer is 42')\n42\n>>> parser('The answer is 54')\n54\n```\n\nOr directly:\n\n```python\n>>> Parser('The answer is {:d}')('The answer is 42')\n42\n```\n\nYou can retrieve many fields:\n\n```python\n>>> Parser('The {:s} is {:d}')('The answer is 42')\n['answer', 42]\n```\n\nAnd you can use numbered fields to order the returned list:\n\n```python\n>>> Parser('The {1:s} is {0:d}')('The answer is 42')\n[42, 'answer']\n```\n\nOr named fields to return an OrderedDict:\n\n```python\n>>> Parser('The {a:s} is {b:d}')('The answer is 42')\n{'a': 'answer', 'b': 42}\n```\n\nYou can ignore some fields using _ as a name:\n\n```python\n>>> Parser('The {_:s} is {:d}')('The answer is 42')\n42\n```\n\nYou can parse into an object attribute:\n\n```python\n>>> obj = Parser('The {0.name:s} is {0.value:d}')('The answer is 42')\n>>> obj.name\n'answer'\n>>> obj.value\n'42'\n```\n\nYou can parse even parse into an nested structues:\n\n```python\n>>> obj = Parser('The {0.content[name]:s} is {0.content[value]:d}')('The answer is 42')\n>>> obj.content\n{'name': 'answer', 'value': 42}\n```\n\n# Limitations\n\n- From the format string:\n  `[[fill]align][sign][#][0][width][,][.precision][type]`\n  only `[type]`, `[sign]` and `[#]` are\n  currently implemented. This might cause trouble to match certain\n  notation like: decimal `-4` written as `- 4`.\n- Lines are matched from beginning to end. `{:d}` will NOT return all\n  the numbers in the string. Use regex for that.\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "Easy to use pattern matching and information extraction",
    "version": "0.7",
    "project_urls": {
        "Bug Tracker": "https://github.com/hgrecco/stringparser/issues",
        "Homepage": "https://github.com/hgrecco/stringparser"
    },
    "split_keywords": [
        "string",
        "parsing",
        "pep3101",
        "regex"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "338d1ce822279ed69e5e5e83826b9f61d37ea146ffaf54b615d34ca363eb0aaa",
                "md5": "5d2bca7f892f8bfb0863aaad30f169e3",
                "sha256": "44678ca6067a5c0cb4c45da38448e1971e2b6fb136b82bfb6a798bdbda7dd71f"
            },
            "downloads": -1,
            "filename": "stringparser-0.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5d2bca7f892f8bfb0863aaad30f169e3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 7866,
            "upload_time": "2023-11-16T00:08:34",
            "upload_time_iso_8601": "2023-11-16T00:08:34.488453Z",
            "url": "https://files.pythonhosted.org/packages/33/8d/1ce822279ed69e5e5e83826b9f61d37ea146ffaf54b615d34ca363eb0aaa/stringparser-0.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "acc057c5e808f68ed9a822d83413a3516f06a7751dc35e7039252e25994bf654",
                "md5": "025b1e3c5d5cc030bc77c064cd026011",
                "sha256": "bf37aac0166fc8f5705b450cc332f0d323e2d8b8191017bbfebe1f489f18f64d"
            },
            "downloads": -1,
            "filename": "stringparser-0.7.tar.gz",
            "has_sig": false,
            "md5_digest": "025b1e3c5d5cc030bc77c064cd026011",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 10211,
            "upload_time": "2023-11-16T00:08:35",
            "upload_time_iso_8601": "2023-11-16T00:08:35.958167Z",
            "url": "https://files.pythonhosted.org/packages/ac/c0/57c5e808f68ed9a822d83413a3516f06a7751dc35e7039252e25994bf654/stringparser-0.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-16 00:08:35",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hgrecco",
    "github_project": "stringparser",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "stringparser"
}
        
Elapsed time: 1.08687s