Dynamically load json type in python

In python, the json module is included in the package, but the yaml module is made by a third party. In the program I am currently writing, to absorb the difference in the environment

  1. If you have a yaml module and you have a yaml config file, read it.
  2. If it doesn't work, try reading with json.
  3. If that doesn't work, give an exception

The behavior is adopted. Since yaml and json have similar interfaces, I treated them exactly the same as below,

try:
    import yaml
    with open("config.yaml") as y:
        config = yaml.load("config.yaml")
except ImportError:
    import json
    try:
        with open("config.json") as j:
            config = json.load(j)
    except AttributeError:
        logger.error("please set config file in json or yaml !!")
        raise


#Use the value of config below ...

There was a pitfall here. In the case of yaml, the key type is automatically guessed and read, but json is always str. In other words, if the config file has the following contents

config.yaml


1: "hoge"
key: "fuga" 

config.json


{
    "1": "hoge",
    "key": "fuga"
}

In either case, you can access " fuga " withconfig ["key"], but"hoge"isconfig ["1"]for json andconfig [" for yaml. Must be accessed with 1].

To avoid this, give a function as a hook at json.load ().

def jsonkeys_to_int_always(x): #If you can set all key types to int, click here.
    if isinstance(x, dict):
            return {int(k):v for k,v in x.items()}
    return x

def jsonkey_to_int_when_possible(x): #Click here if you want to behave in the same way as yaml
    d = {}
    if isinstance(x, dict):
        for k,v in x.items():
            try:
                k = int(k)
            except ValueError:
                pass
            d[k] = v
        return d

config = json.load("config.json", object_hook=jsonkeys_to_int_when_possible)

It seems that you can absorb the difference by Specify the value in yaml, but I don't know the details.

Recommended Posts

Dynamically load json type in python
Handling json in python
Easily format JSON in Python
Dynamically import scripts in Python
Dynamically call methods in Python
Handling of JSON files in Python
Type specified in python. Throw exceptions
Python #JSON
Dynamically define functions (methods) in Python
Load the remote Python SDK in IntelliJ
[Python3] Dynamically define global variables in functions
Data input / output in Python (CSV, JSON)
How to dynamically define variables in Python
Create a JSON object mapper in Python
Type annotations for Python2 in stub files!
Read and write JSON files in Python
How to dynamically zero pad in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Python numeric type
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Python2 string type
Constant in python
Python # string type
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python