• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nielstron / quantulum3 / 931

pending completion
931

push

travis-ci-com

nielstron
Merge branch 'dev'

467 of 467 new or added lines in 14 files covered. (100.0%)

1812 of 1847 relevant lines covered (98.11%)

4.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.73
/quantulum3/classes.py
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
"""
5✔
4
:mod:`Quantulum` classes.
5
"""
6

7
from typing import Any, Dict, List, Optional, Tuple
5✔
8

9
from . import speak
5✔
10

11

12
###############################################################################
13
class Entity(object):
5✔
14
    """
15
    Class for an entity (e.g. "volume").
16
    """
17

18
    def __init__(
5✔
19
        self,
20
        name: str,
21
        dimensions: List[Dict[str, Any]] = [],
22
        uri: Optional[str] = None,
23
    ):
24
        self.name = name
5✔
25
        self.dimensions = dimensions
5✔
26
        self.uri = uri
5✔
27

28
    def __repr__(self):
29
        msg = 'Entity(name="%s", uri=%s)'
30
        msg = msg % (self.name, self.uri)
31
        return msg
32

33
    def __eq__(self, other):
5✔
34
        if isinstance(other, self.__class__):
5✔
35
            return self.name == other.name and self.dimensions == other.dimensions
5✔
36
        else:
37
            return False
×
38

39
    def __ne__(self, other):
5✔
40
        return not self.__eq__(other)
×
41

42
    def __hash__(self):
5✔
43
        return hash(repr(self))
5✔
44

45

46
###############################################################################
47
class Unit(object):
5✔
48
    """
49
    Class for a unit (e.g. "gallon").
50
    """
51

52
    def __init__(
5✔
53
        self,
54
        name: str,
55
        entity: Entity,
56
        surfaces: List[str] = [],
57
        uri: Optional[str] = None,
58
        symbols: List[str] = [],
59
        dimensions: List[Dict[str, Any]] = [],
60
        currency_code: Optional[str] = None,
61
        original_dimensions: Optional[List[Dict[str, Any]]] = None,
62
        lang="en_US",
63
    ):
64
        """Initialization method."""
65
        self.name = name
5✔
66
        self.surfaces = surfaces
5✔
67
        self.entity = entity
5✔
68
        self.uri = uri
5✔
69
        self.symbols = symbols
5✔
70
        self.dimensions = dimensions
5✔
71
        # Stores the untampered dimensions that were parsed from the text
72
        self.original_dimensions = original_dimensions
5✔
73
        self.currency_code = currency_code
5✔
74
        self.lang = lang
5✔
75

76
    def to_spoken(self, count=1, lang=None) -> str:
5✔
77
        """
78
        Convert a given unit to the unit in words, correctly inflected.
79
        :param count: The value of the quantity (i.e. 1 for one watt, 2 for
80
                      two seconds)
81
        :param lang: Language of result
82
        :return: A string with the correctly inflected spoken version of the
83
                 unit
84
        """
85
        return speak.unit_to_spoken(self, count, lang or self.lang)
5✔
86

87
    def __repr__(self):
88
        msg = 'Unit(name="%s", entity=Entity("%s"), uri=%s)'
89
        msg = msg % (self.name, self.entity.name, self.uri)
90
        return msg
91

92
    def __str__(self):
93
        return self.to_spoken()
94

95
    def __eq__(self, other):
5✔
96
        if isinstance(other, self.__class__):
5✔
97
            return (
5✔
98
                self.name == other.name
99
                and self.entity == other.entity
100
                and all(
101
                    dim1["base"] == dim2["base"] and dim1["power"] == dim2["power"]
102
                    for dim1, dim2 in zip(self.dimensions, other.dimensions)
103
                )
104
            )
105
        else:
106
            return False
×
107

108
    def __ne__(self, other):
5✔
109
        return not self.__eq__(other)
5✔
110

111
    def __hash__(self):
5✔
112
        return hash(repr(self))
5✔
113

114

115
###############################################################################
116

117

118
class Quantity(object):
5✔
119
    """
120
    Class for a quantity (e.g. "4.2 gallons").
121
    """
122

123
    def __init__(
5✔
124
        self,
125
        value: float,
126
        unit: Unit,
127
        surface: Optional[str] = None,
128
        span: Optional[Tuple[int, int]] = None,
129
        uncertainty: Optional[float] = None,
130
        lang="en_US",
131
    ):
132
        self.value = value
5✔
133
        self.unit = unit
5✔
134
        self.surface = surface
5✔
135
        self.span = span
5✔
136
        self.uncertainty = uncertainty
5✔
137
        self.lang = lang
5✔
138

139
    def with_vals(
5✔
140
        self,
141
        value=None,
142
        unit=None,
143
        surface=None,
144
        span=None,
145
        uncertainty=None,
146
        lang=None,
147
    ):
148
        return Quantity(
5✔
149
            value if value is not None else self.value,
150
            unit if unit is not None else self.unit,
151
            surface if surface is not None else self.surface,
152
            span if span is not None else self.span,
153
            uncertainty if uncertainty is not None else self.uncertainty,
154
            lang if lang is not None else self.lang,
155
        )
156

157
    def __repr__(self):
158
        msg = 'Quantity(%g, "%s")'
159
        msg = msg % (self.value, repr(self.unit))
160
        return msg
161

162
    def __eq__(self, other):
5✔
163
        if isinstance(other, self.__class__):
5✔
164
            return (
5✔
165
                self.value == other.value
166
                and self.unit == other.unit
167
                and self.surface == other.surface
168
                and self.span == other.span
169
                and self.uncertainty == other.uncertainty
170
            )
171
        else:
172
            return False
×
173

174
    def __ne__(self, other):
5✔
175
        return not self.__eq__(other)
5✔
176

177
    def __str__(self):
178
        return self.to_spoken(self.lang)
179

180
    def to_spoken(self, lang=None):
5✔
181
        """
182
        Express quantity as a speakable string
183
        :return: Speakable version of this quantity
184
        """
185
        return speak.quantity_to_spoken(self, lang or self.lang)
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc