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.

format.py 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. def shorten(file, to_delete, cut=False):
  3. if file.endswith("index.rst") or file.endswith("conf.py"):
  4. return
  5. res = []
  6. with open(file, "r") as fin:
  7. lines = fin.readlines()
  8. for line in lines:
  9. if cut and line.rstrip() == "Submodules":
  10. break
  11. else:
  12. res.append(line.rstrip())
  13. for i, line in enumerate(res):
  14. if line.endswith(" package"):
  15. res[i] = res[i][:-len(" package")]
  16. res[i + 1] = res[i + 1][:-len(" package")]
  17. elif line.endswith(" module"):
  18. res[i] = res[i][:-len(" module")]
  19. res[i + 1] = res[i + 1][:-len(" module")]
  20. else:
  21. for name in to_delete:
  22. if line.endswith(name):
  23. res[i] = "del"
  24. with open(file, "w") as fout:
  25. for line in res:
  26. if line != "del":
  27. print(line, file=fout)
  28. def clear(path='./source/'):
  29. files = os.listdir(path)
  30. to_delete = [
  31. "fastNLP.core.dist_trainer",
  32. "fastNLP.core.predictor",
  33. "fastNLP.io.file_reader",
  34. "fastNLP.io.config_io",
  35. "fastNLP.embeddings.contextual_embedding",
  36. "fastNLP.modules.dropout",
  37. "fastNLP.models.base_model",
  38. "fastNLP.models.bert",
  39. "fastNLP.models.enas_utils",
  40. "fastNLP.models.enas_controller",
  41. "fastNLP.models.enas_model",
  42. "fastNLP.models.enas_trainer",
  43. ]
  44. for file in files:
  45. if not os.path.isdir(path + file):
  46. res = file.split('.')
  47. if len(res) > 4:
  48. to_delete.append(file[:-4])
  49. elif len(res) == 4:
  50. shorten(path + file, to_delete, True)
  51. else:
  52. shorten(path + file, to_delete)
  53. for file in to_delete:
  54. os.remove(path + file + ".rst")
  55. clear()