Skip to content

Commit 7e53ec5

Browse files
authored
Create 0007.py
1 parent 10b1dda commit 7e53ec5

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

pylyria/0007/0007.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# vim:fenc=utf-8
4+
# Copyright By PyLyria
5+
# CreateTime: 2016-03-04 19:36:40
6+
import os
7+
8+
def get_path(root = os.curdir):
9+
root += os.sep
10+
for path, dirs, files in os.walk(root):
11+
for file_name in files:
12+
yield path, file_name
13+
14+
def get_lines(file_name):
15+
with open(file_name,'rt',encoding='utf-8') as f:
16+
for line in f:
17+
yield line.strip()
18+
19+
if __name__ == '__main__':
20+
paths = get_path()
21+
format = ('.py', '.c', '.cpp', '.sql')
22+
annotation = ('#', '//', '--', '/*')
23+
code_statistics = {}
24+
25+
for path, file_name in paths:
26+
if file_name.endswith(format):
27+
code_statistics[file_name] = {}
28+
lines = get_lines(path + os.sep + file_name)
29+
for line in lines:
30+
if len(line) ==0:
31+
code_statistics[file_name]['EmptyLine'] = code_statistics[file_name].get('EmptyLine', 0) + 1
32+
elif line.startswith(annotation):
33+
code_statistics[file_name]['AnnotationLine'] = code_statistics[file_name].get('AnnotationLine', 0) + 1
34+
else:
35+
code_statistics[file_name]['CodeLine'] = code_statistics[file_name].get('CodeLine', 0) + 1
36+
37+
print(code_statistics)

0 commit comments

Comments
 (0)