-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
145 lines (118 loc) · 4.62 KB
/
cli.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import argparse
import logging
import os
import re
import readline
from datetime import datetime
from pprint import pprint
import pandas as pd
def load_dataset(dataset_path):
if dataset_path.endswith('.csv'):
return pd.read_csv(dataset_path)
elif dataset_path.endswith('.json'):
return pd.read_json(dataset_path)
elif dataset_path.endswith('.jsonl'):
return pd.read_json(dataset_path, lines=True)
elif dataset_path.endswith('.xlsx'):
return pd.read_excel(dataset_path)
else:
raise NotImplementedError("We only support .csv/.json/.xlsx for now")
def set_logger(log_dir: str):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_id = datetime.now().strftime('%H_%M_%d_%m_%Y')
log_file = f'{log_dir}/{log_id}.log'
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S',
filename=log_file,
filemode='w')
class CommandParser:
def __init__(self, dataset_df):
self.df = dataset_df
def parse_command(self, command):
if command == 'show':
self.list_features()
return
elif command == 'head':
result = self.show_rows_range(0, 5)
elif command == 'tail':
result = self.show_rows_range(-5, -1)
elif command == 'len':
result = len(self.df)
elif re.match(r'\d+', command):
row_idx = int(re.findall(r'\d+', command)[0])
result = self.show_row(row_idx)
elif re.match(r'\[\d+\]', command):
row_idx = int(re.findall(r'\[(\d+)\]', command)[0])
result = self.show_row(row_idx)
elif re.match(r'\[\d+\]\.\w+', command):
row_idx, feature = re.findall(r'\[(\d+)\]\.(\w+)', command)[0]
result = self.show_feature_value(int(row_idx), feature)
elif re.match(r'\[\d+:\d+\]\.\w+', command):
start_idx, end_idx, feature = re.findall(r'\[(\d+):(\d+)\]\.(\w+)',
command)[0]
result = self.show_rows_range(int(start_idx), int(end_idx))
result = [r[feature] for _, r in result.iterrows()]
elif re.match(r'\[\d+:\d+\]', command):
start_idx, end_idx = map(
int,
re.findall(r'\[(\d+):(\d+)\]', command)[0])
result = self.show_rows_range(start_idx, end_idx)
elif re.match(r'filter\.\w+=.+', command):
feature, value = re.findall(r'filter\.(\w+)=(.+)', command)[0]
result = self.filter_rows(feature, value)
#... (可以按照這樣的模式添加其他的指令解析和功能)
else:
print('Unknown command')
return
if not isinstance(result, pd.DataFrame):
pprint(result)
logging.info(result)
elif len(result.shape) > 1:
for _, item in result.iterrows():
pprint(item)
print('--')
logging.info(item)
else:
pprint(result)
logging.info(result.to_string())
def list_features(self):
print(self.df.columns.tolist())
def show_row(self, row_idx):
return self.df.iloc[row_idx]
def show_feature_value(self, row_idx, feature):
return self.df.at[row_idx, feature]
def show_rows_range(self, start_idx, end_idx):
return self.df.iloc[start_idx:end_idx]
def filter_rows(self, feature, value):
return self.df[self.df[feature] == value]
def main(args):
print('Welcome to the interactive shell!')
print('Type "exit()" to exit the shell')
print('Load dataset from:', args.dataset_path)
dataset_df = load_dataset(args.dataset_path)
print('Dataset loaded successfully! Dataset shape:', dataset_df.shape)
tool = CommandParser(dataset_df)
if args.log:
set_logger(args.log_dir)
# start interactive shell
while True:
command = input('> ')
logging.info(f'> {command}')
if command == 'exit()':
break
try:
tool.parse_command(command)
except Exception as e:
logging.error(e)
print(e)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dataset_path', type=str, required=True)
parser.add_argument('--log',
action=argparse.BooleanOptionalAction,
default=False)
parser.add_argument('--log_dir', type=str, default='./logs')
args = parser.parse_args()
main(args)