Craig W. Wright

Musing and observations

Archive for the ‘Software, Technology, and Computing’ Category

This is a broad catch all for my technical stuff. If it starts to fill up I’ll probably sub-categorize, but for now there’s just one thing!

Bash function for ‘cd’ aliases

Posted by kungfucraig on Sunday, June 21, 2009

For about two weeks there I thought I was going to drop Cygwin for Windows Power Shell. As I was in the process of doing that I ran across an interesting WPSH function by Peter Provost. Nothing earth shattering, but it was pretty useful. Keep a map of aliases to full paths and provide a function “go” that changes your current directory to the full path when you invoke “go” with the appropriate alias.

I got used to using this in WPSH and decided that I would implement it in bash. Of note: I had a number of aliases and shell functions defined before that did this, but using the map, or in the case of bash a file, to hold the aliases makes the whole scheme a heck of a lot more extensible.

Anyway here it is:

##############################################
# GO
#
# Inspired by some Windows Power Shell code
# from Peter Provost (peterprovost.org)
#
# Here are some examples entries:
# work:${WORK_DIR}
# source:${SOURCE_DIR}
# dev:/c/dev
# object:${USER_OBJECT_DIR}
# debug:${USER_OBJECT_DIR}/debug
###############################################
export GO_FILE=~/.go_locations
function go
{
   if [ -z "$GO_FILE" ]
   then
      echo "The variable GO_FILE is not set."
      return
   fi

   if [ ! -e "$GO_FILE" ]
   then
      echo "The 'go file': '$GO_FILE' does not exist."
      return
   fi

   dest=""
   oldIFS=${IFS}
   IFS=$'\n'
   for entry in `cat ${GO_FILE}`
   do
      if [ "$1" = ${entry%%:*} ]
      then
         #echo $entry
         dest=${entry##*:}
         break
      fi
   done

   if [ -n "$dest" ]
   then
      # Expand variables in the go file.
      #echo $dest
      cd `eval echo $dest`
   else
      echo "Invalid location, valid locations are:"
      cat $GO_FILE
   fi
   export IFS=${oldIFS}
}

Posted in Software, Technology, and Computing | Leave a Comment »

Running Python Scripts Inside Windows Power Shell

Posted by kungfucraig on Sunday, May 31, 2009

I took a big leap and decided that I was going to trade in cygwin for Windows Power Shell. Well, at least that’s the theory. We’ll see how it actually goes in the long run.

Anyway, I have quite a few python scripts that I was using, which I can pretty easily port to native Windows Python. But of course I want to be able to run them inside of WPSH.

Assuming a script named “tst.py” in the current working directory, I tried a variety of things to make this happen:

  • Just type in the name of the file “./tst.py”. This zapped up a new command window and ran the python script in it. This is not very useful because I really need to see the output. (Note: If you do this inside of a command shell the behavior is correct.)
  • Rename the file from “tst.py” to “tst.pyw” so that pythonw.exe will be used to run it. Also a no go. I got no new command window, but also no stdout to the WPSH.

Those were the obvious things to try. So I began to search, and found references to IronPython and how to run it inside of WPSH. That and a variety of other things, but nothing that solved my actual problem. (i.e. got my current python scripts to run correctly in WPSH).

Now, if I run python itself from WPSH I get a python prompt from where I can do python stuff. So I tried typing “python tst.py” in WPSH and viola I saw my output.

Since that worked I went ahead and defined the following:

function tst { python tst.py $args}

Given that I have a new WPSH function that calls python on the file “tst.py” and passed through the args. This has the behavior I was looking for. The big downsize here is that I have to write a WPSH wrapper function for each of my python scripts, but I have only about a dozen so that’s what I’m going to do. I am not 100% satisfied with the soluton, but it’s good enough for now.

Update:

I found a better way to do this here. Essentially put:

@setlocal enableextensions & python -x "%~f0" %* & goto :EOF

At the top of your script.

Posted in Software, Technology, and Computing | Leave a Comment »

Deriving from PyQt4.QtCore.QObject

Posted by kungfucraig on Tuesday, March 10, 2009

I was trying to connect the signal of an object I had derived from QObject to the slot of a QWidget. It turns out that if you do not call the QObject.__init__ method in your Python object’s __init__ function that you will get a, “RuntimeError: underlying C/C++ object has been deleted”, exception. It took me a bit of time to figure this out, and since the PyQt documention is sometimes lacking I thought I’d share.

Posted in Software, Technology, and Computing | Leave a Comment »