From b5f522ad8b41c405a70c9a3d043aeb42fecf710d Mon Sep 17 00:00:00 2001 From: agp8x Date: Tue, 25 Jul 2017 17:35:10 +0200 Subject: [PATCH] init --- analyzer/__init__.py | 2 ++ analyzer/analyzer.py | 24 ++++++++++++++++++++ analyzer/biogames.py | 18 +++++++++++++++ biogames.json | 14 ++++++++++++ load.py | 33 +++++++++++++++++++++++++++ log_analyzer.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ sample.json | 1 + 7 files changed, 146 insertions(+) create mode 100644 analyzer/__init__.py create mode 100644 analyzer/analyzer.py create mode 100644 analyzer/biogames.py create mode 100644 biogames.json create mode 100644 load.py create mode 100644 log_analyzer.py create mode 100644 sample.json diff --git a/analyzer/__init__.py b/analyzer/__init__.py new file mode 100644 index 0000000..826acb7 --- /dev/null +++ b/analyzer/__init__.py @@ -0,0 +1,2 @@ +from .analyzer import * +from .biogames import * diff --git a/analyzer/analyzer.py b/analyzer/analyzer.py new file mode 100644 index 0000000..3741b43 --- /dev/null +++ b/analyzer/analyzer.py @@ -0,0 +1,24 @@ +from log_analyzer import LogSettings + + +class Analyzer: + def __init__(self, settings: LogSettings): + self.settings = settings + + def process(self, entry: object) -> bool: + raise NotImplementedError() + + def result(self) -> object: + raise NotImplementedError() + + +class LocationAnalyzer(Analyzer): + + def __init__(self, settings: LogSettings): + super().__init__(settings) + + def result(self) -> object: + return self.settings.spatials + + def process(self, entry: object) -> bool: + pass \ No newline at end of file diff --git a/analyzer/biogames.py b/analyzer/biogames.py new file mode 100644 index 0000000..40691c7 --- /dev/null +++ b/analyzer/biogames.py @@ -0,0 +1,18 @@ +from collections import defaultdict + +from log_analyzer import LogSettings + +from .analyzer import Analyzer + + +class BoardDurationAnalyzer(Analyzer): + def result(self) -> object: + return self.store + + def process(self, entry: object) -> bool: + self.store[entry] += 1 + return False + + def __init__(self, settings: LogSettings): + super().__init__(settings) + self.store = defaultdict(lambda: 0) diff --git a/biogames.json b/biogames.json new file mode 100644 index 0000000..d32b898 --- /dev/null +++ b/biogames.json @@ -0,0 +1,14 @@ +{ + "logFormat": "json", + "entryType": "@class", + "spatials":["...PositionLogEvent"], + "actions":["...QuestionAnswerEvent", "...SimuAnswerEvent"], + "analyzers": { + "analyzer": [ + "LocationAnalyzer" + ], + "analyzer.biogames": [ + "BoardDurationAnalyzer" + ] + } +} \ No newline at end of file diff --git a/load.py b/load.py new file mode 100644 index 0000000..05c2c81 --- /dev/null +++ b/load.py @@ -0,0 +1,33 @@ +import json + + +class Loader: + def load(self, file: str): + raise NotImplementedError() + + def get_entry(self) -> object: + raise NotImplementedError() + + +class JSONLoader(Loader): + data = None + + def load(self, file: str): + self.data = json.load(open(file)) + + def get_entry(self): + for entry in self.data: + yield entry + +class SQLiteLoader(Loader): + def load(self, file: str): + pass + + def get_entry(self) -> object: + pass + + +LOADERS = { + "json": JSONLoader, + "sqlite": SQLiteLoader +} \ No newline at end of file diff --git a/log_analyzer.py b/log_analyzer.py new file mode 100644 index 0000000..057420b --- /dev/null +++ b/log_analyzer.py @@ -0,0 +1,54 @@ +import json +import sys +from load import LOADERS +import analyzer + + +class LogSettings: + log_format = None + entry_type = None + spatials = None + actions = None + analyzers = [] + + def __init__(self, json_dict): + self.log_format = json_dict['logFormat'] + self.entry_type = json_dict['entryType'] + self.spatials = json_dict['spatials'] + self.actions = json_dict['actions'] + for mod in json_dict['analyzers']: + for name in json_dict['analyzers'][mod]: + self.analyzers.append(getattr(sys.modules[mod], name)) + + def __repr__(self): + return str({ + "logFormat": self.log_format, + "entryType": self.entry_type, + "spatials": self.spatials, + "actions": self.actions, + "analyzers": self.analyzers + }) + + +def load_settings(file:str) -> LogSettings: + return LogSettings(json.load(open(file))) + + +if __name__ == '__main__': + settings = load_settings("biogames.json") + print(settings) + logfile = "sample.json" + loader = LOADERS[settings.log_format]() + try: + loader.load(logfile) + except BaseException as e: + raise RuntimeError(e) + analyzers = [] + for analyzer in settings.analyzers: + analyzers.append(analyzer(settings)) + for entry in loader.get_entry(): + for analyzer in analyzers: + analyzer.process(entry) + for analyzer in analyzers: + print("* Result for " + str(type(analyzer))) + print(analyzer.result()) \ No newline at end of file diff --git a/sample.json b/sample.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/sample.json @@ -0,0 +1 @@ +[] \ No newline at end of file