Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bare words / new keywords in Python

Tags:

I wanted to see if it was possible to define new keywords or, as they're called in WAT's Destroy All Software talk when discussing Ruby, bare words, in Python.

I came up with an answer that I couldn't find elsewhere, so I decided to share it Q&A style on StackOverflow.

like image 961
ArtOfWarfare Avatar asked Apr 07 '15 13:04

ArtOfWarfare


People also ask

What are the 33 keywords in Python?

In Python, there are approximately around thirty-three (33) keywords, and a few of the keywords generally used in the program coding are break, continue, true, false, and, or, not, for, while, def, class, if, else, elif, import, from, except, exec, print, return, yield, lambda, global, etc.


1 Answers

I've only tried this in the REPL, outside any block, so far. It may be possible to make it work elsewhere, too.

I put this in my python startup file:

def bareWordHandler(type_, value, traceback_):     if isinstance(value, SyntaxError):         import traceback          # You can probably modify this next line so that it'll work within blocks, as well as outside them:         bareWords = traceback.format_exception(type_, value, traceback_)[1].split()          # At this point we have the raw string that was entered.         # Use whatever logic you want on it to decide what to do.         if bareWords[0] == 'Awesome':             print(' '.join(bareWords[1:]).upper() + '!')             return     bareWordsHandler.originalExceptHookFunction(type_, value, traceback_)  import sys bareWordsHandler.originalExceptHookFunction = sys.excepthook sys.excepthook = bareWordsHandler 

Quick REPL session demonstration afterwords:

>>> Awesome bare words BARE WORDS! 

Use responsibly.

Edit: Here's a more useful example. I added in a run keyword.

if bareWords[0] == 'from' and bareWords[2] == 'run':         atPrompt.autoRun = ['from ' + bareWords[1] + ' import ' + bareWords[3].split('(')[0],                             ' '.join(bareWords[3:])]         return 

atPrompt.autoRun is a list of variables that, when my prompt is displayed, will automatically be checked and fed back. So, for example, I can do this:

>>> from loadBalanceTester run loadBalancerTest(runJar = False) 

And this gets interpreted as:

from loadBalancerTest import loadBalancerTest loadBalancerTest(runJar = False) 

It's kind of like a macro - it's common for me to want to do this kind of thing, so I decided to add in a keyword that lets me do it in fewer keystrokes.

like image 193
ArtOfWarfare Avatar answered Oct 05 '22 02:10

ArtOfWarfare