forked from nkremerh/sugarscape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogparse.py
60 lines (53 loc) · 1.9 KB
/
logparse.py
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
#! /usr/bin/python
import getopt
import json
import sys
def parseLog(logFile):
file = open(logFile)
entries = json.loads(file.read())
timesteps = -1
data = {"population": 0, "agentWealthCollected": 0, "agentWealthTotal": 0,
"environmentWealthCreated": 0, "environmentWealthTotal": 0, "meanHappiness": 0, "meanWealthHappiness": 0,
"meanHealthHappiness": 0, "meanSocialHappiness": 0, "meanFamilyHappiness": 0, "meanConflictHappiness": 0,
"agentStarvationDeaths": 0, "agentMeanTimeToLive": 0,
"agentMeanTimeToLiveAgeLimited": 0, "agentReproduced": 0}
print(data.keys())
for entry in entries:
dataStr = ""
for datum in data:
data[datum] = entry[datum]
print(data[datum], end=',')
print("")
def parseOptions():
commandLineArgs = sys.argv[1:]
shortOptions = "lh:"
longOptions = ["log=", "help"]
logFile = None
try:
args, vals = getopt.getopt(commandLineArgs, shortOptions, longOptions)
except getopt.GetoptError as err:
print(err)
printHelp()
nextArg = 0
for currArg, currVal in args:
nextArg += 1
if currArg in("-l", "--log"):
if currArg == "-l" and nextArg < len(commandLineArgs):
currVal = commandLineArgs[nextArg]
if currVal == "":
print("No log file provided.")
printHelp()
logFile = currVal
elif currArg in ("-h", "--help"):
printHelp()
return logFile
def printHelp():
print("Usage:\n\tpython logparse.py --log log.json\n\nOptions:\n\t-l,--log\tUse specified log file for parsing and summarizing.\n\t-h,--help\tDisplay this message.")
exit(0)
if __name__ == "__main__":
logFile = parseOptions()
if logFile == None:
print("No log file provided.")
printHelp()
summary = parseLog(logFile)
exit(0)