forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipython.ipy
More file actions
executable file
·193 lines (108 loc) · 3.02 KB
/
ipython.ipy
File metadata and controls
executable file
·193 lines (108 loc) · 3.02 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env ipython
# Must use ipy extension, or ipython uses regular python with python file
## Review
# pythonlike shell
# advantages:
# - all programming contructs are sane (python)
# - easier to call system shell
# - autocompletion
# - some bash built-ins like cd
# - quick documentation with ?
## Regular python
# All of normal python works.
a = 1
print a
def f(a):
print a
def f2(a,b):
"""
docstring of f2
"""
print a + b
## Tab completion
#works even for python objects in current scope.
#type f, hit tab, and see `f2`!
##bash built ins
#cd works
##documentation
#put docstring of f2 `less`:
f2?
if '##magic':
#call python functions with less typing
#only work at start of strings
#avoid typing parenthesis and commas:
/f2 1,2
/f2 1 2
#are the same as `f2(1,2)`
#any space is converted to a comma:
#/f2 1, 2
#is the same as f2(1,,2)
#also autoquote:
,f2 ab cd
#same as `f2('ab','cd')`
,f2 12 34
#same as `f2('12','34')`. They are not number anymore but strings!
#autoquote as single argument:
;f a b
#same as `f('a b')`
#you can only have single argument functions in this mode
#TODO how to get their return values?
##shell
!echo a
!echo a | cat
!echo A > a.tmp
!cat a.tmp
!rm a.tmp
#it really runs inside your shell:
!echo *
#TODO how to avoid running in a shell? (python pipes for example)
###get stdout err
a = !echo abc
assert a[0] == 'abc'
a = !echo abc 1>&2
assert a[0] == 'abc'
#TODO how to separate stdout and stderr? I would love to write:
#out, err = !somecommand
a = !echo 'ab\ncd'
assert a == ['ab','cd']
#other expressions besides equality don't work:
#a = [1] + !echo ab
###pass python variable
a = 'ab cd'
!echo $a
###strings are python strings
a = !echo 'ab\ncd'
assert a == ['ab','cd']
###exit status
!false
assert _exit_code == 1
#but does not work when you capture stdout/stderr:
!false
a = !true
assert _exit_code == 1
#TODO how to do this?
#simply does not change.
##rehashx
#once used, you don't need '!' anymore
#TODO it works in interactive, why not here?
%rehashx
#echo b
##startup files
#based on profiles
#to create default profile:
#ipython profile create
#to create a named profile:
#ipython profile create $name
#to locate your profiles folder (does not exist before the first profile create):
#ipython locate
#inside location dirs are organized as follows:
#profile_<name>/startup
#anything under `startup` will be run at startup for the given profile
#start ipython with an specific profile:
#ipython --profile $name
#if ommitted `name=default`.
## %edit
# Open a given function on Vim, and run it after it is edited:
#%edit myfunction
# To just see the source, use:
#myfunction??