# iPython Quest OR # Pythonny Quest
### BY: [Hobson Lane](#links) (nlpia.org) [
1
](#links) ### AT: [San Diego Python](#links) (.org) [
2
](#links) Note: - Before we get started, which sounds better to you: - Which sounds more fun to you - Pythonny-Quest - or - iPython-Quest
### Jun 26, 2025 --v-- ## TL;DR 0. Goals 1. Intelligent automation 2. Pexpect Note: I'll start with the lessons learned so everyone can get something out of it even if I don't have time to finish it all. --v-- ## Goals - **``iPython`` REPL** - interactive challenges
```bash $ pip install ipython-quest # OR $ git clone git@gitlab.com:hobs/pythonny-quest $ cd pythonny-quest $ pip install -e . ``` ```bash $ pyquest || ipython-quest || ipython Welcome to the iPython adventure game! # Hint: import pyquest >>> import pyquest # Hint: help(pyquest) >>> help(pyquest) Help on package pyquest: ... ``` Note: Ideas from: - **django shell_plus** - **sageworks** -> **workbench** - **``Quest.__doc__``** attribute --v-- ## auto imports Ć la `django shell_plus` ```python >>> quest = Quest >>> quest "To get started, type and run `help()` or `help(quest)`" >>> help(quest) ``` Note: Show of hands if you've ever used Django's shell_plus? --v-- ## No Cheating - Paste - Fast typing? Note: - **Ctrl-V, Shift-Insert - `import typing` - **Mouse actions** (menus, buttons)? - **Technical interview** - Favorite standard library module? - Open process text files - Import unneeded packages --v-- ## Approaches 1. ask ClaudeCode 2. imitate a working program 3. RTFM Note: Of course my first thought was to do a little cheating myself, so see if I need to update my mental model about how useful LLMs are for learning. Spoiler alert: I only cheated myself out of several hours of my life wasted fixing nonsense code. --v-- ## Ask ClaudeCode - Prompted Claude with requirements - Generated 4 different programs - More than 2000 lines of code - All 4 were broke - None were portable --v-- ## Bugs Syntax, Semantic, and Logic errors. 1. non-existent prompt-toolkit ``Keys`` 2. disabled many printable characters 3. only detected **`ctrl-v`** 4. ignored mouse pastes Note: 1. ``Space`` a special character 2. ``string.printable`` 3. ``.Insert``, ``.ControlInsert``, ``.ShiftInsert`` --v-- ## Spaced Out #### BAD ```python @new_bindings.add(Keys.Space) def _(event): monitor.log_keystroke('SPACE') event.app.current_buffer.insert_text(c) ``` #### GOOD ```python for char in string.printable: ... ``` Note: Because there is a ``Keys.Backspace`` Claude assumed there should be a ``Keys.Space``. This is because of the semantic similarity between --v-- ## ignored printable Keys #### BAD ```python for char in 'abcdefghij...STUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?/`~': @new_bindings.add(char) def _(event, c=char): monitor.log_keystroke(c) event.app.current_buffer.insert_text(c) ``` #### BETTER ```python for char in string.printable: ... ``` --v-- #### BETTER ```python @new_bindings.add(Keys.Enter) def _(event): monitor.log_keystroke('ENTER') event.app.current_buffer.validate_and_handle() @new_bindings.add(Keys.Backspace) def _(event): monitor.log_keystroke('BACKSPACE') event.app.current_buffer.delete_before_cursor() ``` #### ``` ``` --v-- ## 0.1.1.3 paste keystrokes #### only ctrl-V ```python # Detect Ctrl+V paste @new_bindings.add(Keys.ControlV) def _(event): print("\nšØ PASTE SHORTCUT DETECTED: Try typing the code yourself!") monitor.log_keystroke('CTRL+V_PASTE') ``` #### more paste keystrokes ```python # Monitor more keystrokes for k in (Keys.ControlV, Keys.Insert, Keys.ControlInsert, Keys.ShiftInsert): @new_bindings.add(k) def _(event): print("\nšØ PASTE SHORTCUT DETECTED: Try typing the code yourself!") monitor.log_keystroke(f'{str(k)}_PASTE') ``` --v-- ## git #### display_keys.py [
](/2025-06-19-images/display_keys_insert_copy_paste_shift_v_menu.png) --v-- ```bash āāā Key Display Tool āāā Press any key to see its Keys enum value Press Ctrl+C or Ctrl+Q to exit Key History (last 5 keys): āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā [13:32:48.260] Keys.KeyPress(key=
'>, data='import...') (Special key) [13:32:32.792] Keys.KeyPress(key=
'>, data='') (Special key) [13:32:31.076] Keys.Keys.Insert (Insert key) [13:32:22.552] Keys.KeyPress(key=
, data='\x16') (Special key) [13:32:21.249] Keys.KeyPress(key=
'>, data='- `import typing`\r') (Special key) ``` Note: You could truncate the ``data='import...'`` to force user to use tab-completion. --- ## More Intelligent Agents 1. `uv` 2. `source` 3. `mkslides` 4. `start_ipython()` only works when ... Note: Agentic LLMs are just fuzzy automation (good guessers). 1. Rust ``uv`` faster better cheaper 2. Bash ``source`` is tricky 3. Python ``mkslides`` auto-deploys nlpia.org 4. from `django shell_plus` You learn best-practices if you use high quality packages as your guide. --v-- ## 1.1 `uv` vs `pdm` #### `pdm` is clever ```bash pip install pdm eval $(pdm venv activate in-project) pdm install . pdm add . ``` #### `uv` is intelligent ```bash pip install uv source .venv/bin/activate uv pip install -e . alias pip='uv pip' ``` Note: - Heard of `pdm` or `uv`? - But you said it was Rust? - Your preferred package manager? --v-- ## `uv` is fast #### `alias pip='uv pip'` ```bash $ pip install pexpect Audited 1 package in 69ms $ pip install ipython Audited 1 package in 21ms ``` ```bash $ pip install --upgrade pexpect Resolved 2 packages in 117ms Audited 2 packages in 0.08ms $ pip install --upgrade ipython Resolved 17 packages in 224ms Prepared 1 package in 1ms Uninstalled 1 package in 6ms Installed 1 package in 12ms - ipython==9.1.0.dev0 (from file:///home/hobs/code/hobson/ipython) + ipython==9.3.0 ``` Note: - **``pexpect``** tiny; previously installed elsewhere - **``ipython``** big; already installed in ``.venv`` --v-- ## `workon` #### _`.workon`_ ```bash source .venv/bin/activate \ || ( uv venv --python 3.11 .venv \ && source .venv/bin/activate \ && uv pip install -e . ) ``` Note: - learn something -> encode it - help your teammates or future self --v-- ```bash uv init # pdm init uv pip install # pdm install uv add # pdm add uv venv --python 3.12 # pdm venv create uv build # pdm build uv publish # pdm publish ``` Note: - almost always instantaneous for all these commands - uses existing APIs (pip, venv) - uses command-line args (with defaults) - if you can't predict what your tool will do, it's not a tool, it's a chaos gorilla --v-- ## 1.2 `source` is too smart "In the face of ambiguity, refuse the temptation to guess." #### BAD: ```bash $ source .env $ source .workon ``` #### OK: ```bash $ source .venv/bin/activate ``` Note: - While we're on the topic of `activate` and `venv` ... - `source` is **NOT** pythonic. - Do you see the bug? - Which `.env` file will this run? - Skipping this tiny character cost me days. - Your future self will thank you if you fix it tonight. --v-- ## Pythonic Bash "Explicit is better then implicit." #### `source ./.env` --v-- #### _`ipython-quest/.workon`_ ```bash source .venv/bin/activate || ( \ uv venv --python 3.11 .venv \ && source .venv/bin/activate \ && uv pip install -e . \ && source ./.env ) ``` --v-- ## 1.5 Smaller == Smarter ```bash alias aider='AIDER_MODEL=ollama_chat/qwen2.5-coder:32b aide' ``` --- ## ``pexpect`` review - spawn a process - send str to stdin - monitor stdout & stderr - move cursor (e.g. arrow keys) Note: - Great for monitoring printable keystrokes - Can't prevent copy/paste - Can't execute --- ### Links 1. [nlpia.org](nlpia.org) 1. [SanDiegoPython.org](https://sandiegopython.org)