— count words in html file with python

#!/usr/bin/python

import nltk
import string
from urllib import urlopen
from itertools import imap

url = "http://google.com"
html = urlopen(url).read()
text = nltk.clean_html(html)
text_noPunc = text.translate(string.maketrans("",""), string.punctuation)
words = text_noPunc.split()
max_word_len = max(imap(len, words))
vocabulary = nltk.probability.FreqDist(words)

for word in vocabulary:
    print word,
    print ' ' * (max_word_len + 5 - word.__len__()),
    print str(vocabulary[word])

Using NTLK.

Gist on Github.

— DualBoot Archlinux/Windows7 Installation

Documentation of the process of installing archlinux and Windows 7 in a dual-boot configuration.

System Specs

  • AMD X2 dual-core processor, running at 2.8 ghz, circa 2002
  • ASUS ATX motherboard
  • 2 gigs of ram
  • 100gb drive
  • nvidia gtx 550 ti graphics card
  • linksys wmp54g wireless pci network card
  • 550w power supply

Installing Windows

Windows was installed from a USB stick using Microsoft's Windows 7 USB/DVD tool. It's required to create this key from a Windows 7 system. Boot with the USB drive, then install Windows on the drive in a single partition. Windows also creates a System Reserved partition for itself. Once installed, via Start Menu > Administrative Tools > Computer Management > Disk Manamagent, select 'Shrink Partition' on the main Windows parition to create another partition for Arch. The default value for the shrink is 50%, so for my setup the value for the new disk size was ~50GB, which was ideal.

Note about this install. I have an old Linksys (Cysco) WMP54G Wireless PCI Card. Drivers from Linksys/Cysco's website didn't work, but following this blog post, the generic RALink drivers worked great.


more →

— print formatted JSON, XML from osx command line

Working with various interfaces that output json or xml results in lots of situations where you have a single-line, unformatted output. Here's a quick way to format json, xml from the CLI.

via the command line:

format json

cat unformatted.json | python -m json.tool

format json from clipboard

pbpaste | python -m json.tool

format xml from clipboard

pbpaste | xmllint --format -
xmllint is part of libxml2 and installed by default on OSX. Be aware that xmllint cleans up XML as well as formatting it, ocassionally modifying the output.

for all above examples, you can pipe back to the clipboard with | pbcopy at the end of the command, or output to a file with > output.json or > output.xml.

— command line audio on OSX

Since there is no equivilent to /dev/dsp or /dev/audio on OSX, you need to install an alternative like sox.

Install sox by either downloading the OSX binary from http://sox.sourceforge.net/ or install homebrew, and then install sox with brew install sox

Examples

cat audio from /dev/urandom/ :

cat /dev/urandom | sox -traw -r44100 -b16 -u - -tcoreaudio

audio from an executable

cat > test.c
main(t) {
  for( t = 0;;t++)
    putchar( t * ((( t >> 12 ) | (t >> 8)) & (63& (t >> 4 ))));
}
[ctrl-c]
gcc test.c -o test
./test | sox -traw -r8000 -b8 -u - -tcoreaudio

references


— command history

history | awk {'print $2'} | sort | uniq -c | sort -k1 -rn | head

example

// brand new netbook
  24 ls
  14 cd
  12 defaults
   9 unzip
   8 ssh
   5 mv
   3 mkdir
   3 chmod
   3 cat
   2 unrar
// quilime.com
 173 git
 140 ls
  84 cd
  18 emacs
  15 cat
  13 mv
  12 rm
   5 ln
   4 mkdir
   4 ./scripts/content

— Terminal Emulator on Windows via Cygwin

How to install cygwin on windows:

  1. Install Cygwin from setup.exe

  2. Install the following packages:

    • xorg-server (required, the Cygwin X Server)
    • xinit (required, scripts for starting the X server: xinit, startx, startwin (and a shortcut on the Start Menu to run it), startxdmcp.bat )
    • xorg-docs (optional, man pages)
    • X-start-menu-icons (optional, adds shortcuts to X Clients and Server to the Start menu)
    • mintty (windows-feel terminal)
  3. Optional Packages

    • openssh
    • git, svn
    • wget, curl
    • rsync
    • vim, emacs
    • any additional fonts



more →

— create bootable iso from unix/osx terminal

  1. diskutil list
    Determines the device node assigned to your flash media (e.g. /dev/disk2)

  2. diskutil unmountDisk /dev/disk#
    Replace # with the disk number from the last command; in the previous example, # is 2)

  3. sudo dd if=/path/to/example.iso of=/dev/diskN bs=1m
    Replace /path/to/example.iso with the path to the iso; for example: ./windows7.iso. After typing in your sudo password, the process will start invisibly.

  4. diskutil eject /dev/disk#
    Remove your flash media device when the command completes. Done!

Referenced from BurningIsoHowto

