-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.elv
91 lines (82 loc) · 2.32 KB
/
python.elv
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
use str
use path
var curr-venv
# Assert elvish version
var major minor = (take 2 [(str:split . $version)])
if (not (and (== $major 0) (<= 19 $minor) (<= $minor 20))) {
fail 'Incompatible elvish version'
}
# Activate/deactivate venv under given path.
# Examples:
# venv-activate /path/to/my/.venv # activate
# venv-activate - # deactivate
fn venv-activate {|path|
if (eq $path -) {
if (is $curr-venv $nil) {
return
} else {
echo (styled "== Deactivating venv: "$curr-venv green)
var new-paths = []
var curr-venv-bin = $curr-venv/bin
for p $paths {
if (not-eq $p $curr-venv-bin) {
set new-paths = [$@new-paths $p]
}
}
set paths = $new-paths
set curr-venv = $nil
set edit:rprompt = { put "" }
}
} else {
venv-activate -
if (not (path:is-dir $path)) {
echo (styled "== Creating venv: "$path green)
python3 -m virtualenv $path
}
set curr-venv = (path:abs $path)
echo (styled "== Activating venv: "$curr-venv green)
var curr-venv-bin = $curr-venv/bin
set paths = [$curr-venv-bin $@paths]
set edit:rprompt = { styled (path:base $path) inverse }
}
var text = (echo "Active python3:" (which python3))
echo (styled $text blue)
}
# Activate venv if $venv-name dir exists.
# Otherwise deactivate last venv.
# Can be called multiple times.
# Example:
# init-auto-venv-activation .venv
fn init-auto-venv-activation {|venv-name|
fn watch-venv {
if (path:is-dir $venv-name) {
venv-activate $venv-name > /dev/null
} else {
venv-activate - > /dev/null
}
}
# activate/deactivate after `cd` or interactive dir change
set after-chdir = [$@after-chdir {|path| watch-venv }]
# activate right away
watch-venv
}
fn wrap-py-code {|code| put "
import json as _json
locals().update(_json.loads(input()))
"$code"
print(_json.dumps({k: v for k, v in locals().items() if not k.startswith('_')}))
"}
# Evaluate code in python3.
# Examples:
#
# pyeval [&] z=10+20 # ▶ [&z=(num 30.0)]
#
# var res: = (ns (pyeval [&x=(num 123) &y=(num 2)] 'z = x / y'))
# echo $res:z # 61.5
#
# fn dv {|x y| { put (pyeval [&x=(num $x) &y=(num $y)] 'z = x / y')[z] } }
# dv 123 2 # ▶ (num 61.5)
# dv 123 0 # exception
fn pyeval {|vars code|
put $vars | to-json | python3 -c (wrap-py-code $code) | from-json
}