Sunday, November 12, 2017

NodeJs Bash Build Script

#!/bin/bash
# Each lambda is technically its own project with its own dependencies
function npmTask() {
  startingDir=$(pwd)
  npmCommand=$1
  for nodeDir in $startingDir/lambdas/*/;
    do
      echo "start node dir: $nodeDir from $startingDir"
      if [ -d $nodeDir  ] && [ $nodeDir != "$startingDir/lambdas/spec/" ];
      then
        echo "start node dir: $nodeDir from $startingDir"
        cd $nodeDir
        npm $npmCommand
        cd $startingDir
      else
        echo "no node modules found"
      fi
    done
}
npmTask "install"
npmTask "build"
npmTask "test"

Sunday, October 1, 2017

Python Map Reduce on Tuple List

I was recently asked to read some pseudocode for calculating a class average of weighted scores. I wanted to work this out in Python to demonstrate how the design decisions around data structures impact implementation. Two independent lists assume order is maintained properly in two places. However it makes the code easier to explain.

flatScores = [75,95,85,65]
weights = [0.2,0.35,0.15,0.3]

def mult(x,y): return x * y
# map(mult,flatScores,weights)
# [15.0, 33.25, 12.75, 19.5]
reduce((lambda sum,score: (sum + score) ),list(map(mult,flatScores,weights)))/len(flatScores)
#20.125

A single list of tuples allows members to include all relevant data in one place. 

scores = [[75,0.2],[95,0.35],[85,0.15],[65,0.3]]
def mapper(scoreWeights): return map( lambda scoreWeight: scoreWeight[0] * scoreWeight[1] , scoreWeights )
# mapper(scores)
# [15.0, 33.25, 12.75, 19.5]
reduce(lambda sum, cur: sum + cur, mapper(scores))/len(scores)
#20.125

Thursday, May 4, 2017

Bash Function To Create Desktop Shortcuts

Shortcuts are located in three places
/usr/share/applications/intellij.desktop for all users
a folder in the home dir for a given user
~/Desktop to see the shortcut on the desktop

Format is
 [Desktop Entry]
 Version=13.0
 Type=Application
 Terminal=false
 Icon[en_US]=/home/rob/.intellij-13/bin/idea.png
 Name[en_US]=IntelliJ
 Exec=/home/rob/.intellij-13/bin/idea.sh
 Name=IntelliJ
 Icon=/home/rob/.intellij-13/bin/idea.png

The function looks like this:

function add_a_shortcut() {
  APP=$1
  ICON=$2
  EXEFILE=$3
  EXEC="bash -ic \"$EXEFILE &\""
  SHORTCUT="/usr/share/applications/$APP.desktop"
  USERSHORTCUT="/home/vagrant/Desktop/$APP.desktop"
  if [ -e "$EXEFILE" ]; then
    sudo touch $SHORTCUT $USERSHORTCUT
    sudo chmod 777 $SHORTCUT $USERSHORTCUT
    echo "[Desktop Entry]" | tee $SHORTCUT $USERSHORTCUT
    echo "Encoding=UTF-8" | tee -a $SHORTCUT $USERSHORTCUT
    echo "Comment=Launch $1" | tee -a $SHORTCUT $USERSHORTCUT
    echo "Type=Application" | tee -a $SHORTCUT $USERSHORTCUT
    echo "Terminal=false" | tee -a $SHORTCUT $USERSHORTCUT
    echo "Exec=$EXEC" | tee -a $SHORTCUT $USERSHORTCUT
    echo "Name=$APP" | tee -a $SHORTCUT $USERSHORTCUT
    echo "Icon=$ICON" | tee -a $SHORTCUT $USERSHORTCUT
   
    sudo chmod 644 $SHORTCUT
    sudo chown root:root $SHORTCUT
    sudo chown vagrant:vagrant $USERSHORTCUT
    echo "INFO: Created $SHORTCUT $USERSHORTCUT"
  else
    echo "ERROR: Failed to create $SHORTCUT $USERSHORTCUT"
  fi
}
EXAMPLE USAGE
add_a_shortcut aggregation-designer /opt/pentaho/design-tools/aggregation-designer/aggregation-designer.app/Contents/Resources/pad.icns /opt/pentaho/design-tools/aggregation-designer/startaggregationdesigner.sh

Thursday, March 16, 2017

Why is it so hard to install VirtualBox Guest Additions on Centos

 yum update
 yum install gcc make kernel-devel bzip2
 mkdir -p /media/cdrom
 mount /dev/sr0 /media/cdrom
 sh /media/cdrom/VBoxLinuxAdditions.run