File: definitions.py

package info (click to toggle)
python-pint 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 1,916 kB
  • sloc: python: 20,307; makefile: 149
file content (47 lines) | stat: -rw-r--r-- 1,297 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
pint.definitions
~~~~~~~~~~~~~~~~

Kept for backwards compatibility

:copyright: 2022 by Pint Authors, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import annotations

import flexparser as fp

from . import errors
from .delegates import ParserConfig, txt_defparser


class Definition:
    """This is kept for backwards compatibility"""

    @classmethod
    def from_string(cls, input_string: str, non_int_type: type = float) -> Definition:
        """Parse a string into a definition object.

        Parameters
        ----------
        input_string
            Single line string.
        non_int_type
            Numerical type used for non integer values.

        Raises
        ------
        DefinitionSyntaxError
            If a syntax error was found.
        """
        cfg = ParserConfig(non_int_type)
        parser = txt_defparser.DefParser(cfg, None)
        pp = parser.parse_string(input_string)
        for definition in parser.iter_parsed_project(pp):
            if isinstance(definition, Exception):
                raise errors.DefinitionSyntaxError(str(definition))
            if not isinstance(definition, (fp.BOS, fp.BOF)):
                return definition

        # TODO: What shall we do in this return path.