You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_config_saver.py 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import os
  2. import unittest
  3. import configparser
  4. import json
  5. from fastNLP.loader.config_loader import ConfigSection, ConfigLoader
  6. from fastNLP.saver.config_saver import ConfigSaver
  7. class TestConfigSaver(unittest.TestCase):
  8. def test_case_1(self):
  9. config_file_dir = "./test/loader/"
  10. config_file_name = "config"
  11. config_file_path = os.path.join(config_file_dir, config_file_name)
  12. tmp_config_file_path = os.path.join(config_file_dir, "tmp_config")
  13. with open(config_file_path, "r") as f:
  14. lines = f.readlines()
  15. standard_section = ConfigSection()
  16. t_section = ConfigSection()
  17. ConfigLoader(config_file_path).load_config(config_file_path, {"test": standard_section, "t": t_section})
  18. config_saver = ConfigSaver(config_file_path)
  19. section = ConfigSection()
  20. section["doubles"] = 0.8
  21. section["tt"] = 0.5
  22. section["test"] = 105
  23. section["str"] = "this is a str"
  24. test_case_2_section = section
  25. test_case_2_section["double"] = 0.5
  26. for k in section.__dict__.keys():
  27. standard_section[k] = section[k]
  28. config_saver.save_config_file("test", section)
  29. config_saver.save_config_file("another-test", section)
  30. config_saver.save_config_file("one-another-test", section)
  31. config_saver.save_config_file("test-case-2", section)
  32. test_section = ConfigSection()
  33. at_section = ConfigSection()
  34. another_test_section = ConfigSection()
  35. one_another_test_section = ConfigSection()
  36. a_test_case_2_section = ConfigSection()
  37. ConfigLoader(config_file_path).load_config(config_file_path, {"test": test_section,
  38. "another-test": another_test_section,
  39. "t": at_section,
  40. "one-another-test": one_another_test_section,
  41. "test-case-2": a_test_case_2_section})
  42. assert test_section == standard_section
  43. assert at_section == t_section
  44. assert another_test_section == section
  45. assert one_another_test_section == section
  46. assert a_test_case_2_section == test_case_2_section
  47. config_saver.save_config_file("test", section)
  48. with open(config_file_path, "w") as f:
  49. f.writelines(lines)
  50. with open(tmp_config_file_path, "w") as f:
  51. f.write('[test]\n')
  52. f.write('this is an fault example\n')
  53. tmp_config_saver = ConfigSaver(tmp_config_file_path)
  54. try:
  55. tmp_config_saver._read_section()
  56. except Exception as e:
  57. pass
  58. os.remove(tmp_config_file_path)
  59. try:
  60. tmp_config_saver = ConfigSaver("file-NOT-exist")
  61. except Exception as e:
  62. pass

一款轻量级的自然语言处理(NLP)工具包,目标是减少用户项目中的工程型代码,例如数据处理循环、训练循环、多卡运行等