tonybaldwin | blog

non compos mentis

Posts Tagged ‘python

Get me some learnin' (MIT Open Courseware)

leave a comment »

So, I decided,”Enough with the tinkering with the hackery…Time to start learning for real.”, and started to take this course (Introduction to Computer Science and Programming) through MIT’s Open Courseware.

So far, I haven’t even “attended” the first lecture (watch a video), but skipped ahead to the first assignment, being like that, and jumped right to solving it. The assignment was to create a program, in any language, that asks the user for their last name, then first name, then prints back the first name, then last name. I did it in 6 different languages, just for fun. ūüėÄ

First python:

#!/usr/bin/env python

# first assignment for MIT intro comp sci class.
# Problem Set 0
# Name: Tony Baldwin
# Collaborators: none
# Time: 0:30

last = raw_input("Please enter your last name:  ")
first = raw_input("Please enter your first name: ")
print("Hello, " + first + " " +  last + "!")

Then bash:

#!/bin/bash

# MIT Intro to CS & Programming, assignment 1
# by tony baldwin

read -p "Please enter your last name: " last
read -p "Please enter your first name: " first
echo Hello, $first $last!

Then lisp:

#!/usr/bin/clisp

; MIT Intro to CS & Programming, assignment 1
; tony baldwin

(format t "Please enter your last name: ")
    (let ((last (read)))
(format t "Please enter your first name: ")
     (let  ((first (read)))
     (format t "~%Hello, ~A ~A!" first last)))

Then tcl:

#!/usr/bin/env tclsh8.5

# MIT Intro to CS & Programming, assignment 1
# by tony baldwin

puts "Please enter your last name: "
gets stdin last
puts "Please enter your first name: "
gets stdin first
puts "Hello, $first $last!"

Then perl:

#!/usr/bin/perl -w
use strict;
# MIT Intro to CS & Prog
# assignment PS0

print "Please enter your last name: ";
chomp($last = );
print "Please enter your first name: ";
chomp($first = );
print "Hello, $first $last!\n";

And now, some ruby:

#!/usr/bin/ruby

# MIT Intro to CS & Programming
# assignment ps0

puts "Please enter your last name: "
last = gets
puts "Please enter your first name: "
first = gets
puts "Hello, " + first.chomp + " " +  last.chomp + "!"

I do know that eventually I will have to attend the lectures, but this first assignment seemed rather straightforward.

I was motivated to start really learning my hackery when, a couple of nights ago, I started to look at lisp. ¬†Something in the sparse efficiency of lisp struck me as, well, striking. Beautiful, even. Weird…

./tony

Advertisements

Written by tonybaldwin

September 1, 2011 at 6:18 am

Thou unmuzzled, malmsey-nosed scullian! (randomness in php)

with 6 comments

Some time ago, I wrote fo0l and Shakes, being random Shakespearean insult generators in python, fo0l being a basic script, and Shakes being the same, dressed up with a tkinter gui.

Today, I translated fo0l to php, creating a webinterface for this lovely linguistic tool.

Try it out HERE, if thou hast the heart, thou frothy, shard-borne haggard!

What did I do?
Let’s look at fo0l, first:

#!/usr/bin/python
# Shakespearean insult generator

from random import randint

a = ("artless", "bawdy", "beslubbering", "bootless", "churlish", "cockered", "clouted", "craven", "currish", "dankish", "dissembling", "droning", "errant", "fawning", "fobbing", "froward", "frothy", "gleeking", "goatish", "gorbellied", "impertinent", "infectious", "jarring", "loggerheaded", "lumpish", "mammering", "mangled", "mewling", "paunchy", "pribbling", "puking", "puny", "qualling", "rank", "reeky", "roguish", "ruttish", "saucy", "spleeny", "spongy", "surly", "tottering", "unmuzzled", "vain", "venomed", "villainous", "warped", "wayward", "weedy", "yeasty", "cullionly", "fusty", "caluminous", "wimpled", "burly-boned", "misbegotten", "odiferous", "poisonous", "fishified", "Wart-necked") # 60 items

a1 = randint(0,59)
a2 = a[a1]

