In [2]: re.findall? Signature: re.findall(pattern, string, flags=0) Docstring: Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.
Empty matches are included in the result. File: ~/.pyenv/versions/3.9.0/lib/python3.9/re.py Type: function
Data structure also contains labeled axes (rows and columns). Arithmetic operations align on both row and column labels. Can be thought of as a dict-like container for Series objects. The primary pandas data structure.
In [2]: solve() IndexError: list index out of range
In [3]: %debug > /Users/switowski/workspace/iac/solver.py(11)count_trees() 9 x = (x + dx) % mod 10 y += dy ---> 11 if values[y][x] == "#": 12 count += 1 13 return count
ipdb>
7. 自动开启调试
如果我们希望异常出现时,调试器自动启动,可以使用%pdb命令,再次使用该命令会关闭该功能。
In [1]: %pdb Automatic pdb calling has been turned ON
In [2]: from solver import solve
In [3]: solve() IndexError: list index out of range
> /Users/switowski/workspace/iac/solver.py(11)count_trees() 9 x = (x + dx) % mod 10 y += dy ---> 11 if values[y][x] == "#": 12 count += 1 13 return count
In [2]: ls -al total 8 drwxr-xr-x 5 switowski staff 480 Dec 21 17:26 ./ drwxr-xr-x 55 switowski staff 1760 Dec 22 14:47 ../ drwxr-xr-x 9 switowski staff 384 Dec 21 17:27 .git/ drwxr-xr-x 4 switowski staff 160 Jan 25 11:39 __pycache__/ -rw-r--r-- 1 switowski staff 344 Dec 21 17:26 solver.py
# Node REPL inside IPython? Sure! In [3]: !node Welcome to Node.js v12.8.0. Type ".help"for more information. > var x = "Hello world" undefined > x 'Hello world' >
In [2]: solve() IndexError: list index out of range
Plain
In [3]: %xmode Exception reporting mode: Plain
In [4]: solve() Traceback (most recent call last): File ""
, line 1, in solve() File "/Users/switowski/workspace/iac/solver.py", line 27, in solve sol_part1 = part1(vals) File "/Users/switowski/workspace/iac/solver.py", line 16, in part1 return count_trees(vals, 3, 1) File "/Users/switowski/workspace/iac/solver.py", line 11, in count_trees if vals[y][x] == "#": IndexError: list index out of range
Context(默认设置)
In [5]: %xmode Exception reporting mode: Context
In [6]: solve() --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in ----> 1 solve()
# Just pasting the code won't work In [1]: >def greet(name): ...: > print(f"Hello {name}") File "", line 1 >def greet(name): ^ SyntaxError: invalid syntax
# But using %paste works In [2]: %paste >def greet(name): > print(f"Hello {name}")
## -- End pasted text --
In [3]: greet("Sebastian") Hello Sebastian
23. 列出所有变量
使用%whos命令可以显示出当前会话的所有变量,包括变量类型和存储的数据。
In [1]: a = 100
In [2]: name = "Sebastian"
In [3]: squares = [x*x for x in range(100)]
In [4]: squares_sum = sum(squares)
In [5]: def say_hello(): ...: print("Hello!") ...:
In [6]: %whos Variable Type Data/Info ----------------------------------- a int 100 name str Sebastian say_hello function <function say_hello at 0x111b60a60> squares list n=100 squares_sum int 328350
# The following code would fail in the standard Python REPL # because we can't call await outside of an async function In [3]: await asyncio.gather(worker(), worker(), worker()) Hi Hi Hi Bye Bye Bye