# Path: hyperengine/base/util.py
# def str_to_dict(s):
#   return ast.literal_eval(s)
# 
# def smart_str(val):
#   if type(val) in [float, np.float32, np.float64] and val:
#     return '%.6f' % val if abs(val) > 1e-6 else '%e' % val
#   if type(val) == dict:
#     return '{%s}' % ', '.join(['%s: %s' % (repr(k), smart_str(val[k])) for k in sorted(val.keys())])
#   if type(val) in [list, tuple]:
#     return '[%s]' % ', '.join(['%s' % smart_str(i) for i in val])
#   return repr(val)
# 
# def random_id(size=6, chars=string.ascii_uppercase + string.digits):
#   return ''.join(random.choice(chars) for _ in range(size))


import datetime
import os
import numpy as np
from .logging import *
from .util import str_to_dict, smart_str, random_id




class BaseIO(object):
  """
  The base class responsible for I/O operations.
  """

  def __init__(self, **params):
    self.load_dir = params.get('load_dir')
    self.save_dir = _format_path(params.get('save_dir'))

  @staticmethod
  def _prepare(directory):
    if directory is None:
      return False
    if not os.path.exists(directory):
      vlog('Creating a directory ', directory)
      os.makedirs(directory)
    return True

  @staticmethod
  def _load_dict(from_file):
    if not os.path.exists(from_file):
      debug('Cannot load a dict. File does not exist: ', from_file)
      return {}
    try:
      with open(from_file, 'r') as file_:
        line = file_.readline()

# Path: hyperengine/base/util.py
# def str_to_dict(s):
#   return ast.literal_eval(s)
# 
# def smart_str(val):
#   if type(val) in [float, np.float32, np.float64] and val:
#     return '%.6f' % val if abs(val) > 1e-6 else '%e' % val
#   if type(val) == dict:
#     return '{%s}' % ', '.join(['%s: %s' % (repr(k), smart_str(val[k])) for k in sorted(val.keys())])
#   if type(val) in [list, tuple]:
#     return '[%s]' % ', '.join(['%s' % smart_str(i) for i in val])
#   return repr(val)
# 
# def random_id(size=6, chars=string.ascii_uppercase + string.digits):
#   return ''.join(random.choice(chars) for _ in range(size))


# Path: hyperengine/base/base_io.py
import datetime
import os
import numpy as np
from .logging import *
from .util import str_to_dict, smart_str, random_id





class BaseIO(object):
  """
  The base class responsible for I/O operations.
  """

  def __init__(self, **params):
    self.load_dir = params.get('load_dir')
    self.save_dir = _format_path(params.get('save_dir'))

  @staticmethod
  def _prepare(directory):
    if directory is None:
      return False
    if not os.path.exists(directory):
      vlog('Creating a directory ', directory)
      os.makedirs(directory)
    return True

  @staticmethod
  def _load_dict(from_file):
    if not os.path.exists(from_file):
      debug('Cannot load a dict. File does not exist: ', from_file)
      return {}
    try:
      with open(from_file, 'r') as file_:
        line = file_.readline()

        return str_to_dict(line)