Draw a polygon with any number of sides in Python using the "turtle" graphics library

import turtle

def drawPolygon(sides, fillcolor='DarkOliveGreen'):
    #init pen
    turtle.reset()
    turtle.fillcolor(fillcolor)
    turtle.fill(True)

    #draw a shape
    degree = 360.0 / sides
    for i in range(sides):
        turtle.right(degree)
        turtle.forward(degree)
    turtle.fill(False)

Sample usage:
>>> drawPolygon(4)
... draws an olive green square.

Learning emacs and Clojure ever so slowly

Learning_emacs

Meikel Brandemeyer's stable 2.2.0 release of VimClojure has gotten me back into clojure work.  Well, that and my recent forays into Node.JS which reminded me how much I like Clojure syntax.  VimClojure has excellent highlighting and the delectable vim keyboard navigation and editing but the customized nailgun server Meikel distributed with VimClojure 2.2.0 is running somewhat poorly for me.  I've found that lein swank + swank clojure performs better, plus emacs is lisp almost all the way down so it's a further excuse to train myself on functional programming.

Here are a few emacs things I've picked up in the past few days:

M-x describe-key:
Which elisp function does a given key combination invoke?  This is going to come in handy since Vimpulse seems to stomp on swank-clojure's (slime-repl-set-package package) command.  It's the only way I know of to quickly send my current file's namespace over to the SLIME repl.

Cygwin headaches:
  • The native cygwin build of Emacs doesn't work too well for whatever reason and Steve Yegge's blog says back in 2004 that I should use NTEmacs instead.  So I'm doing that, but the file paths are screwy.
  • I downloaded some scripts to deal with cygwin paths and symlinks... good since my code folder is symlinked into my Dropbox space.
  • Java doesn't believe in cygwin paths either, so attempts to play with Google App Engine are going to be more awkward than necessary.
I have experimented with vm-based development and dual booting in the past but I keep coming back to the native dev environment because being able to start coding in less than a minute is important as is fast execution time of my code.

Thanks technomancy, LauJensen, and hiredman in #clojure on freenet for help with various aspects of emacs integration with Clojure.

Next steps: 
Reread the Joy of Clojure while using the latest pre-release chapters of Clojure in Action to help me port my chatbox backend from node.js to clojure.  I might trying hooking it up to redis since I know Amit has a section on redis in his book.

Get up and running with Node.js on your own Amazon EC2 server

Create an Amazon Web Services account.

 

Create a new EC2 Micro-sized instance using the Alestic 32-bit EBS AMI for your region.  Fire it up.

Connect via SSH using your AWS private key and the instructions on the AWS site.  Your username is "ubuntu".  Be sure your EC2 security group is ready to receive connections on port 22 from your local network.
 
Use vim to create a new file named bootstrap.sh with the following contents and then execute it using "sh ./bootstrap.sh".
 
#!/bin/bash
#patch ubuntu
sudo apt-get update -y
sudo apt-get install -y build-essential git-core --fix-missing

#install node.js
wget http://nodejs.org/dist/node-v0.2.1.tar.gz
gunzip node-v0.2.1.tar.gz | tar -xvf
tar -xvf node-v0.2.1.tar
cd node-v0.2.1
./configure
make && make install
cd ~
rm node-v0.2.1.tar

#install node package manager
sudo chown -R $USER /usr/local
curl http://npmjs.org/install.sh | sh

#install coffeescript and run a coffee test app
npm install coffee-script
git clone git://gist.github.com/584584.git
coffee ./584584/gistfile1.coffee

Connect to your server on port 3000 to see if your hello, Node.js script worked or not.

That's it!  Head to the node.js mailing list with any questions.

Read a file to stdout using Node.js

require('fs').readFileSync('abc.txt').toString()); 

Taken from the NodeJs google group, saved until I get the hang of Node.

Clojure snippet of the day

user> (defn ladder [n]
                  (reduce + (range 1 (inc n))))
#'user/ladder
user> (ladder 16)
136

Winning an argument with Steve Jobs isn't easy

"I used to have a notion about what you do if Steve's wrong. How do you talk him off the ledge if you really think he's wrong? And I used to be one of the designated guys who used to go in there and try to do that. And he has so many ways of getting his way when he's right.

 

