File size: 2,517 Bytes
67a9b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import base64
import json
import numbers
import pickle
import warnings
from collections import OrderedDict


def load_list(filename, encoding='utf-8', start=0, stop=None):
    assert isinstance(start, numbers.Integral) and start >= 0
    assert (stop is None) or (isinstance(stop, numbers.Integral) and stop > start)
    
    lines = []
    with open(filename, 'r', encoding=encoding) as f:
        for _ in range(start):
            f.readline()
        for k, line in enumerate(f):
            if (stop is not None) and (k + start > stop):
                break
            lines.append(line.rstrip('\n'))
    return lines


def save_list(filename, list_obj, encoding='utf-8', append_break=True):
    with open(filename, 'w', encoding=encoding) as f:
        if append_break:
            for item in list_obj:
                f.write(str(item) + '\n')
        else:
            for item in list_obj:
                f.write(str(item))


def load_json(filename, encoding='utf-8'):
    with open(filename, 'r', encoding=encoding) as f:
        data = json.load(f, object_pairs_hook=OrderedDict)
    return data


def save_json(filename, data, encoding='utf-8', indent=4, cls=None, sort_keys=False):
    if not filename.endswith('.json'):
        filename = filename + '.json'
    with open(filename, 'w', encoding=encoding) as f:
        json.dump(data, f, indent=indent, separators=(',',': '),
                  ensure_ascii=False, cls=cls, sort_keys=sort_keys)


def load_bytes(filename, use_base64: bool = False) -> bytes:
    """Open the file in bytes mode, read it, and close the file.
    
    References:
        pathlib.Path.read_bytes
    """
    with open(filename, 'rb') as f:
        data = f.read()
    if use_base64:
        data = base64.b64encode(data)
    return data


def save_bytes(filename, data: bytes, use_base64: bool = False) -> int:
    """Open the file in bytes mode, write to it, and close the file.
    
    References:
        pathlib.Path.write_bytes
    """
    if use_base64:
        data = base64.b64decode(data)
    with open(filename, 'wb') as f:
        ret = f.write(data)
    return ret


def load_as_base64(filename) -> bytes:
    warnings.warn('khandy.load_as_base64 will be deprecated, use khandy.load_bytes instead!')
    return load_bytes(filename, True)


def load_object(filename):
    with open(filename, 'rb') as f:
        return pickle.load(f)
        
        
def save_object(filename, obj):
    with open(filename, 'wb') as f:
        pickle.dump(obj, f)