b = ("base-court", "bat-fowling", "beef-witted", "beetle-headed", "boil-brained", "clapper-clawed", "clay-brained", "common-kissing", "crook-pated", "dismal-dreaming", "dizzy-eyed", "doghearted", "dread-bolted", "earth-vexing", "elf-skinned", "fat-kidneyed", "fen-sucked", "flap-mouthed", "fly-bitten", "folly-fallen", "fool-born", "full-gorged", "guts-griping", "half-faced", "hasty-witted", "hedge-born", "hell-hated", "idle", "headed", "ill-breeding", "ill-nurtured", "knotty-pated", "milk-livered", "motley-minded", "onion-eyed", "plume-plucked", "pottle-deep", "pox-marked", "reeling-ripe", "rough-hewn", "rude-growing", "rump-fed", "shard-borne", "sheep-biting", "spur-galled", "swag-bellied", "tardy-gaited", "tickle-brained", "toad", "spotted", "unchin-snouted", "weather-bitten", " whoreson", "malmsey-nosed", "rampallian", "lily", "livered", "scurvy-valiant", "brazen-faced", "unwash'd", "bunch-back'd", "leaden-footed", "muddy-mettled", "pigeon-liver'd", "scale-sided") # 62 items

b1 = randint(0,61)
b2 = b[b1]

c = ("apple-john", "baggage", "barnacle", "bladder", "boar-pig", "bugbear", "bum-bailey", "canker-blossom", "clack-dish", "clotpole", "coxcomb", "codpiece", "death-token", "dewberry", "flap-dragon", "flax-wench", "flirt-gill", "foot-licker", "fustilarian", "giglet", "gudgeon", "haggard", "harpy", "hedge-pig", "horn-beast", "hugger-mugger", "joithead", "lewdster", "lout", "maggot-pie", "malt-worm", "mammet", "measle", "minnow", "miscreant", "moldwarp", "mumble-news", "nut-hook", "pigeon-egg", "pignut", "puttock", "pumpion", "ratsbane", "scut", "skainsmate", "strumpet", "varlot", "vassal", "whey-face", "wagtail", "knave", "blind-worm", "popinjay", "scullian", "jolt-head", " malcontent", "devil-monk", "toad", "rascal", "Basket-Cockle") # 60 items

c1 = randint(0,59)
c2 = c[c1]

print("Thou " + a2 + ", " + b2 + " " + c2 + "!")

Now, how did I translate that to php?


Enjoy!

./tony

Written by tonybaldwin

May 17, 2011 at 6:44 pm

Tritium WM

with 5 comments

tritium window manager

tritium window manager

Today I am playing with Tritium WM, a tabbed/tiling window manager with features similar to Ion3, only, entirely written in python (as opposed to C and Lua, like Ion3).
So far, it seems to work. It isn’t drawing java swing gui windows real well, sadly, and one of my main work apps uses java swing guis.
Other than that, it is snappy.
There really is NO documentation for this window manager…Nothing. I “spoke” with the author of this wm in #debian on freenode, and the only instructions he gave me were to look at the keys.py script for keybindings to figure out to navigate this wm, which, like ion3, is largely controlled from the keyboard. I may volunteer to write some documentation for this guy. I don’t know. There IS a man page, but it tells almost nothing.
In fact, this is the entire man page:

TRITIUM(1) TRITIUM(1)

NAME
tritium – a tabbed/tiling window manager for the X Window System

SYNOPSIS
tritium [options]

DESCRIPTION
tritium is a tabbed / tiling window manager geared towards keyboard users.

OPTIONS
-c, -use filename as the tritium config file.

AUTHOR
tritium was written by Mike O’Connor

June 1, 2007 TRITIUM(1)

From the keys.py file, so far I have figured out the following:

  • F1 gives a prompt to search for a man page.
  • F2 will call your default terminal
  • F3 will open command prompt.
  • Ctrl-F3 gives a python shell prompt (like the in idle).
  • Shift+F3 will run a command in a terminal.
  • F4 will open an ssh prompt to ssh to another machine.
  • Shift+F9 creates a new workspace.
  • F12 opens a program menu, much like dmenu in dwm or wmii.
  • Mod4 = the “Window Key”, between the Left-Alt (Mod1) and Left-Ctrl
  • Mod4+J to the next workspace, Mod4+K back a workspace.
  • Mod4+Tab changes focus between columns.
  • Mod4+L and Mod4+H move back and forth between windows or frames in a column.
  • Mod4+F(number) allows one to navigate between workspaces (ie. Mod4+F3 navigates to the third workspace).
  • Mod4+W will close a program.
  • Mod4+S slits a column horizontally
  • Mod4+Shift+S splits it vertically.
  • Mod4+(arrow key) will move the split between frames, thus resizing them. (ie. Mod4+(right arrow) will squeeze the column to the right over, and makes the left column larger, etc.).
  • Mod4+G and Win+; will navigate back and forth between columns, and across workspaces (as opposed to Mod4+Tab, which only changes focus between columns in the same workspace).
  • Mod4-Shift+J moves a window to the frame below, Mod4-Shift+K moves a the frame above. ¬†Mod4+Shift+L moves the windows to the frame to the right. Mod4-Shift+H moves it to the left.

