| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import pytest |
| 26 |
|
| 27 |
from datetime import datetime |
| 28 |
|
| 29 |
from pymeasure.experiment.results import replace_placeholders |
| 30 |
from pymeasure.experiment.procedure import Procedure |
| 31 |
from pymeasure.experiment import Parameter, BooleanParameter, FloatParameter |
| 32 |
|
| 33 |
|
| 34 |
def test_replace_placeholders(): |
| 35 |
class FakeProcedure(Procedure): |
| 36 |
str_param = Parameter("String Parameter") |
| 37 |
bool_param = BooleanParameter("Boolean Parameter") |
| 38 |
float_param = FloatParameter("Float Parameter") |
| 39 |
|
| 40 |
fake = FakeProcedure() |
| 41 |
fake.set_parameters({ |
| 42 |
"str_param": "test", |
| 43 |
"bool_param": False, |
| 44 |
"float_param": 1.252 |
| 45 |
}) |
| 46 |
|
| 47 |
assert replace_placeholders("{String Parameter}", fake) == "test" |
| 48 |
assert replace_placeholders("{Boolean Parameter}", fake) == "False" |
| 49 |
assert replace_placeholders("{Float Parameter:.2f}", fake) == "1.25" |
| 50 |
assert replace_placeholders( |
| 51 |
"{String Parameter}_{Float Parameter}_{Boolean Parameter}", fake) == "test_1.252_False" |
| 52 |
|
| 53 |
with pytest.raises(KeyError): |
| 54 |
replace_placeholders("{Unknown Parameter}", fake) |
| 55 |
|
| 56 |
date_format = "%Y-%m" |
| 57 |
time_format = "%H:%M" |
| 58 |
now = datetime.now() |
| 59 |
date = now.strftime(date_format) |
| 60 |
time = now.strftime(time_format) |
| 61 |
assert replace_placeholders( |
| 62 |
"{date}--{time}", fake, date_format=date_format, time_format=time_format |
| 63 |
) == date + '--' + time |