File: context.py

package info (click to toggle)
python-pint 0.25.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,940 kB
  • sloc: python: 20,478; makefile: 148
file content (203 lines) | stat: -rw-r--r-- 6,216 bytes parent folder | download
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
pint.delegates.txt_defparser.context
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Definitions for parsing Context and their related objects

Notices that some of the checks are done within the
format agnostic parent definition class.

See each one for a slighly longer description of the
syntax.

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

from __future__ import annotations

import numbers
import re
import typing as ty
from dataclasses import dataclass
from typing import Union

import flexparser as fp

from ...facets.context import definitions
from ..base_defparser import ParserConfig, PintParsedStatement
from . import block, common, plain

# TODO check syntax
T = ty.TypeVar("T", bound="Union[ForwardRelation, BidirectionalRelation]")


def _from_string_and_context_sep(
    cls: type[T], s: str, config: ParserConfig, separator: str
) -> T | None:
    if separator not in s:
        return None
    if ":" not in s:
        return None

    rel, eq = s.split(":")

    parts = rel.split(separator)

    src, dst = (config.to_dimension_container(s) for s in parts)

    return cls(src, dst, eq.strip())


@dataclass(frozen=True)
class ForwardRelation(PintParsedStatement, definitions.ForwardRelation):
    """A relation connecting a dimension to another via a transformation function.

    <source dimension> -> <target dimension>: <transformation function>
    """

    @classmethod
    def from_string_and_config(
        cls, s: str, config: ParserConfig
    ) -> fp.NullableParsedResult[ForwardRelation]:
        return _from_string_and_context_sep(cls, s, config, "->")


@dataclass(frozen=True)
class BidirectionalRelation(PintParsedStatement, definitions.BidirectionalRelation):
    """A bidirectional relation connecting a dimension to another
    via a simple transformation function.

        <source dimension> <-> <target dimension>: <transformation function>

    """

    @classmethod
    def from_string_and_config(
        cls, s: str, config: ParserConfig
    ) -> fp.NullableParsedResult[BidirectionalRelation]:
        return _from_string_and_context_sep(cls, s, config, "<->")


@dataclass(frozen=True)
class BeginContext(PintParsedStatement):
    """Being of a context directive.

    @context[(defaults)] <canonical name> [= <alias>] [= <alias>]
    """

    _header_re = re.compile(
        r"@context\s*(?P<defaults>\(.*\))?\s+(?P<name>\w+)\s*(=(?P<aliases>.*))*"
    )

    name: str
    aliases: tuple[str, ...]
    defaults: dict[str, numbers.Number]

    @classmethod
    def from_string_and_config(
        cls, s: str, config: ParserConfig
    ) -> fp.NullableParsedResult[BeginContext]:
        try:
            r = cls._header_re.search(s)
            if r is None:
                return None
            name = r.groupdict()["name"].strip()
            aliases = r.groupdict()["aliases"]
            if aliases:
                aliases = tuple(a.strip() for a in r.groupdict()["aliases"].split("="))
            else:
                aliases = ()
            defaults = r.groupdict()["defaults"]
        except Exception as exc:
            return common.DefinitionSyntaxError(
                f"Could not parse the Context header '{s}': {exc}"
            )

        if defaults:
            txt = defaults
            try:
                defaults = (part.split("=") for part in defaults.strip("()").split(","))
                defaults = {str(k).strip(): config.to_number(v) for k, v in defaults}
            except (ValueError, TypeError) as exc:
                return common.DefinitionSyntaxError(
                    f"Could not parse Context definition defaults '{txt}' {exc}"
                )
        else:
            defaults = {}

        return cls(name, tuple(aliases), defaults)


@dataclass(frozen=True)
class ContextDefinition(
    block.DirectiveBlock[
        definitions.ContextDefinition,
        BeginContext,
        ty.Union[
            plain.CommentDefinition,
            BidirectionalRelation,
            ForwardRelation,
            plain.UnitDefinition,
        ],
    ]
):
    """Definition of a Context

        @context[(defaults)] <canonical name> [= <alias>] [= <alias>]
            # units can be redefined within the context
            <redefined unit> = <relation to another unit>

            # can establish unidirectional relationships between dimensions
            <dimension 1> -> <dimension 2>: <transformation function>

            # can establish bidirectionl relationships between dimensions
            <dimension 3> <-> <dimension 4>: <transformation function>
        @end

    See BeginContext, Equality, ForwardRelation, BidirectionalRelation and
    Comment for more parsing related information.

    Example::

        @context(n=1) spectroscopy = sp
            # n index of refraction of the medium.
            [length] <-> [frequency]: speed_of_light / n / value
            [frequency] -> [energy]: planck_constant * value
            [energy] -> [frequency]: value / planck_constant
            # allow wavenumber / kayser
            [wavenumber] <-> [length]: 1 / value
        @end
    """

    def derive_definition(self) -> definitions.ContextDefinition:
        return definitions.ContextDefinition(
            self.name, self.aliases, self.defaults, self.relations, self.redefinitions
        )

    @property
    def name(self) -> str:
        assert isinstance(self.opening, BeginContext)
        return self.opening.name

    @property
    def aliases(self) -> tuple[str, ...]:
        assert isinstance(self.opening, BeginContext)
        return self.opening.aliases

    @property
    def defaults(self) -> dict[str, numbers.Number]:
        assert isinstance(self.opening, BeginContext)
        return self.opening.defaults

    @property
    def relations(self) -> tuple[BidirectionalRelation | ForwardRelation, ...]:
        return tuple(
            r
            for r in self.body
            if isinstance(r, (ForwardRelation, BidirectionalRelation))
        )

    @property
    def redefinitions(self) -> tuple[plain.UnitDefinition, ...]:
        return tuple(r for r in self.body if isinstance(r, plain.UnitDefinition))