debug.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import re
  5. from pathlib import Path
  6. from filelock import FileLock
  7. def remove_symbols(text):
  8. return re.sub(r'[^\w\s]', '', text)
  9. class Debugger:
  10. def _log_file(self) -> Path:
  11. file: Path = Path(f'./log/quiz/supplement.txt')
  12. if not file.parent.exists():
  13. file.parent.mkdir(parents=True)
  14. if not file.exists():
  15. file.touch()
  16. return file
  17. def append_one(self, question: str, options: list[str]):
  18. question = remove_symbols(question)
  19. options = [remove_symbols(option) for option in options]
  20. file = self._log_file()
  21. lock = FileLock(f"{file}.lock")
  22. line = f'{question},{options[0]},{options[1]},{options[2]},{options[3]}\n'
  23. with lock:
  24. with open(file, 'a', encoding='utf-8') as f:
  25. f.write(line)
  26. f.flush()
  27. def close_fn(self):
  28. return
  29. if __name__ == '__main__':
  30. a = Debugger()
  31. a.append_one('1', ['1', '2', '3', '4'])
  32. a.append_one('5', ['1', '2939/;.', '3', '4'])
  33. a.append_one('1', ['1', '2', '3', '4'])
  34. a.close_fn()