Skip to content

Commit 81e11b2

Browse files
author
Lukas Fehring
committed
Change row identification in ResultProcessor.
Instead of checking keyfields the experiment_id is utilized.
1 parent 50d7f5e commit 81e11b2

File tree

3 files changed

+19
-24
lines changed

3 files changed

+19
-24
lines changed

py_experimenter/experimenter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,14 +387,14 @@ def _execution_wrapper(self,
387387
:raises NoExperimentsLeftError: If there are no experiments left to be executed.
388388
:raises DatabaseConnectionError: If an error occurred during the connection to the database.
389389
"""
390-
_, keyfield_values = self.dbconnector.get_experiment_configuration(random_order)
390+
experiment_id, keyfield_values = self.dbconnector.get_experiment_configuration(random_order)
391391

392392
result_field_names = utils.get_result_field_names(self.config)
393393
custom_fields = dict(self.config.items('CUSTOM')) if self.has_section('CUSTOM') else None
394394
table_name = self.get_config_value('PY_EXPERIMENTER', 'table')
395395

396396
result_processor = ResultProcessor(self.config, self.database_credential_file_path, table_name=table_name,
397-
condition=keyfield_values, result_fields=result_field_names)
397+
result_fields=result_field_names, experiment_id = experiment_id)
398398
result_processor._set_name(self.name)
399399
result_processor._set_machine(socket.gethostname())
400400

py_experimenter/result_processor.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class ResultProcessor:
2424
database.
2525
"""
2626

27-
def __init__(self, _config: dict, credential_path, table_name: str, condition: dict, result_fields: List[str]):
27+
def __init__(self, _config: dict, credential_path, table_name: str, result_fields: List[str], experiment_id: int):
2828
self._table_name = table_name
29-
self._where = ' AND '.join([f"{str(key)}='{str(value)}'" for key, value in condition.items()])
3029
self._result_fields = result_fields
3130
self._config = _config
3231
self._timestamp_on_result_fields = utils.timestamps_for_result_fields(self._config)
32+
self._experiment_id = experiment_id
3333

3434
if _config['PY_EXPERIMENTER']['provider'] == 'sqlite':
3535
self._dbconnector = DatabaseConnectorLITE(_config)
@@ -54,7 +54,7 @@ def process_results(self, results: dict) -> None:
5454

5555
keys = self._dbconnector.escape_sql_chars(*list(results.keys()))
5656
values = self._dbconnector.escape_sql_chars(*list(results.values()))
57-
self._dbconnector._update_database(keys=keys, values=values, where=self._where)
57+
self._dbconnector._update_database(keys=keys, values=values, where=f'ID = {self._experiment_id}')
5858

5959
@staticmethod
6060
def _add_timestamps_to_results(results: dict, time: datetime) -> List[Tuple[str, object]]:
@@ -69,19 +69,16 @@ def _change_status(self, status):
6969
time = time.strftime("%m/%d/%Y, %H:%M:%S")
7070

7171
if status == 'done' or status == 'error':
72-
self._dbconnector._update_database(keys=['status', 'end_date'], values=[status, time], where=self._where)
72+
self._dbconnector._update_database(keys=['status', 'end_date'], values=[status, time], where=f'ID = {self._experiment_id}')
7373

7474
def _write_error(self, error_msg):
75-
self._dbconnector._update_database(keys=['error'], values=[error_msg], where=self._where)
75+
self._dbconnector._update_database(keys=['error'], values=[error_msg], where=f'ID = {self._experiment_id}')
7676

7777
def _set_machine(self, machine_id):
78-
self._dbconnector._update_database(keys=['machine'], values=[machine_id], where=self._where)
78+
self._dbconnector._update_database(keys=['machine'], values=[machine_id], where=f'ID = {self._experiment_id}')
7979

8080
def _set_name(self, name):
81-
self._dbconnector._update_database(keys=['name'], values=[name], where=self._where)
82-
83-
def _not_executed_yet(self) -> bool:
84-
return self._dbconnector.not_executed_yet(where=self._where)
81+
self._dbconnector._update_database(keys=['name'], values=[name], where=f'ID = {self._experiment_id}')
8582

8683
def _valid_result_fields(self, result_fields):
8784
return set(result_fields).issubset(set(self._result_fields))

test/test_result_processor.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,27 @@
1616
@patch.object(database_connector_mysql.DatabaseConnectorMYSQL, '_test_connection')
1717
@patch.object(database_connector_mysql.DatabaseConnectorMYSQL, '_create_database_if_not_existing')
1818
@pytest.mark.parametrize(
19-
'config, table_name, condition, result_fields, expected_provider',
19+
'config, table_name, result_fields, expected_provider',
2020
[
2121
(
2222
utils.load_config(os.path.join('test', 'test_config_files', 'load_config_test_file', 'my_sql_test_file.cfg')),
2323
'test_table',
24-
{'test': 'condition'},
2524
['result_field_1', 'result_field_2'],
2625
DatabaseConnectorMYSQL
2726
),
2827
(
2928
utils.load_config(os.path.join('test', 'test_config_files', 'load_config_test_file', 'sqlite_test_file.cfg')),
3029
'test_table',
31-
{'test': 'condition'},
3230
['result_field_1', 'result_field_2'],
3331
DatabaseConnectorLITE
3432
),
3533
]
3634
)
37-
def test_init(create_database_if_not_existing_mock, test_connection_mysql, test_connection_sqlite, config, table_name, condition, result_fields, expected_provider):
35+
def test_init(create_database_if_not_existing_mock, test_connection_mysql, test_connection_sqlite, config, table_name, result_fields, expected_provider):
3836
create_database_if_not_existing_mock.return_value = None
3937
test_connection_mysql.return_value = None
4038
test_connection_sqlite.return_value = None
41-
result_processor = ResultProcessor(config, CREDENTIAL_PATH, table_name, condition, result_fields)
39+
result_processor = ResultProcessor(config, CREDENTIAL_PATH, table_name, result_fields, 0)
4240

4341
assert table_name == result_processor._table_name
4442
assert result_fields == result_processor._result_fields
@@ -60,7 +58,7 @@ def test_init_raises_error(mock_fn):
6058
@patch.object(database_connector_mysql.DatabaseConnectorMYSQL, '_test_connection')
6159
@patch.object(database_connector_mysql.DatabaseConnectorMYSQL, '_create_database_if_not_existing')
6260
@pytest.mark.parametrize(
63-
'result_fields, results, error, errorstring',
61+
'result_fields, results, error, errorstring, experiment_id',
6462
[
6563
(
6664
[
@@ -71,18 +69,18 @@ def test_init_raises_error(mock_fn):
7169
'result_field_2': 'result_field_2_value',
7270
},
7371
InvalidResultFieldError,
74-
f"Invalid result keys: {{'result_field_2'}}"
72+
f"Invalid result keys: {{'result_field_2'}}",
73+
0
7574
),
7675
]
7776
)
78-
def test_process_results_raises_error(create_database_mock, test_connection_mock, result_fields, results, error, errorstring):
77+
def test_process_results_raises_error(create_database_mock, test_connection_mock, result_fields, results, error, errorstring, experiment_id):
7978
create_database_mock.return_value = None
8079
test_connection_mock.return_value = None
8180
table_name = 'test_table'
82-
condition = {'test': 'condition'}
8381
config = utils.load_config(os.path.join('test', 'test_config_files', 'load_config_test_file', 'my_sql_test_file.cfg'))
8482

85-
result_processor = ResultProcessor(config, CREDENTIAL_PATH, table_name, condition, result_fields)
83+
result_processor = ResultProcessor(config, CREDENTIAL_PATH, table_name, result_fields, experiment_id)
8684

8785
with pytest.raises(error, match=errorstring):
8886
result_processor.process_results(results)
@@ -102,8 +100,8 @@ def test_valid_result_fields(create_database_if_not_existing_mock, test_connecti
102100
create_database_if_not_existing_mock.return_value = None
103101
test_connection_mock.return_value = None
104102
mock_config = utils.load_config(os.path.join('test', 'test_config_files', 'load_config_test_file', 'my_sql_test_file.cfg'))
105-
assert subset_boolean == ResultProcessor(mock_config, CREDENTIAL_PATH, 'test_table_name', {
106-
'test_condition_key': 'test_condition_value'}, used_result_fields)._valid_result_fields(existing_result_fields)
103+
assert subset_boolean == ResultProcessor(mock_config, CREDENTIAL_PATH, 'test_table_name',
104+
used_result_fields, 0)._valid_result_fields(existing_result_fields)
107105

108106

109107
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)