https://github.com/annotation/text-fabric
Raw File
Tip revision: 591403d47305c4a8bf3630adc8d9ac3b77b45424 authored by Dirk Roorda on 08 March 2019, 14:17:59 UTC
minor changes
Tip revision: 591403d
lib.py
import os
import pickle
import gzip
from .parameters import PICKLE_PROTOCOL, GZIP_LEVEL
from .core.helpers import console


def writeSets(sets, dest):
  destPath = os.path.expanduser(dest)
  (baseDir, fileName) = os.path.split(destPath)
  if not os.path.exists(baseDir):
    try:
      os.makedirs(baseDir, exist_ok=True)
    except Exception:
      console(f'Cannot create directory "{baseDir}"', error=True)
      return False
  with gzip.open(destPath, "wb", compresslevel=GZIP_LEVEL) as f:
    pickle.dump(sets, f, protocol=PICKLE_PROTOCOL)
  return True


def readSets(source):
  sourcePath = os.path.expanduser(source)
  if not os.path.exists(sourcePath):
    console(f'Sets file "{source}" does not exist.')
    return False
  with gzip.open(sourcePath, "rb") as f:
    sets = pickle.load(f)
  return sets
back to top