From 7fb7c1b5b41d29c15f8322e12385ee9057e540e5 Mon Sep 17 00:00:00 2001 From: ChenXin Date: Mon, 19 Aug 2019 21:48:41 +0800 Subject: [PATCH] make a tool to check the alias name and __all__ part --- docs/count.py | 98 +++++++++++++++++++++++++++++++++++++++++++++ fastNLP/__init__.py | 7 ++-- 2 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 docs/count.py diff --git a/docs/count.py b/docs/count.py new file mode 100644 index 00000000..d906f4c0 --- /dev/null +++ b/docs/count.py @@ -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__ diff --git a/fastNLP/__init__.py b/fastNLP/__init__.py index ec192568..a6767088 100644 --- a/fastNLP/__init__.py +++ b/fastNLP/__init__.py @@ -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",