I’ m probably missing something useful in there. ¬†I added this information to the man page on my machine, and sent the new file to Mike O’ Connor. ¬†Hopefully he’ll add that to the project’s file on sourceforge. The screenshot shows my updated man page.
I’ll probably play with tritium some more. ¬†The reasons for which I was curious about it were, first, that I had heard/seen it compared to Ion3 ¬†(which I had used and loved for some time before the author thereof, Tuomo V., went off the deep end over the entire Free Software community not bending over backwards to cater to his whims), and, that it is entirely written in python, which I can make far more sense of than C (my fu is not strong, alas, in C, although I did make sense of the Lua in ion3, even if I could never claim to know the language). ¬†For both of those same reasons, I’ll probably spend more time with Tritium.
UPDATE(2011.02.14): I keep playing with this wm. gdm did not want to recognize it automagically on my main box (the AMD64 box), so I had to make a tritium.desktop in the /etc/share/xsessions directory (copied one of the *.desktop files and edited it). On my old box, I had removed GDM. I only had to add it to the .xinitrc file I wrote in my /home on that box. I used gsetroot to add a wallpaper, and ran conky, much as I do in openbox, to show a clock and some system parameters (cpu/mem/swap, net up/net down / clock) in the bottom right corner. Looks cool, now.

From screenshots

Written by tonybaldwin

February 12, 2011 at 9:10 pm

Python v. Tcl/Tk: Denting & Tweeting

with one comment

python v. tcl/tkSo.  I have now made two little denter/tweeter programs (to send updates to twitter.com and identi.ca), one with Tcl/Tk, the other with Python. I figured a little comparison, perhaps, was in order.

If you look at them, of course, they look, well, just about the same.  Tkinter is, after all, analogous to Tk.
The Tcl/Tk program made it incredibly simple to display the response from the remote server, which I haven’t succeeded in doing with the python script, yet. ¬†Both rely on calling an external program (curl) to send updates, rather than relying on the languages’ built-in tools. ¬†I could probably work out HTTP POST in tcl rather painlessly. ¬†I did try to use python’s urllib to post, unfruitfully, and resorted back to calling curl.
the links: iDenTickles (tcl/tk) / iDenTweetyPie (python)
the code for both programs is available at the above wiki links

The Tcl/Tk program, which has the added feature of displaying the server response, has only 47 lines of code, 245 words, 1844 characters.  It took me less than an hour to write it.

The Python program, however, which does precisely the same exact thing as the tcl/tk program, without displaying the server response, has 104 lines of code, 564 words, and 4073 characters.  It took me the better part of a day to write it.  Oh, but the python program tells you if your update is too long.
One must ask oneself, of course, is this a testament to the power and simplicity of tcl/tk?  Or, is it simply an indication of my lack of skill with python?
I can’t answer that defnitively, but, to me, it really looks like tcl/tk is a bit more efficient. ¬†Admittedly, I’m not a very skilled programmer at all, in truth. ¬†Timewise, of course, I have been writing tcl/tk for a couple of years, and only just now delving into python. As such, I was able to throw the tcl/tk program together quickly, while, my efforts to “translate” my tcl/tk program into python required a bit of research on the syntax for writing tkinter guis, and other elements. ¬†It just really looks to me as though Python/Tkinter takes a lot more code to do the same thing. ¬†I really have drawn that conclusion. ¬†Especially building a gui, it seems, is more cumbersome with tkinter than with simple, good old tcl/tk. ¬†I know there are other means of building a gui with python (wxwidgets, pygtk, pyqt, etc.), but I wanted to try the one most similar to that with which I am already familiar, and, I believe it is a fairer comparison when using a similar gui ToolKit.
At this juncture, I do have to say, I feel a great loyalty and deep affinity for tcl/tk. ¬†I don’t understand why it isn’t in wider use, frankly. ¬†It is an incredibly powerful language, used for a vast array of purposes, and, in my opinion, is probably the easiest programming language to learn (of course, I haven’t tried them all), especially for a beginning programmer. ¬†One can be up and running, creating useful programs in a relatively short time. ¬†I also feel the need to give kudoz to the tcl/tk community and the tcl.tk wiki, which is replete tons of example code, detailed explanations, and great resources for learning how to program in tcl/tk. ¬†The tcl-ers that hang out at #tcl on irc.freenode.net, additinoally, are extremely helpful, and patient. ¬†They won’t hold your hand, but they’ll tolerate a newbie, and point them in the right direction, without any snobbishness or derision.
I can’t say the same for my experiences with pythonistas. ¬†Their irc channel was a little less friendly, imho. ¬†Maybe I just caught them on a bad day, or maybe I was having a bad day. ¬†After all, Pythonistas are known for having a sense of humor. ¬†Admittedly, I was frustrated when I finally went to their channel for a bit of support, and frustrated, whiny n00b is no fun to play with, any way. ¬†Moreover, the python community does have a lot of documentation available online. Nonetheless, to me, it seems that it is written for other programmers, not for the uninitiated, so, is not so easily read as much of the tcl/tk resources. ¬†Their sample code is not well explained, where someone new to programming can really make sense of it. This may also be a function of time, since tcl/tk has been around a bit longer than python.

