|
1 | 1 | import re |
2 | 2 | from decimal import Decimal |
3 | 3 |
|
4 | | -import fhirpathpy.engine.util as util |
5 | | -import fhirpathpy.engine.nodes as nodes |
| 4 | +from fhirpathpy.engine import nodes, util |
6 | 5 |
|
7 | 6 | # This file holds code to hande the FHIRPath Existence functions (5.1 in the |
8 | 7 | # specification). |
@@ -36,10 +35,10 @@ def to_integer(ctx, coll): |
36 | 35 |
|
37 | 36 | value = util.get_data(coll[0]) |
38 | 37 |
|
39 | | - if value == False: |
| 38 | + if value is False: |
40 | 39 | return 0 |
41 | 40 |
|
42 | | - if value == True: |
| 41 | + if value is True: |
43 | 42 | return 1 |
44 | 43 |
|
45 | 44 | if util.is_number(value): |
@@ -72,7 +71,7 @@ def to_quantity(ctx, coll, to_unit=None): |
72 | 71 | v = util.val_data_converted(coll[0]) |
73 | 72 | quantity_regex_res = None |
74 | 73 |
|
75 | | - if isinstance(v, (int, Decimal)): |
| 74 | + if isinstance(v, int | Decimal): |
76 | 75 | result = nodes.FP_Quantity(v, "'1'") |
77 | 76 | elif isinstance(v, nodes.FP_Quantity): |
78 | 77 | result = v |
@@ -211,10 +210,10 @@ def to_boolean(ctx, coll): |
211 | 210 |
|
212 | 211 | if var_type == "bool": |
213 | 212 | return val |
214 | | - elif var_type == "int" or var_type == "float": |
215 | | - if val == 1 or val == 1.0: |
| 213 | + elif var_type in ("int", "float"): |
| 214 | + if val in (1, 1.0): |
216 | 215 | return True |
217 | | - elif val == 0 or val == 0.0: |
| 216 | + elif val in (0, 0.0): |
218 | 217 | return False |
219 | 218 | elif var_type == "str": |
220 | 219 | lower_case_var = val.lower() |
@@ -248,17 +247,13 @@ def string_singleton(coll): |
248 | 247 |
|
249 | 248 | def singleton(coll, type): |
250 | 249 | if len(coll) > 1: |
251 | | - raise Exception( |
252 | | - "Unexpected collection {coll}; expected singleton of type {type}".format( |
253 | | - coll=coll, type=type |
254 | | - ) |
255 | | - ) |
| 250 | + raise Exception(f"Unexpected collection {coll}; expected singleton of type {type}") |
256 | 251 | elif len(coll) == 0: |
257 | 252 | return [] |
258 | 253 | to_singleton = singleton_eval_by_type[type] |
259 | 254 | if to_singleton: |
260 | 255 | val = to_singleton(coll) |
261 | 256 | if val is not None: |
262 | 257 | return val |
263 | | - raise Exception("Expected {type}, but got: {coll}".format(type=type.lower(), coll=coll)) |
264 | | - raise Exception("Not supported type {}".format(type)) |
| 258 | + raise Exception(f"Expected {type.lower()}, but got: {coll}") |
| 259 | + raise Exception(f"Not supported type {type}") |
0 commit comments