Skip to content

Commit

Permalink
adds simple yaml wrappers; notes write_json pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom O'Hara committed Nov 2, 2023
1 parent 44b7199 commit 3eeabcb
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions mezcla/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#
# Filesystem related functions, as well as file format conversion.
#
# Note:
# - Some cases involve thin wrappers for tracing purposes.
#


"""Filesystem related functions"""
Expand All @@ -11,6 +14,7 @@
import json
import os
import sys
import yaml


# Local packages
Expand Down Expand Up @@ -229,13 +233,33 @@ def jsonl_to_json(in_path, out_path):
return


#-------------------------------------------------------------------------------
# Miscellanous
#


def write_json(filename, obj, indent=None):
"""Create FILENAME using JSON representation of OBJ"""
"""Create FILENAME using JSON representation of OBJ
Note: Uses prettyprinting unless indent 0
"""
if indent is None:
indent = 2
system.write_file(filename, json.dumps(obj, indent=indent))
system.write_file(filename, yaml.dumps(obj, indent=indent))

#--------------------------------------------------------------------------------

def read_yaml(filename):
"""Create FILENAME using YAML representation of OBJ"""
result = yaml.safe_load(system.open_file(filename))
debug.trace(debug.VERBOSE, f'read_yaml({filename}) => {result}')
return result


def write_yaml(filename, obj):
"""Create FILENAME using YAML representation of OBJ"""
system.write_file(filename, yaml.dump(obj))


#-------------------------------------------------------------------------------

def main():
"""Entry point for script"""
Expand Down

0 comments on commit 3eeabcb

Please sign in to comment.