Source code for citylearn.utils.file_handler

import pickle
from pathlib import Path
from typing import Any
import simplejson as json
import yaml

[docs] class FileHandler:
[docs] @staticmethod def resolve_schema_root(schema: dict, schema_filepath: str) -> Path: """Resolve local data roots independently of the process directory. Prefer schema-relative roots. Older repository schemas include their full repository-relative directory; resolve those against an existing ancestor without changing the stored schema or consulting the cwd. """ schema_dir = Path(schema_filepath).expanduser().resolve().parent configured = schema.get('root_directory') if configured is None: return schema_dir root = Path(configured).expanduser() if root.is_absolute(): return root.resolve() candidate = (schema_dir / root).resolve() if candidate.is_dir(): return candidate for ancestor in schema_dir.parents: legacy_candidate = (ancestor / root).resolve() if legacy_candidate.is_dir(): return legacy_candidate return candidate
[docs] @staticmethod def read_json(filepath: str, **kwargs) -> dict: """Return JSON document as dictionary. Parameters ---------- filepath : str pathname of JSON document. Other Parameters ---------------- **kwargs : dict Other infrequently used keyword arguments to be parsed to `simplejson.load`. Returns ------- dict JSON document converted to dictionary. """ with open(filepath, encoding='utf-8-sig') as f: json_file = json.load(f, **kwargs) return json_file
[docs] @staticmethod def write_json(filepath: str, dictionary: dict, **kwargs): """Write dictionary to JSON file. Parameters ---------- filepath : str pathname of JSON document. dictionary: dict dictionary to convert to JSON. Other Parameters ---------------- **kwargs : dict Other infrequently used keyword arguments to be parsed to `simplejson.dump`. """ kwargs = {'ignore_nan': True, 'sort_keys': False, 'default': str, 'indent': 2, **kwargs} with open(filepath,'w') as f: json.dump(dictionary, f, **kwargs)
[docs] @staticmethod def read_yaml(filepath: str) -> dict: """Return YAML document as dictionary. Parameters ---------- filepath : str pathname of YAML document. Returns ------- dict YAML document converted to dictionary. """ with open(filepath, 'r') as f: data = yaml.safe_load(f) return data
[docs] @staticmethod def write_yaml(filepath: str, dictionary: dict, **kwargs): """Write dictionary to YAML file. Parameters ---------- filepath : str pathname of YAML document. dictionary: dict dictionary to convert to YAML. Other Parameters ---------------- **kwargs : dict Other infrequently used keyword arguments to be parsed to `yaml.safe_dump`. """ kwargs = {'sort_keys': False, 'indent': 2, **kwargs} with open(filepath, 'w') as f: yaml.safe_dump(dictionary, f, **kwargs)
[docs] @staticmethod def read_pickle(filepath: str, **kwargs) -> Any: """Return pickle file as some Python class object. Parameters ---------- filepath : str pathname of pickle file. Other Parameters ---------------- **kwargs : dict Other infrequently used keyword arguments to be parsed to `pickle.load`. Returns ------- Any Pickle file as a Python object. """ with (open(filepath, 'rb')) as f: data = pickle.load(f, **kwargs) return data
[docs] @staticmethod def write_pickle(filepath: str, data: Any, **kwargs): """Write Python object to pickle file. Parameters ---------- filepath : str pathname of pickle document. data: dict object to convert to pickle. Other Parameters ---------------- **kwargs : dict Other infrequently used keyword arguments to be parsed to `pickle.dump`. """ with open(filepath, 'wb') as f: pickle.dump(data, f, **kwargs)
[docs] @staticmethod def join_url(*args: str) -> str: url = '/'.join([a.strip('/') for a in args]) return url