- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnet_data_sum.py
More file actions
Latest commit
executable file
·72 lines (61 loc) · 2.58 KB
/
Copy pathnet_data_sum.py
File metadata and controls
executable file
·72 lines (61 loc) · 2.58 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
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
"""
Rewrite a file with network download/uploads statistics for the day to sum the
values basing on whether they are of the same date or not
"""
###############################################################################
__author__="Gianluca Fiore"
__license__="GPL"
__version__="1.0"
__mantainer__="Gianluca Fiore"
__date__="03/10/2008"
__email__="forod.g@gmail.com"
__status__="stable"
importre
date=re.compile('^(?P<date>\d+):') # matches the date
up=re.compile("Up.(?P<up>\d+).+") # matches the up total
down=re.compile("Down.(?P<down>\d+).+") # matches the down total
net_data=open("/mnt/d/Stuff/net_data_statistics.txt", "r")
lines=net_data.readlines() # read each file's line
# Create one list containing the whole lines from the file
whole_lines= []
# and another containing the new, edited, lines
newLines= []
forlineinlines:
line_strip=line.strip()
whole_lines.append(line_strip)
iflen(whole_lines) <2:
# if there aren't at least 2 lines in the list
# there is no point in making a comparition
newLines.append(whole_lines[-1])
else:
# save the two lines
line1=whole_lines[-1]
line2=whole_lines[-2]
lastDate=date.match(line1) # get the last date
penultimateDate=date.match(line2) # get the penultimate date
iflastDate.group(1) ==penultimateDate.group(1):
# sum the down and up totals
line1Up=up.search(line1)
line2Up=up.search(line2)
line1Down=down.search(line1)
line2Down=down.search(line2)
newUpTot=int(line1Up.group(1)) +int(line2Up.group(1))
newDownTot=int(line2Down.group(1)) +int(line2Down.group(1))
# finally make the final string to be written back to the file
newLine="%s: Up[%s kB] Down[%s kB]"% \
(lastDate.group(1), newUpTot, newDownTot)
# lastly, append the modified line to the final list and remove the
# two latest lines
newLines.pop()
newLines.append(newLine)
else:
# no duplicate dates then, just keep going on but before add the
# penultimate line to a list
newLines.append(whole_lines[-1])
# At this point, the file should have been corrected, write back to it
withopen("/mnt/d/Stuff/net_data_statistics.txt", "w") asnet_data:
forelementsinnewLines:
net_data.write(elements+"\n")