-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
61 lines (54 loc) · 2.19 KB
/
utils.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
import ast
import io
from contextlib import redirect_stdout, redirect_stderr
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
from typing import Iterator, Tuple
def unindent(text:str) -> str:
lines = text.splitlines()
indent = min(len(line) - len(line.lstrip()) for line in lines if line.strip())
return '\n'.join(line[indent:] for line in lines)
def extract_code(lines: Iterator[str]) -> str:
code = []
for line in lines:
if line == '```':
return unindent('\n'.join(code))
code.append(line)
return None
def exec_code(code) -> Tuple[str, str, str|Exception]:
with redirect_stdout(io.StringIO()) as out, redirect_stderr(io.StringIO()) as err:
try:
result = exec_python(code)
except Exception as e:
result = e
return out.getvalue(), err.getvalue(), result
def exec_python(code:str) -> str:
block = ast.parse(code, mode='exec')
last = ast.Expression(block.body.pop().value) if isinstance(block.body[-1], ast.Expr) else None
_globals, _locals = {}, {}
exec(compile(block, '<string>', mode='exec'), _globals, _locals)
return eval(compile(last, '<string>', mode='eval'), _globals, _locals) if last else None
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
def elapsed_time(timestamp: pd.Timestamp) -> str:
now = datetime.now()
elapsed = now - timestamp.to_pydatetime()
if elapsed < timedelta(minutes=1):
seconds = int(elapsed.total_seconds())
return f"{seconds} seconds ago"
elif elapsed < timedelta(hours=1):
minutes = int(elapsed.total_seconds() // 60)
return f"{minutes} minutes ago"
elif elapsed < timedelta(days=1):
hours = int(elapsed.total_seconds() // 3600)
return f"{hours} hours ago"
elif elapsed < timedelta(days=30):
days = int(elapsed.total_seconds() // 86400)
return f"{days} days ago"
elif elapsed < timedelta(days=365):
months = int(elapsed.total_seconds() // (30 * 86400))
return f"{months} months ago"
else:
years = int(elapsed.total_seconds() // (365 * 86400))
return f"{years} years ago"