First off, he's larger than life, because he started Apple and you didn't. So, you give him a lot of room.  Second, his clock speed is faster than yours is, except for Ross Perot's. For some reason, Ross Perot could debate with him in real time. For most of us, he'd get ahead of us, anticipate what we were going to say, cut us off midway in our sentences, and we would have to go out, collect ourselves and go back in.  Third off, he has access to information you don't have, because he says 'Oh, I just got off the phone with the CEO of IBM. He said blah, blah, blah.' And well, you don't have that information.  And then he's, and he's more charismatic than you are. He's more charismatic than anyone. So there's four ways he can get his way when he's wrong.  And the fifth way, which he will use, is if the first four don't work, then he will start challenging your heredity and get all personal about it, which you can't do in return.

 

So, I used to hold the corners of my mouth down when I was talking to him, and he'd start to challenge me, saying I was immature and all that. Because I think if the great Steve Jobs has got to use that to win his argument, then you know you've won the argument. Just get out of there. And three days later, he'll send out an e-mail, maybe, and say, 'hey, I've been thinking, maybe we should consider this.' And if that happens, that's a great thing."

 

Test driving Munin service statistics on a LAMP virtual machine

Munin_on_lamp

Someone looking to host a high-availability LAMP application using multiple servers (such as a cloud-based app with load balancing) needs insight into the performance profile of every piece of the stack.  Munin provides that.

Ubuntu setup was as simple as
sudo apt-get install munin munin-node

The output defaults to http://localhost/munin.  Attached is a screenshot of my first results.  Note that you'll want a tool like nagios to keep track of service and system failures.  Munin appears to be more for profiling performance.

The initial list of Munin graphs on my virtual server was divided into the following categories: 

 

 [ Apache Disk Mysql Network Other Postfix Processes System ]

Learning more about CakePHP and git rollbacks

I broke some stuff with my CakePHP just now due to confusion following the instructions of the Simple ACL tutorial.  I had thought to back up my database with a shell script at the time of my last commit so there was a potential for a rollback.

Unfortunately I haven't yet figured out how to cleanly roll something back in git.  I poked about with that for a while and then decided that my primary focus was getting CakePHP working and not becoming a Git wizard so I just nuked the local copy of the app and did a quick git clone to rebuild it.

Unfortunately that led me to another bout of lost time trying to figure out some PHP errors thrown by CakePHP.  I believe the major problem was that my .gitignore file passed over all of the tmp folders in my CakePHP app and Cake was unable to recover from that.   You can see when you unzip the Cake tarball it comes with stuff preloaded under ./app/tmp.

I just found a snippet on the Bakery that alludes to manually rebuilding the /app/tmp directory structure using the following script:
mkdir -p tmp/cache/models tmp/cache/views tmp/cache/persistent tmp/logs tmp/sessions tmp/tests

I quickly tested this against yesterday's CakePHP sample app and it seems to have worked - now I can reliably restore from a github backup.  I will try this again once I get the ACL tutorial completed to see if it solves all the problems I was seeing earlier today.  I should be able to clone my github backup from earlier today but I actually wiped the whole project so that I could reuse the name cake_acl.

PHP training update

Cakephp_diagnostics

Yesterday I successfully implemented the first CakePHP tutorial - setting up a rudimentary blog.  You can see my source code on GitHub at http://github.com/dpritchett/caketaster

Today I'm pushing through to the second tutorial on the CakePHP site - a site with users and an access control list.  Follow along here: http://github.com/dpritchett/cake_acl

My crude development strategy so far is this:
  1. Unzip the latest CakePHP source into my home directory
  2. Move the resulting directory to /var/www/new_project_name
  3. sudo chown dpritchett /var/www/new_project_name
  4. Set up the database config and secret salt and whatnot to make the CakePHP diagnostic splash page lights go green
  5. Create a new GitHub project to host the code
  6. Step through the tutorial and commit after each new feature is finished.
I am using VirtualBox with the Turnkey LAMP ISO as my starting point.  It's got Ubuntu 8.04 LTS and I just hit apt-get to update everything and get the packages I need.  Editing right now is happening in VIM.  Last night I mirrored the VirtualBox disk image from my work machine over to my home laptop so that my virtual dev server environment can be consistent.

Check back soon for updates on my progress!

Get up and running with a Turnkey LAMP server

I've just set up Turnkey LAMP in VirtualBox so that I can start prototyping some new PHP applications.

I may publish the finished product to my web space on Sharing at Work if I need to see it online.  Hopefully Tubu can handle CakePHP.