File size: 1,124 Bytes
eaf2e33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import re
import pandas as pds
from root import PRJROOT


def auto_dire(path=None, name='trial'):
    dire_id = 0
    folder = PRJROOT if path is None else getpath(path)
    tar = os.path.join(folder, f'{name}{dire_id}')
    while os.path.exists(tar):
        dire_id += 1
        tar = os.path.join(folder, f'{name}{dire_id}')
    os.makedirs(tar)
    return tar


def getpath(*args):
    """ if is absolute root_folder or related path, return {path}, else return {PRJROOT/path} """
    path = os.path.join(*args)
    if os.path.isabs(path) or re.match(r'\.+[\\/].*', path):
        return path
    else:
        return os.path.join(PRJROOT, path)

def load_dict_json(path, *keys):
    with open(getpath(path), 'r') as f:
        data = json.load(f)
    if len(keys) == 1:
        return data[keys[0]]
    return tuple(data[key] for key in keys)

def load_singlerow_csv(path, *keys):
    data = pds.read_csv(getpath(path))
    if len(keys) == 1:
        return data[keys[0]][0]
    return (data[key][0] for key in keys)

if __name__ == '__main__':
    print(os.path.join('smn', '**', 'dsf'))
    pass