File size: 911 Bytes
e91104d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fractions import Fraction
from pathlib import Path
from json import JSONEncoder
from .utils import *


def rmtree(path: Path):
    if path.is_file():
        path.unlink()
    else:
        for ch in path.iterdir():
            rmtree(ch)
        path.rmdir()


def safe_save(fpath, data, save_fun, rewrite=False, error_msg='File {fpath} exists! To rewite it use `--rewrite` flag', **kwargs):
    if not fpath.is_file() or rewrite:
        save_fun(str(fpath), data, **kwargs)
    else:
        raise FileExistsError(error_msg.format(fpath=fpath))


class FractionJSONEncoder(JSONEncoder):
    def default(self, o):
        if isinstance(o, Fraction):
            return {'Fraction': [o.numerator, o.denominator]}
        else:
            return o.__dict__


def fraction_from_json(json_object):
    if 'Fraction' in json_object:
        return Fraction(*json_object['Fraction'])
    return json_object