Browse Source

add config saver

tags/v0.1.0
xuyige 6 years ago
parent
commit
dbf1e492fd
1 changed files with 35 additions and 0 deletions
  1. +35
    -0
      fastNLP/saver/config_saver.py

+ 35
- 0
fastNLP/saver/config_saver.py View File

@@ -0,0 +1,35 @@
import os

import json
import configparser

from fastNLP.loader.config_loader import ConfigSection, ConfigLoader
from fastNLP.saver.logger import create_logger

class ConfigSaver(object):

def __init__(self, file_path):
self.file_path = file_path

def save_section(self, section_name, section):
cfg = configparser.ConfigParser()
if not os.path.exists(self.file_path):
raise FileNotFoundError("config file {} not found. ".format(self.file_path))
cfg.read(self.file_path)
if section_name not in cfg:
cfg.add_section(section_name)
gen_sec = cfg[section_name]
for key in section:
if key in gen_sec.keys():
try:
val = json.load(gen_sec[key])
except Exception as e:
print("cannot load attribute %s in section %s"
% (key, section_name))
try:
assert section[key] == val
except Exception as e:
logger = create_logger(__name__, "./config_saver.log")
logger.warning("this is a warning #TODO")
cfg.set(section_name,key, section[key])
cfg.write(open(self.file_path, 'w'))

Loading…
Cancel
Save