Discussion forum for David Beazley

How to modify Python interpreter


#1

Hi David, I’m a big fan of your talks. I watched Builtin Super Heros yesterday and besides the content (which was a wonderful refresher!) I was interested in the modified python interpreter you were using. Ya ya notebooks are cool and all but there is sort of a retro-cool simplicity in building slides as ascii art in a live interpreter session. How did you do it?


#2

Yes David, share us your secrets :slight_smile:


#3

Ah yes, the modified interpreter from the Superheroes talk. In truth, it’s not really a modified interpreter so much as a monkey-patched environment. The interpreter was a stock Python-3.5 installation. A number of things were patched to produce different output. For example, the builtin types (dict, list, set, etc.) were replaced by a wrapper object:

 class SlideWrapper(object):
       def __init__(self, cls, text):
             self.cls = cls
             self.text = tex
       def __call__(self, *args, **kwargs):
            return self.cls(*args, **kwargs)
       def __repr__(self):
            # Create the slide text
            return result

dict = SlideWrapper(dict, '... slide text ...')
tuple = SlideWrapper(tuple, '... slide text ...') 
set = SlideWrapper(set, '... slide text ...')

If you merely typed ‘dict’, it would then show a custom slide. So, that was one of the modifications.

Other modifications included a custom display hook installed via sys.displayhook. This was looking for certain keywords which, when supplied, would produce slide output. The id() function was replaced by an alternative that was looking for very specific things (known in advance).

Display of the image in the middle of the talk was done using special escape sequences of iTerm2 that allow inline images to be displayed in the terminal (see the code for the imgcat program demo in iTerm2 for an example).

For the most part, all of it was very custom and not especially reusable. Just having fun messing with people mostly ;-).


#4

Thanks for your attention !


#5

I thought, It was created by import code library, but, SlidWrapper , look so good idea, I have to modify mine . thanks Dave.:heart_eyes:


#6

This presentation was so great, that I started developing something like a library to do this. Feel free to check it out!


#7

Oh, interesting. I’ve been wondering if I could use some of the REPL-presentation stuff in the context of teaching courses. There are a few challenges though. Unlike a conference talk, a training class might have upwards of 600 presentations slides presented over multiple days. I’m not exactly sure how I could mentally coordinate something like that ;-).

It’s still something I might fool around with later though.


#8

Thanks Dave, Alex for sharing these…

After watching Dave’s two talks, I cobbled something up for use in my weekly python office hours I ran for a while at Intel - my approach was different, and I intended to get back to it.

There’s a lot I’m learning from looking at Alex’s code (I’ve never pondered / played with displayhook, for example).

I started with something more archaic and basic (an evening’s 2-beer job), which made me write slides in an nroff-ish format (lines staring w/ '.' == directive) which did three things:

  • explicitly mark start and end of slide; a slide always encapsulated a code snippet;
  • explicitly mark start / end of code sections (which got exec’d into the current context); Alex does this by opening a code file;
  • space between slides where just that - not processed, except for later distribution, and then as doctest format. If anything, this served as “presenter notes;”

I then took the presentation input, and generated a file I could give to people. The slide text turned into python comments (w/ boxing so it was clear they’d been slides). The code emitted as just code, and the notes between slides, doctests. The idea was you could load the file, and try things yourself.

As for Dave’s “complaint” about long presentations / classes, I was looking to mimic something of presentation software with presenters notes, and the ability to see the “index” and scan through / go back to slides. Obviously, exec’ing your context makes it hard to go back too much, but I think breaking this into “sections” or “lessons” (sort of the way Dave’s Python Programming video was organized) would make things manageable.

I’m not sure I grok Dave’s wrapper motivation… I’ll have to look at that more later.


#9

Thank you, Mr. Dave.