# Python Automation ## with # Pexpect #### Hobson Lane #### Python User Group #### May 22, 2025 --v-- ## TL;DR 1. SoTA code gen 2. GnuChess is real AI 3. Automate game play with Pexpect --v-- ## Codegen assistants LLMs integrated into your IDE are more efficient
4
1. **[Cursor](https://www.cursor.com/)** - VSCode fork - Gemini 2.5 Pro 2. **[Zed](zed.dev)** - Sublime ported to Rust - by maker of Atom - Claude 3.7 Sonnet note: I queried Llama3.2 and Codellama for outline syntax and Llama3.2 was better even than codellama. Shy failed miserably. Likewise for these speaker notes I'm reading to you. --v-- ## Rust tooling for Python - ruff (faster pylint) - UV (better pip) - Zed --v-- ## Codegen assistants LLMs integrated into your IDE are more efficient 1. Cursor (VSCode fork) + Gemini 2.5 pro 2. Zed + Claude 3.7 Sonnet --- ## Codegen applications - Web development - App development - Devops () --v-- ## Would you like to play a game? Games are fun benchmarks. - easy for humans - hard for LLMs note: Game play is an effective and fun way to benchmark and compare AI algorithms. Games that are straightforward for humans can be challenging for LLMs. The ARC Prize challenge is a game play benchmark that's more difficult and less familiar for machines and humans. --v-- - Boardgames (Landlord's Game) - Jeopardy (trivia) -> IBM Watson - Chess -> IBM Deep Blue - Video Games -> - Go -> Alpha Go - -> Alpha Go Zero - -> Alpha Fold - -> Alpha - -> Alpha Evolve - [ARC Prize](arcprize.org) is the SoTA game for AGI. note: Show of hands for anyone who has played "The Landlord's Game", invented in 1903 by Elizabeth J. Magie --v-- ## The Landlord's Game  note: Show of hands for anyone who has played "The Landlord's Game", invented in 1903 by Elizabeth J. Magie --v-- ## Game utility functions - zero sum - positive sum - negative sum --v-- ## Game play is legal+ethical - no black listing - no DMCA - no FBI knocking on your door --v-- ## Games are infinite All you need are: - rules of game/problem - simulation game moves note: You don't even really the rules of the game, you just need a simulation or a fast, cheap, real-world system to interact with. --- ## CLI Games Simple and not-so-simple CLI games | name | year | lines | lang | sep | notes |---------------|-----:|------:|------|-----|---------------------------------------------------------------------| | dialog_tree | 2017 | 89 | py2 | \| | [jonjanelle](github.com/jonjanelle/DialogTree) | | dialog | 2019 | 89 | py3 | ~ | [obxfisherman](github.com/obxfisherman/dialog_tree) | | Colossal Cave | 1971 | 2500 | py3 | \\n | [Will Crowther](github.com/brandon-rhodes/python-adventure) | | GNU Chess | 2011 | 4000 | C | nan | [Stockfish](github.com/madnight/gnuchess) | | Eliza | 1966 | 236 | py | nan | [Joseph Weizenbaum](github.com/wadetb/eliza/blob/master/eliza.py) | --- ## GNU Chess ```bash $ gnuchess ``` Try to move a pawn too far: ``` White (1) : e2e6 Invalid move: e2e6 ``` Try again. ``` White (1) : e2e4 ``` --v-- ## ASCII Chess Board ```text 1. e2e4 black KQkq e3 r n b q k b n r p p p p p p p p . . . . . . . . . . . . . . . . . . . . P . . . . . . . . . . . P P P P . P P P R N B Q K B N R ``` --v-- ## ANSI Terminal `pexpect.ANSI.term()` for terminal emulation. Must already be running python within POSIX terminal (Cygwin) - `htop` - Rogue - ASCII art games - APS for armored vehicles ;-) You need `pexpect.ANSI.term()` if text not printed one line by line. --v-- ``` Thinking... ``` --v-- ```bash white KQkq e6 r n b q k b n r p p p p . p p p . . . . . . . . . . . . p . . . . . . . P . . . . . . . . . . . P P P P . P P P R N B Q K B N R My move is : e5 ``` --v-- ## Actual reasoning (CoT) ``` White (1) : e4 ... Thinking... 1 -40 0 2 a5 White (1) : 1 -34 0 6 b6 1 +8 0 42 d5 1 +10 0 77 Nc6 2 -44 0 120 Nc6 Nc3 ... 13 -19 344 12423426 Nf6 e5 Nd5 Nf3 Nc6 d4 e6 c4 Bb4+ Bd2 Nb6 Bd3 d5 c5 13 -15 604 21257361 e5 Nc3 Nf6 Nf3 Nc6 Bb5 Bb4 d3 O-O Bxc6 dxc6 Nxe5 Qd4 Nc4 Bxc3+ bxc3 Qxc3+ Bd2 13 -15 1183 41600000 e5 Nc3 Nf6 Nf3 Nc6 Bb5 Bb4 d3 O-O Bxc6 dxc6 Nxe5 Qd4 Nc4 Bxc3+ bxc3 Qxc3+ Bd2 ``` --v-- ## Easy mode ```bash $ gnuchess --easy ``` --v-- ## UCI (not UC Irvine) ```bash $ gnuchess --uci Chess ``` ### Universal Chess Interface --v-- ## X-Board (engine mode) ```bash $ gnuchess --post --xboard Chess ``` --v-- ## `go` forces machine to move ```bash go 1 +26 0 2 Na3 ... 15 +17 2728 89787074 e4 Nf6 e5 Nd5 Nf3 Nc6 d4 e6 Bb5 Bb4+ Bd2 O-O O-O a6 Bxc6 dxc6 Bxb4 Nxb4 1. ... e2e4 My move is : e2e4 ``` ## Logical reasoning (CoT) ```bash 1 -17 0 2 (Nf6) e5 1 +44 0 5 (Nf6) Nc3 2 -10 0 82 (Nf6) Nc3 Nc6 3 +4 0 412 (Nf6) Nc3 d5 Qf3 3 +9 0 546 (Nf6) e5 Ne4 Nc3 4 +14 0 1042 (Nf6) e5 Nd5 Nc3 Nf4 5 +15 0 1710 (Nf6) e5 Nd5 Qf3 Nb4 Na3 6 +3 0 3715 (Nf6) e5 Nd5 Nf3 Nc6 Nc3 Nf4 7 +18 1 19772 (Nf6) e5 Nd5 Nf3 Nc6 Bc4 Nb6 Bb5 8 +3 1 40244 (Nf6) e5 Nd5 c4 Nb4 Nc3 d6 a3 N4c6 9 +17 3 100675 (Nf6) e5 Nd5 Nf3 Nc6 c4 Nb6 Nc3 d5 d3 10 +12 8 259701 (Nf6) e5 Nd5 d4 Nc6 c4 Nb6 c5 Nd5 Nc3 Ncb4 11 +11 24 767457 (Nf6) e5 Nd5 Nf3 d6 Nc3 Nxc3 dxc3 Nc6 Bb5 Bf5 O-O dxe5 Bxc6+ bxc6 Nxe5 Qxd1 Rxd1 Bxc2 11 +16 38 1172976 (Nf6) Nc3 Nc6 Nf3 d5 exd5 Nxd5 Bb5 Nxc3 dxc3 Qxd1+ Kxd1 Bf5 Bxc6+ bxc6 Nd4 Rd8 Bf4 12 +6 69 2160919 (Nf6) Nc3 Nc6 Nf3 d5 exd5 Nxd5 Bb5 Nxc3 dxc3 Qxd1+ Kxd1 Bf5 Bxc6+ bxc6 Nd4 O-O-O Bd2 Be4 12 +17 69 2161287 (Nf6) e5 Nd5 Nf3 Nc6 d4 e6 Bb5 Bb4+ Bd2 O-O O-O a6 Bxc6 dxc6 Bxb4 Nxb4 13 +17 69 2161745 (Nf6) e5 Nd5 Nf3 Nc6 d4 e6 Bb5 Bb4+ Bd2 O-O O-O a6 Bxc6 dxc6 Bxb4 Nxb4 14 +26 353 11178370 (Nf6) e5 Nd5 Nf3 Nc6 d4 e6 Bc4 Bb4+ Bd2 d6 Bxd5 Bxd2+ Nbxd2 exd5 O-O O-O Qe2 dxe5 dxe5 15 +36 898 29662589 (Nf6) e5 Nd5 Nf3 Nc6 d4 e6 Bc4 Bb4+ Bd2 Nb6 Bb5 Nd5 Nc3 O-O Nxd5 Bxd2+ Qxd2 exd5 16 +28 2045 67574002 (Nf6) e5 Nd5 Nf3 Nc6 d4 e6 Bc4 d6 O-O Be7 exd6 Qxd6 Nc3 Nxc3 bxc3 O-O Rb1 e5 e7e5 1. e7e5 ``` --v-- ```bash 1. e7e5 ... 13 +18 706 24417876 Nf3 Nf6 Nc3 Nc6 Bb5 Bc5 O-O O-O d3 d6 Bxc6 bxc6 Be3 Bb6 2. ... g1f3 My move is : g1f3 1 +54 0 2 (Nf6) Nc3 ... d14 +36 1799 63999322 (Nf6) Nxe5 d6 Nf3 Nxe4 Nc3 Nxc3 dxc3 Nc6 Bd3 Be7 O-O O-O Re1 Be6 Bf4 7d6 2. d7d6 ``` --v-- ## Beam search ``` 1 +59 0 2 Na3 1 +87 0 3 Nc3 ... 13 +67 968 34233124 Nc3 Nf6 d4 exd4 Nxd4 Be7 Bc4 Nbd7 O-O Ne5 Bb5+ c6 Be2 O-O Bf4 3. ... b1c3 My move is : b1c3 1 +78 0 4 (Nf6) d4 ... 14 +51 1266 44652980 (Nf6) d4 exd4 Nxd4 Be7 Be2 Nbd7 O-O O-O Bf4 Nc5 f3 Nh5 Be3 Bg5 f4 ``` ## References 1. [2015 New Scientist - Google Deep Mind AI Outplays Humans at Video Games](https://www.newscientist.com/article/dn27031-google-deepmind-ai-outplays-humans-at-video-games/) 2. [Fake history - Landlord's Game](https://landlordsgame.info/) 3. [Wikipeida - Landlord's Game](https://en.wikipedia.org/wiki/landlords-game) 4. [LLM Example Fails at mesa_python.gitlab.io](https://mesa_python.gitlab.io)