Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I'm writing Python, I wanna be able to pull up an interpreter quickly, and import what I need easily. Previously, this involved:

bash4.3 $ python3
>>> import os, sys, readline, lxml

I was irked by the amount of typing in that, so I wrote a couple of tiny bash functions that live in my .bashrc, so I can just do (for example):

bash4.3 $ from os import path
>>> 
from os import path: success!
>>> 

or:

bash4.3 $ import os, sys, readline, lxml
>>> 
imported os, sys, readline, lxml
>>> 

Perhaps this might be regarded by some as a non-issue, but for my workflow when I just want to test something quickly, it's really handy.

The functions:

function import () {
  ARGS=$*
  ARGS=$(python3 -c "import re;print(', '.join(re.findall(r'([\d\w]+)[, ]*', '$ARGS')))")
  echo -ne '\0x04' | python3 -i
  python3 -c "import $ARGS" &> /dev/null
  if [[ $? != 0 ]]; then
    echo "sorry, junk module in list"
  else
    echo "imported $ARGS"
  fi
  python3 -i -c "import $ARGS"
}

function from () {
  ARGS=$*
  ARGS=$(python3 -c "import re; s = '$ARGS'.replace(',', ', '); args = ' '.join(re.findall(r'^([\d\w]+) import ([\d\w, ]+)$', s)[0]).split(' '); print('from', args[0], 'import', ' '.join(args[1:]).replace(',', '').replace(' ', ','))")
  echo -ne '\0x04' | python3 -i
  python3 -c "$ARGS" &> /dev/null
  if [[ $? != 0 ]]; then
    echo "junk module in list"
  else
    echo "$ARGS: success!"
  fi
  python3 -i -c "$ARGS"
}

I'm aware python3 -m module exists, however, it requires module to be a fully-fledged module, with an __init__.py and stuff. If I'm writing a small script that isn't really designed to be a module (i.e, that has module-level expressions that will be run if the module is imported), then I want to import it normally, not with -m.

I'm also aware I could use && and || for short-circuiting over if [[ ]]; then; fi but I consider this more readable.

I'm looking for responses about any part, really, but I'd be most interested to hear whether I'm unknowingly abusing bash in some way (quoting, perhaps?) and about the probable inefficiency of the Python code I'm using to process / sanitise the args.

I'd also be happy to hear about corner cases not caught by my regex / parsing job, and how they sudo rm -rf --no-preserve-root /-ed you.

share|improve this question
    
Now, why did the Python tag get put ahead of the bash one? :( I put the Python tag second for a reason. – cat Feb 18 at 20:16
    
Have you heard of virtualenv ? Does it not suit you for some reason? – janos Feb 21 at 0:06
    
@janos No, but the last I looked into it, it's more work and time than I need – cat Feb 21 at 0:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.