I do want to make it very clear: I’m really not here to pick on python. ¬†I know that it’s a powerful language with a great many uses, and a favorite of a great many real hackers who know a lot more about programming than I do. ¬†I will continue to learn to write it, and believe it will serve me quite well for various purposes, and I believe I will continue to have fun learning it. ¬†But, I think I might continue to point out how tcl/tk is much easier and seemingly efficient, too…

Written by tonybaldwin

March 23, 2010 at 3:30 am

Python v. Tcl/Tk (denting & tweeting)

leave a comment »

python v. tcl/tkSo.  I have now made two little denter/tweeter programs (to send updates to twitter.com and identi.ca), one with Tcl/Tk, the other with Python. I figured a little comparison, perhaps, was in order.

If you look at them, of course, they look, well, just about the same.  Tkinter is, after all, analogous to Tk.
The Tcl/Tk program made it incredibly simple to display the response from the remote server, which I haven’t succeeded in doing with the python script, yet. ¬†Both rely on calling an external program (curl) to send updates, rather than relying on the languages’ built-in tools. ¬†I could probably work out HTTP POST in tcl rather painlessly. ¬†I did try to use python’s urllib to post, unfruitfully, and resorted back to calling curl.
the links: iDenTickles (tcl/tk) / iDenTweetyPie (python)
the code for both programs is available at the above wiki links

The Tcl/Tk program, which has the added feature of displaying the server response, has only 47 lines of code, 245 words, 1844 characters.  It took me less than an hour to write it.

The Python program, however, which does precisely the same exact thing as the tcl/tk program, without displaying the server response, has 104 lines of code, 564 words, and 4073 characters. ¬†It took me the better part of a day to write it. Oh, but the python program does tell you if your post is too long, something I neglect to program into the tcl/tk script. That’s a bonus.
One must ask oneself, of course, is this a testament to the power and simplicity of tcl/tk?  Or, is it simply an indication of my lack of skill with python?
I can’t answer that defnitively, but, to me, it really looks like tcl/tk is a bit more efficient. ¬†Admittedly, I’m not a very skilled programmer at all, in truth. ¬†Timewise, of course, I have been writing tcl/tk for a couple of years, and only just now delving into python. As such, I was able to throw the tcl/tk program together quickly, while, my efforts to “translate” my tcl/tk program into python required a bit of research on the syntax for writing tkinter guis, and other elements. ¬†It just really looks to me as though Python/Tkinter takes a lot more code to do the same thing. ¬†I really have drawn that conclusion. ¬†Especially building a gui, it seems, is more cumbersome with tkinter than with simple, good old tcl/tk. ¬†I know there are other means of building a gui with python (wxwidgets, pygtk, pyqt, etc.), but I wanted to try the one most similar to that with which I am already familiar, and, I believe it is a fairer comparison when using a similar gui ToolKit.
At this juncture, I do have to say, I feel a great loyalty and deep affinity for tcl/tk. ¬†I don’t understand why it isn’t in wider use, frankly. ¬†It is an incredibly powerful language, used for a vast array of purposes, and, in my opinion, is probably the easiest programming language to learn (of course, I haven’t tried them all), especially for a beginning programmer. ¬†One can be up and running, creating useful programs in a relatively short time. ¬†I also feel the need to give kudoz to the tcl/tk community and the tcl.tk wiki, which is replete tons of example code, detailed explanations, and great resources for learning how to program in tcl/tk. ¬†The tcl-ers that hang out at #tcl on irc.freenode.net, additinoally, are extremely helpful, and patient. ¬†They won’t hold your hand, but they’ll tolerate a newbie, and point them in the right direction, without any snobbishness or derision.
I can’t say the same for my experiences with pythonistas. ¬†Their irc channel was a little less friendly, imho. ¬†Maybe I just caught them on a bad day, or maybe I was having a bad day. ¬†After all, Pythonistas are known for having a sense of humor. ¬†Admittedly, I was frustrated when I finally went to their channel for a bit of support, and frustrated, whiny n00b is no fun to play with, any way. ¬†Moreover, the python community does have a lot of documentation available online. Nonetheless, to me, it seems that it is written for other programmers, not for the uninitiated, so, is not so easily read as much of the tcl/tk resources. ¬†Their sample code is not well explained, where someone new to programming can really make sense of it. This may also be a function of time, since tcl/tk has been around a bit longer than python.
I do want to make it very clear: I’m really not here to pick on python. ¬†I know that it’s a powerful language with a great many uses, and a favorite of a great many real hackers who know a lot more about programming than I do. ¬†I will continue to learn to write it, and believe it will serve me quite well for various purposes, and I believe I will continue to have fun learning it. ¬†But, I think I might continue to point out how tcl/tk is much easier and seemingly efficient, too…

