diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/task_runner.py | 6 | ||||
| -rw-r--r-- | python/tasks.py | 38 |
2 files changed, 28 insertions, 16 deletions
diff --git a/python/task_runner.py b/python/task_runner.py index ad2fb817..f54807d7 100644 --- a/python/task_runner.py +++ b/python/task_runner.py @@ -14,15 +14,15 @@ def run(): while True: rpc = Rpc.from_json(q.blpop("tasks")[1].decode("utf-8")) - print("Running '{}' with {}".format(rpc.fun, rpc.args)) + print(f"Running '{rpc.fun}' with {rpc.args}") if rpc.fun == "generate_scenarios": rpc.args += [ET] try: rpc() except CalledProcessError: - print("'{}' did not complete".format(rpc.fun)) + print(f"'{rpc.fun}' did not complete") else: - print("'{}' completed".format(rpc.fun)) + print(f"'{rpc.fun}' completed") if rpc.fun == "build_portfolios": q.rpush("tasks", str(Rpc("build_scenarios", rpc.args))) if rpc.fun == "build_scenarios": diff --git a/python/tasks.py b/python/tasks.py index 05c5f51c..d20191e5 100644 --- a/python/tasks.py +++ b/python/tasks.py @@ -4,29 +4,41 @@ import sys from intex.intex_scenarios import generate_scenarios from json import loads, dumps +from pathlib import Path + def build_portfolios(workdate, dealname, reinvflag): - rpath = os.path.join(os.environ['CODE_DIR'], "R") - logpath = os.path.join(os.environ['LOG_DIR']) - args = ["Rscript", "--vanilla", os.path.join(rpath, "build_portfolios.R"), \ - workdate, dealname + "," + reinvflag] + rpath = Path(__file__).parent.parent / "R" + args = [ + "Rscript", + "--vanilla", + rpath / "build_portfolios.R", + workdate, + dealname + "," + reinvflag, + ] subprocess.check_call(args, env=os.environ, cwd=rpath) + def build_scenarios(workdate, dealname, reinvflag): - rpath = os.path.join(os.environ['CODE_DIR'], "R") - logpath = os.path.join(os.environ['LOG_DIR']) - args = ["Rscript", "--vanilla", os.path.join(rpath, "build_scenarios.R"), \ - workdate, dealname + "," + reinvflag] + rpath = Path(__file__).parent.parent / "R" + args = [ + "Rscript", + "--vanilla", + rpath / "build_scenarios.R", + workdate, + dealname + "," + reinvflag, + ] subprocess.check_call(args, env=os.environ, cwd=rpath) -class Rpc(object): + +class Rpc: def __init__(self, fun, args): self.fun = fun self.args = args def __str__(self): - return dumps({'fun': self.fun, - 'args': self.args}) + return dumps({"fun": self.fun, "args": self.args}) + def __call__(self): globals()[self.fun](*self.args) @@ -34,7 +46,7 @@ class Rpc(object): def from_json(cls, s): rpc = loads(s) if sys.version_info[0] < 3: - instance = cls(rpc['fun'].encode(), [arg.encode() for arg in rpc['args']]) + instance = cls(rpc["fun"].encode(), [arg.encode() for arg in rpc["args"]]) else: - instance = cls(rpc['fun'], rpc['args']) + instance = cls(rpc["fun"], rpc["args"]) return instance |
