From 2a75ddbaa7dc272c51b6a2da12bd422a71bb0d0a Mon Sep 17 00:00:00 2001 From: Christoph Alt <christoph.alt@fau.de> Date: Tue, 27 Sep 2022 16:42:32 +0200 Subject: [PATCH] added json loading --- cbutil/__init__.py | 2 +- cbutil/postprocessing/__init__.py | 2 +- cbutil/postprocessing/plain_text.py | 6 ++++++ cbutil/util.py | 13 +++++++++---- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cbutil/__init__.py b/cbutil/__init__.py index b0700ed..8d2139f 100644 --- a/cbutil/__init__.py +++ b/cbutil/__init__.py @@ -1,6 +1,6 @@ from .upload import DBConfig, Uploader from .processing_functions import mesa_pd_text from .postprocessing import * -from .util import read_file_line_wise, time_conversion +from .util import read_file_line_wise, time_conversion, file_time_to_sec, get_from_nested_dict from .data_points import DataPoint, data_point_factory from .get_job_info import get_url_from_env, get_job_datapoints diff --git a/cbutil/postprocessing/__init__.py b/cbutil/postprocessing/__init__.py index 035ee90..5f06c8e 100644 --- a/cbutil/postprocessing/__init__.py +++ b/cbutil/postprocessing/__init__.py @@ -1,3 +1,3 @@ -from .plain_text import process_linewise, iterate_csv +from .plain_text import process_linewise, iterate_csv, json2dict from .sqlite import sqlite_context, query_complete_table, build_iterate_query, iterate_all_tables from .sqlite_helper import query_builder diff --git a/cbutil/postprocessing/plain_text.py b/cbutil/postprocessing/plain_text.py index 083f918..389531a 100644 --- a/cbutil/postprocessing/plain_text.py +++ b/cbutil/postprocessing/plain_text.py @@ -1,4 +1,5 @@ import csv +import json from collections import abc from typing import Callable, Iterable @@ -30,3 +31,8 @@ class CSVIterator(abc.Iterable): with open(self.file_name, "r") as in_file: for row in csv.DictReader(in_file): yield row + + +def json2dict(path: str) -> dict: + with open(path, "r") as json_file: + return json.load(json_file) diff --git a/cbutil/util.py b/cbutil/util.py index d42acf3..01cf6f7 100644 --- a/cbutil/util.py +++ b/cbutil/util.py @@ -1,11 +1,12 @@ -import git +import logging +import operator import os from datetime import datetime - -from typing import Union, Tuple +from functools import reduce from pathlib import Path +from typing import Tuple, Union -import logging +import git logger = logging.getLogger(__file__) @@ -68,3 +69,7 @@ def time_conversion(time_stamp, *, pattern="%Y-%m-%d %H:%M:%S"): dt = datetime.fromisoformat(time_stamp) return int(dt.timestamp()) + + +def get_from_nested_dict(nested_dict, keys): + return reduce(operator.getitem, keys, nested_dict) -- GitLab