Written by tonybaldwin

March 22, 2010 at 8:28 pm

iDenTweetyPie

leave a comment »

iDenTweetyPie

iDenTweetyPie: python/tkinter tweeter/denter

I hacked up a little more python/tkinter silliness.

iDenTweetyPie sends a dent/tweet(update) to twitter or identi.ca.

That’s all, really.

I have no plans to make a full-blown client for either site, just a quick-n-dirty updating tool. I would like, however, to figure out the pubsubhub thingy to send buzzes to google/buzz.

That would take some additionaly work, since, at the moment, iDenTweetyPie tells you that you talk too much and refuses to proceed if your update is longer than 160 characters, which, of course, isn’t necessary for buzz, since buzz tolerates longer updates.

I might work on that. ¬†I don’t know. ¬†This was just a little exercise, really.

I had already written a similar little program in tcl/tk, iDenTickle, which initially only did dents, but I have updated that one to send tweets, too.

my dents :  my tweets

./tony

Written by tonybaldwin

March 22, 2010 at 7:02 am

PyGrill: Throw some FreshMeat on the Grill

leave a comment »

PyGrill:  Throw some FreshMeat on the Grill

PyGrill: Throw some FreshMeat on the Grill

Well…I’ve been translating like mad for weeks and weeks on end, so, after deliverying a project in the weehours last night, and, well, what with spring in the air,¬†I decided to take a bit of time off from translating today, and do that which seemed most appropriate:
engage in a bit of hackery.

Well, you know, spring/creation, being creative…

I’ve decided to try and hack up a graphical user interface (gui) for ESR’s freshmeat-submit.
Mr. Raymond’s freshmeat-submit is a python script for submitting projects to Freshmeat.net, the popular software site (on which I have several projects of my own, in tcl/tk, not python).
All I’m trying to do is make a gui to interact with his script, and in python/tkinter, as a learning experience, so, all the really nifty, hardcore hackery involved in this belongs to Mr. Raymond.

It’s here: PyGrill: Throw some FreshMeat on the Grill.

It’s a pretty n00bish, and rather inefficient bit of code, at this juncture, but I have plans. ¬†This is the very beginnings of a beginning. ¬†I haven’t even got it firing off the submit script, yet, but rather just spitting out the command line arguments to do so, by clicking marinade. ¬†Eventually, “marinade” will save the xml output for future use, while “Grill it!” will actually fire off Mr. Raymond’s script and submit the project to Freshmeat. ¬†But, I’m done working on it for today, because, my kid is home from school, it’s Friday, and it’s too darned nice out to stay in here and hack. ¬†I think we’re going to go for a bike ride.

I wouldn’t even call this a beta release. It’s not even a release, really…it’s just a preview of what will be coming shortly. ¬†I almost feel silly even posting about it at so early a stage, but, posting I am.
I explain my future plans, and display the rather inefficient, and quite incomplete code I’ve so far generated at on the wiki page I’ve created for the project. I’m sure I’ll have something better worked out within the week, and be posting about it again. Incidentally, this is only my second attempt at working with python/tkinter, and still one of my very earliest attempts with python, with or without gui elements.
And, no, I haven’t given up on good old Tcl/TK (with which I could have whipped out the whole, complete program in less time than I’ve spent on this feeble attempt). I’m just broadening my horizons.
After all, I do hope some day to be be a real hacker.

My goals in working on this project are:

  1. to better understand both python and tkinter;
  2. to learn how to generate xml with python, and;
  3. to learn how to play nice with the xml-rpc protocol, in order to;
  4. create a new blog client that will crosspost to wordpress, blogger, liverjournal, insanejournal, and dreamwidth.
  5. waste time hacking when I could be cleaning house, marketing, doing my taxes, etc.

Written by tonybaldwin

March 19, 2010 at 1:10 pm