Repository navigation
Expand file tree
/
Copy pathline_counter.py
More file actions
61 lines (51 loc) · 1.92 KB
/
Copy pathline_counter.py
File metadata and controls
61 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
def get_valid_nol_in(file):
def is_valid_line(line):
stripped_line = line.strip()
return (stripped_line != '' and
stripped_line != '{' and
stripped_line != '}' and
stripped_line != '[' and
stripped_line != ']' and
stripped_line != '},' and
stripped_line != '],' and
'"id":' not in stripped_line)
content = file.read().split("\n")
content = list(filter(is_valid_line, content))
return len(content)
def count_number_of_lines(root, number):
"""
| Recursion used to iterate through provided
| directory, and count the total number of
| valid lines in all .json files.
:param root: String
:param number: Int
:return: Void
"""
if os.path.isdir(root):
subdirs = os.listdir(root)
total = number
for subdir in subdirs:
total += count_number_of_lines(f"{root}/{subdir}", number)
return total
else:
if not root.endswith('.json'):
return number
# Ignore special tables
if root.endswith('-dt.json') or root.endswith('-di.json'):
return number
filedir = root
with open(filedir, 'r', encoding="utf-8") as file:
return number + get_valid_nol_in(file)
if __name__ == "__main__":
with open("./README.md", 'r', encoding='utf-8') as readme:
lines = readme.readlines()
index_of_count_line = -1
for index, item in enumerate(lines):
if item.strip().startswith("Current valid number of lines in DB:"):
index_of_count_line = index
if index_of_count_line != -1:
lines[index_of_count_line] = (f"Current valid number of lines in DB: "
f"{count_number_of_lines('.', 0)}\n")
with open("./README.md", 'w', encoding='utf-8') as readme:
readme.writelines(lines)