def test_yaml_write_sorting(tmpdir): temp_dir = str(tmpdir) data = { "a": 1, "c": 2, "b": 3, } sorted_yaml_file = random_file("yaml") file_utils.write_yaml(temp_dir, sorted_yaml_file, data, sort_keys=True) expected_sorted = """a: 1 b: 3 c: 2 """ with open(os.path.join(temp_dir, sorted_yaml_file), "r") as f: actual_sorted = f.read() assert actual_sorted == expected_sorted unsorted_yaml_file = random_file("yaml") file_utils.write_yaml(temp_dir, unsorted_yaml_file, data, sort_keys=False) expected_unsorted = """a: 1 c: 2 b: 3 """ with open(os.path.join(temp_dir, unsorted_yaml_file), "r") as f: actual_unsorted = f.read() assert actual_unsorted == expected_unsorted
def test_yaml_read_and_write(tmpdir): temp_dir = str(tmpdir) yaml_file = random_file("yaml") long_value = long(1) if six.PY2 else 1 # pylint: disable=undefined-variable data = { "a": random_int(), "B": random_int(), "text_value": u"中文", "long_value": long_value, "int_value": 32, "text_value_2": u"hi" } file_utils.write_yaml(temp_dir, yaml_file, data) read_data = file_utils.read_yaml(temp_dir, yaml_file) assert data == read_data yaml_path = os.path.join(temp_dir, yaml_file) with codecs.open(yaml_path, encoding="utf-8") as handle: contents = handle.read() assert "!!python" not in contents # Check that UTF-8 strings are written properly to the file (rather than as ASCII # representations of their byte sequences). assert u"中文" in contents def edit_func(old_dict): old_dict["more_text"] = u"西班牙语" return old_dict assert "more_text" not in file_utils.read_yaml(temp_dir, yaml_file) with safe_edit_yaml(temp_dir, yaml_file, edit_func): editted_dict = file_utils.read_yaml(temp_dir, yaml_file) assert "more_text" in editted_dict assert editted_dict["more_text"] == u"西班牙语" assert "more_text" not in file_utils.read_yaml(temp_dir, yaml_file)
def test_creation_and_hydration(self): exp_id = random_int() name = "exp_%d_%d" % (random_int(), random_int()) location = random_file(".json") exp = Experiment(exp_id, name, location) self._check(exp, exp_id, name, location) as_dict = {"experiment_id": exp_id, "name": name, "artifact_location": location} self.assertEqual(dict(exp), as_dict) proto = exp.to_proto() exp2 = Experiment.from_proto(proto) self._check(exp2, exp_id, name, location) exp3 = Experiment.from_dictionary(as_dict) self._check(exp3, exp_id, name, location)
def test_yaml_read_and_write(self): yaml_file = random_file("yaml") long_value = long(1) if six.PY2 else 1 # pylint: disable=undefined-variable data = { "a": random_int(), "B": random_int(), "text_value": u"中文", "long_value": long_value, "int_value": 32, "text_value_2": u"hi" } file_utils.write_yaml(self.test_folder, yaml_file, data) read_data = file_utils.read_yaml(self.test_folder, yaml_file) self.assertEqual(data, read_data) yaml_path = file_utils.build_path(self.test_folder, yaml_file) with codecs.open(yaml_path, encoding="utf-8") as handle: contents = handle.read() self.assertNotIn("!!python", contents) # Check that UTF-8 strings are written properly to the file (rather than as ASCII # representations of their byte sequences). self.assertIn(u"中文", contents)
def test_creation_and_hydration(self): exp_id = str(random_int()) name = "exp_%d_%d" % (random_int(), random_int()) lifecycle_stage = LifecycleStage.ACTIVE location = random_file(".json") exp = Experiment(exp_id, name, location, lifecycle_stage) self._check(exp, exp_id, name, location, lifecycle_stage) as_dict = { "experiment_id": exp_id, "name": name, "artifact_location": location, "lifecycle_stage": lifecycle_stage } self.assertEqual(dict(exp), as_dict) proto = exp.to_proto() exp2 = Experiment.from_proto(proto) self._check(exp2, exp_id, name, location, lifecycle_stage) exp3 = Experiment.from_dictionary(as_dict) self._check(exp3, exp_id, name, location, lifecycle_stage)
def test_yaml_read_and_write(self): yaml_file = random_file("yaml") data = {"a": random_int(), "B": random_int()} file_utils.write_yaml(self.test_folder, yaml_file, data) read_data = file_utils.read_yaml(self.test_folder, yaml_file) self.assertEqual(data, read_data)