Browse Source

make a tool to check the alias name and __all__ part

tags/v0.4.10
ChenXin 5 years ago
parent
commit
7fb7c1b5b4
2 changed files with 102 additions and 3 deletions
  1. +98
    -0
      docs/count.py
  2. +4
    -3
      fastNLP/__init__.py

+ 98
- 0
docs/count.py View File

@@ -0,0 +1,98 @@
import os


def find_all(path='../fastNLP'):
head_list = []
alias_list = []
for path, dirs, files in os.walk(path):
for file in files:
if file.endswith('.py'):
name = ".".join(path.split('/')[1:])
if file.split('.')[0] != "__init__":
name = name + '.' + file.split('.')[0]
if len(name.split('.')) < 3 or name.startswith('fastNLP.core'):
heads, alias = find_one(path + '/' + file)
for h in heads:
head_list.append(name + "." + h)
for a in alias:
alias_list.append(a)
heads = {}
for h in head_list:
end = h.split('.')[-1]
file = h[:-len(end) - 1]
if end not in heads:
heads[end] = set()
heads[end].add(file)
alias = {}
for a in alias_list:
for each in a:
end = each.split('.')[-1]
file = each[:-len(end) - 1]
if end not in alias:
alias[end] = set()
alias[end].add(file)
print("IN alias NOT IN heads")
for item in alias:
if item not in heads:
print(item, alias[item])
elif len(heads[item]) != 2:
print(item, alias[item], heads[item])
print("\n\nIN heads NOT IN alias")
for item in heads:
if item not in alias:
print(item, heads[item])


def find_class(path):
with open(path, 'r') as fin:
lines = fin.readlines()
pars = {}
for i, line in enumerate(lines):
if line.strip().startswith('class'):
line = line.strip()[len('class'):-1].strip()
if line[-1] == ')':
line = line[:-1].split('(')
name = line[0].strip()
parents = line[1].split(',')
for i in range(len(parents)):
parents[i] = parents[i].strip()
if len(parents) == 1:
pars[name] = parents[0]
else:
pars[name] = tuple(parents)
return pars


def find_one(path):
head_list = []
alias = []
with open(path, 'r') as fin:
lines = fin.readlines()
flag = False
for i, line in enumerate(lines):
if line.strip().startswith('__all__'):
line = line.strip()[len('__all__'):].strip()
if line[-1] == ']':
line = line[1:-1].strip()[1:].strip()
head_list.append(line.strip("\"").strip("\'").strip())
else:
flag = True
elif line.strip() == ']':
flag = False
elif flag:
line = line.strip()[:-1].strip("\"").strip("\'").strip()
if len(line) == 0 or line[0] == '#':
continue
head_list.append(line)
if line.startswith('def') or line.startswith('class'):
if lines[i + 2].strip().startswith("别名:"):
names = lines[i + 2].strip()[len("别名:"):].split()
names[0] = names[0][len(":class:`"):-1]
names[1] = names[1][len(":class:`"):-1]
alias.append((names[0], names[1]))
return head_list, alias


if __name__ == "__main__":
find_all() # use to check __all__

+ 4
- 3
fastNLP/__init__.py View File

@@ -13,11 +13,11 @@ fastNLP 中最常用的组件可以直接从 fastNLP 包中 import ,他们的
__all__ = [
"Instance",
"FieldArray",
"DataSetIter",
"BatchIter",
"TorchLoaderIter",
"Vocabulary",
"DataSet",
"Const",
@@ -51,7 +51,8 @@ __all__ = [
"LossFunc",
"CrossEntropyLoss",
"L1Loss", "BCELoss",
"L1Loss",
"BCELoss",
"NLLLoss",
"LossInForward",


Loading…
Cancel
Save