Bonus tip! You want to see how far the dd copy is coming along? Run in another terminal instance:
$ sudo killall -INFO dd
The process info will display in the original terminal.

— filebrowser

File Browser PHP script on GitHub.

demo: media.quilime.com

— show hidden files (OSX Finder)

via Terminal

show hidden files:

defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder

hide hidden files:

defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder

— Combine Files (Windows)

copy /b file1+ file2 output

eg

copy /b picture.jpg + archive.rar file.jpg

Open file.jpg with the default application, it will show the picture "picture.jpg". Change the extension to "file.rar" or if you try to open "file.jpg" with an archiver you will get the contents of "archive.rar".

— create ssh keys

ssh-keygen -t rsa
// linux
ssh-copy-id [user@]host
// osx
cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
eval `ssh-agent`
ssh-add

— linux bash profile

alias ls='ls --color'
export CLICOLOR=1
export LSCOLORS=gxFxCxDxBxgggdabagacad

export EDITOR='emacs'

— osx bash profile

# color ls
export CLICOLOR=1
export TERM=xterm-color
export LSCOLORS=GxFxCxDxBxegedabagacad


function parse_git_branch {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo "("${ref#refs/heads/}")"
}

#Black       0;30     Dark Gray     1;30
#Blue        0;34     Light Blue    1;34
#Green       0;32     Light Green   1;32
#Cyan        0;36     Light Cyan    1;36
#Red         0;31     Light Red     1;31
#Purple      0;35     Light Purple  1;35
#Brown       0;33     Yellow        1;33
#Light Gray  0;37     White         1;37

# colors
RED="\[\033[31m\]"
GREEN="\[\033[32m\]"
YELLOW="\[\033[33m\]"
BLUE="\[\033[0;34m\]"
BLUE_B="\[\033[1;34m\]"
WHITE_B="\[\033[1;37m\]"
NO_COLOR="\[\033[0m\]"

# prompt with git repo
export PS1="$WHITE_B\u@\h $BLUE_B\w $RED\$(parse_git_branch) $NO_COLOR \n$BLUE_B\$ $NO_COLOR"
export PS2="$BLUE_B> $NO_COLOR";

# aliases
alias ff="find . -type f -name "

— resize multiple images

for k in $(ls *.JPG); do convert $k -resize 50% -quality 80 r_$k; done

— Maya render bat batch file

*.bat file structure

REM // .bat comment syntax
C:\Progra~1\Autodesk\Maya2010\bin\render -s 1 -e 30 -im output_image myscene.ma
C:\Progra~1\Autodesk\Maya2010\bin\render -s 1 -e 30 -im output_image2 anotherscene.ma

The path to render.exe must be the 8.3 character DOS version.
To execute the file, save as a *.bat and double click it in Explorer.


more →

— Rewrite URL: Replace Double Slashes With Single Slash

RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

— date for new filename

#!/bin/bash
# Shell script to create file named after the current date
# YYYY-MM-DD format

DATE=$(date +%Y"-"%m"-"%d)
echo -e "new file" > $DATE

— Rename Multiple Files via Shell

ls | nl -nrz -w2 | while read a b; do mv "$b" filename.$a.png; done;

— Sphere Intersect in Maya/MEL

Function to return location of intersect with poly mesh and spherical object moving in the positive direction on the Y axis.


more →

— Image Slice

Shell script that slices a single image into any number of vertical and horizontal sections.

#!/bin/bash
#@author  gabriel dunne <quilime.com>

IMAGE=$1
IMAGE_W=$2
IMAGE_H=$3
ROWS=$4
COLS=$5

if [ $# -eq 0 ]
then
    echo "usage: image width height rows cols"
    echo "example: ./slice.sh Sunset.jpg 800 600 16 16"
    exit
else

    for (( x = 1; x <= COLS; x++ ))
    do
        for (( y = 1 ; y <= ROWS; y++ ))
        do
            let CROP_X = `expr $IMAGE_W-IMAGE_W/$x`
            let CROP_Y = `expr $IMAGE_H-IMAGE_H/$y`
            let CROP_W = `expr $IMAGE_W/$ROWS`
            let CROP_H = `expr $IMAGE_H/$COLS`
            echo -n "crop ${CROP_W}x${CROP_H}+${CROP_X}+${CROP_Y} result: [${x},${y}]_$IMAGE"
            echo ""
            convert $IMAGE -crop ${CROP_W}x${CROP_H}+${CROP_X}+${CROP_Y} [${x},${y}]_$IMAGE
        done
    done
    
fi

more →

— MEL Notepad

mel notepad with various code snippets

process selection list

string $select[] = ls -sl;
for ( $node in $select ) // process each
{
   /* ... */
}


if node exists

string $node = "object";
if ( objExists $node )
{
   // The node exists
}

regexp

Strip component

string $node = "pCube1.f[2]";
string $no_component = match "^[^\.]*" $node;
// Result: "pCube1" //


more →

↑ index