File size: 884 Bytes
b621857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-

import os
import io
import tarfile
import json
import numpy as np
import numpy.lib.format


def mkdir(path):
    os.makedirs(path, exist_ok=True)
    return path


def npy_loads(data):
    stream = io.BytesIO(data)
    return np.lib.format.read_array(stream)


def npz_loads(data):
    return np.load(io.BytesIO(data))


def json_loads(data):
    return json.loads(data)


def load_json(filepath):
    with open(filepath, "r") as f:
        data = json.load(f)
        return data


def write_json(filepath, data):
    with open(filepath, "w") as f:
        json.dump(data, f, indent=2)


def extract_tar(tar_path, tar_cache_folder):

    with tarfile.open(tar_path, "r") as tar:
        tar.extractall(path=tar_cache_folder)

    tar_uids = sorted(os.listdir(tar_cache_folder))
    print(f"extract tar: {tar_path} to {tar_cache_folder}")
    return tar_uids