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.

count.py 3.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. def find_all(path='../fastNLP'):
  3. head_list = []
  4. alias_list = []
  5. for path, dirs, files in os.walk(path):
  6. for file in files:
  7. if file.endswith('.py'):
  8. name = ".".join(path.split('/')[1:])
  9. if file.split('.')[0] != "__init__":
  10. name = name + '.' + file.split('.')[0]
  11. if len(name.split('.')) < 3 or name.startswith('fastNLP.core'):
  12. heads, alias = find_one(path + '/' + file)
  13. for h in heads:
  14. head_list.append(name + "." + h)
  15. for a in alias:
  16. alias_list.append(a)
  17. heads = {}
  18. for h in head_list:
  19. end = h.split('.')[-1]
  20. file = h[:-len(end) - 1]
  21. if end not in heads:
  22. heads[end] = set()
  23. heads[end].add(file)
  24. alias = {}
  25. for a in alias_list:
  26. for each in a:
  27. end = each.split('.')[-1]
  28. file = each[:-len(end) - 1]
  29. if end not in alias:
  30. alias[end] = set()
  31. alias[end].add(file)
  32. print("IN alias NOT IN heads")
  33. for item in alias:
  34. if item not in heads:
  35. print(item, alias[item])
  36. elif len(heads[item]) != 2:
  37. print(item, alias[item], heads[item])
  38. print("\n\nIN heads NOT IN alias")
  39. for item in heads:
  40. if item not in alias:
  41. print(item, heads[item])
  42. def find_class(path):
  43. with open(path, 'r') as fin:
  44. lines = fin.readlines()
  45. pars = {}
  46. for i, line in enumerate(lines):
  47. if line.strip().startswith('class'):
  48. line = line.strip()[len('class'):-1].strip()
  49. if line[-1] == ')':
  50. line = line[:-1].split('(')
  51. name = line[0].strip()
  52. parents = line[1].split(',')
  53. for i in range(len(parents)):
  54. parents[i] = parents[i].strip()
  55. if len(parents) == 1:
  56. pars[name] = parents[0]
  57. else:
  58. pars[name] = tuple(parents)
  59. return pars
  60. def find_one(path):
  61. head_list = []
  62. alias = []
  63. with open(path, 'r') as fin:
  64. lines = fin.readlines()
  65. flag = False
  66. for i, line in enumerate(lines):
  67. if line.strip().startswith('__all__'):
  68. line = line.strip()[len('__all__'):].strip()
  69. if line[-1] == ']':
  70. line = line[1:-1].strip()[1:].strip()
  71. head_list.append(line.strip("\"").strip("\'").strip())
  72. else:
  73. flag = True
  74. elif line.strip() == ']':
  75. flag = False
  76. elif flag:
  77. line = line.strip()[:-1].strip("\"").strip("\'").strip()
  78. if len(line) == 0 or line[0] == '#':
  79. continue
  80. head_list.append(line)
  81. if line.startswith('def') or line.startswith('class'):
  82. if lines[i + 2].strip().startswith("别名:"):
  83. names = lines[i + 2].strip()[len("别名:"):].split()
  84. names[0] = names[0][len(":class:`"):-1]
  85. names[1] = names[1][len(":class:`"):-1]
  86. alias.append((names[0], names[1]))
  87. return head_list, alias
  88. if __name__ == "__main__":
  89. find_all() # use to check __all__