Wednesday, February 3, 2010
Spam Musubi
It's February in Skokieland, and a fresh snowfall has come, adding yet another layer of beautiful white snow. Santa has long since come and gone, leaving a nice stocking full of coal under the Hanuka bush to keep Mike warm. It would be lovely to be somewhere warm, near the ocean, and sit and sip umbrella drinks after parasailing, but that is not in the cards for an unemployed layabout such as I. Still, the most wonderful Miss Lizzy has got a nice bunch of coconuts and a plate full of Spam musubi, made by yours truly, Mike. I call it my tropical vacation lunch. I had to roll my own Spam musubi because the 7-Eleven's of Skokieland do not carry it. The Illinois Tech 7-Eleven did (and maybe still does) carry decent pitas stuffed with felafel or chicken shawarma, and samosas with wonderful green coriander chutney but that is literally on the other side of town, over an hour away on the el. The local joints do sell mediocre po' boys featuring soft bread rolls, yellow process cheese and something like Oscar Mayer bologna sausage, but that's just not working for me today.
Yes, I have eaten these alleged po' boys. It's one of the night school students quick meal units. We'll discuss the frozen burrito fetish later -- you need to microwave those, and often there's just not enough time or clean microwaves available.
I never thought I would ever be eating spam, much less cooking it with teriyaki sauce, but here I am. I blame it on winter, and the general lack of good tomatoes.
Of course, Spam musubi ain't half bad, especially with some furikake added. Since Miss Lizzy purchased the jumbo tin of Spam, I only used half the can so far. I can see more Spam teriyaki in my near future.
Monday, February 1, 2010
Python 3000, or Confuse-A-Coder
(Photo by:Matthew W. Jackson)
So here I am, newbie pythonista. Writing code. On my computer. On other people's computers, using Portable Python. Doing new, geeky things.
OK, to be honest, How To Think Like a Computer Scientist is more my speed, and I have worked through all of that. (Alas, I still think like Mike.) I even did the entire MIT 6.00 class, (which now has video!) although I do not yet grok the dynamic programming solution to the knapsack problem. I did study electrical engineering, and even earned a BSEE, but math/CS types study some different stuff, I think. Or perhaps I was dozing through class? Either could be true, as far as I can tell.
Things change over time. Some changes are a good thing. Isn't there anything you would like to change in your life? Wouldn't would you like a few do-overs? I sure could use a few. The Python guys seem to think so, too. Python is transitioning to Python 3, and things are bit different. Some things have been really done over. Here are some differences that I have found, explained simply.
Print is now a function and not a keyword.
This means you have to say:
print() instead of :
You can no longer say:
print 'spam spam spam',
to suppress the newline. You now must say:
print('spam spam spam', sep="")Yes, I know, this is gripping material. Apparently someone, somewhere wants to be able to redefine print to do something useful, like making spam musubi topped with foie gras, or confusing a cat. This is not possible with print defined as a keyword, so get used to the parentheses. A good way to do that is to put the following line near the top of all your Python 2 code:
from __future__ import print_function
This will enforce the Python 3 syntax, and you won't have to change your code if you ping-pong between Python 2 and Python 3, as I do.Integer division can now return a float.
Before Python acted like, well, the C programming language that I remember learning at night school. If you divided two integers, your result was always an integer. So 5/2 would return 2
(Note that the sentence lacks a period because 2 is an integer, while 2. is a float. Adding a period is confusing.)
In Python 3, if you divide two ints, you can get a float. In Python 3, 5/2 will return 2.5
(Again, I left the period off on purpose.) This is the correct answer, after all. I think Guido van Rossum changed his mind here. No biggie, unless you do as much crap ass integer calculation that comes from working Project Euler problems.
To get used to this new behavior, put the following line near the top of all your Python 2 code:
from __future__ import division
If you want to work on Project Euler problems, seek professional help.
Strings have changed.
I've just started working with this. According to Guido,
Python 3.0 uses the concepts of text and (binary) data instead of Unicode strings and 8-bit strings. All text is Unicode; however encoded Unicode is represented as binary data.
I'm still sorting this out so I can't help you much here. To enforce Python 3 strings text, put the following line near the top of all your Python 2 code:
from __future__ import unicode_literalsMy current project, a Python synthesizer noisemaker, pukes and errors out if I use this line. Works fine in Python 2.6.4 without it. I seem to be confused about using the wave and struct modules. At least I think that's the problem. I'm not using much else in the way of strings text in the program. I plan to sort this out down the road.
A lot of functions that used to return a list now return an object that is not a list.
Guido writes:
Views And Iterators Instead Of Lists
Some well-known APIs no longer return lists:
- dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient).
- Also, the dict.iterkeys(), dict.iteritems() and dict.itervalues() methods are no longer supported.
- map() and filter() return iterators. If you really need a list, a quick fix is e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).
- range() now behaves like xrange() used to behave, except it works with values of arbitrary size. The latter no longer exists.
- zip() now returns an iterator.
I grok some of this, and I have run into problems with these changes in the wild. Especially when plagiarizing copying using other people's code. You can't make lists anymore by saying things like:
s = range(1000)
In Python3, s is not a list. This idiom is real popular in the wild. Remember this when you use Google to find someone else's code to solve your problem. It doesn't work any more. You must explicitly create the list:
s = list(range(1000))
if you really want s to be a list.
I tend to read in numeric data from text files, and it winds up as strings in a list. Being the schemer that I am (more later, maybe, on the joys and sorrows of Lisp) I would use map() to convert all the strings in a list to integers. Here's a line of code that I wrote on 13 Mar 2009:
temp1 = map(int,data.split())
For Python 3, you would have to say:
temp1 = list(map(int,data.split()))
A better solution is to stop writing Lisp code in Python and use a list comprehension, which works in both Python 2 and Python 3:
A better solution is to stop writing Lisp code in Python and use a list comprehension, which works in both Python 2 and Python 3:
temp1 = [int(x) for x in data.split()]
which is much more pythonic, and cleaner once you start comprehending list comprehensions.
There's an automagic script, 2to3, which I have never used because I feel that I am still learning Python, and I need to solve this kind of problem using my finely tuned mental machine. Or all by myself, anyway. It may be useful.
There is a -3 switch that will warn you about things that will not work in Python 3. I am starting to use this switch, but I think that some of the results are so not my problem to fix:
mikey@hatshepsut:~/workspace/mysynth/src$ python -3 mysynth
/usr/lib/python2.6/site.py:1: DeprecationWarning: The 'new' module has been removed in Python 3.0; use the 'types' module instead.
"""Append module search paths for third-party packages to sys.path.
/usr/lib/python2.6/site.py:1: DeprecationWarning: The 'new' module has been removed in Python 3.0; use the 'types' module instead.
"""Append module search paths for third-party packages to sys.path.
Another problem is that many useful libraries are not yet available for Python 3. No numpy yet, and of course nothing that depends on numpy. I think this stuff will exist in Python 3, but not yet. The best thing do right now is to stick with Python 2 if you need something that doesn't exist yet in Python 3, but use the from __future__ import statements. (I'm working on it...). Otherwise, start using Python 3. Unless you're actually paid to write Python code, and your boss says No Python 3 for you!
At least, thats what I'm doing for now.
Cheers!
References:
Whats New in Python 3.0 by Guido van Rossum
A clear explanation of list comprehensions . by OlliHow To Think Like a Computer Scientist (Uses Python 2.)
MIT 6.00 Introduction to Computer Science and Programming (Uses Python 2.)
Dive into Python 3 by Mark Pilgrim (A challenging read for me.)
Sunday, January 31, 2010
In the beginning...
I have another web site, called Mike's Cafe. On this site I stated "this is not a blog, it just looks like one." For some reason this was important to me. I'm not sure why. I was just about to add some more content to the site when I realized that it not only looked like a blog, but that I was treating it like a blog and even thinking of it like a blog. I didn't have anything major to add to the site, I just wanted to gas on a bit. Stop kidding yourself Mike, that's what a blog is all about. So that's why Mike's Cafe II is here, so I can blog.
(Yes, I have installed the libcdio-paranoia-dev package... has something changed in the last five years?)
Although I am currently running on Vista, my normal desktop is Linux. I currently run Ubuntu, although I've been running Linux as a desktop user since the 1990's. I have used Slackware, Red hat, Suse, Debian and Arch. I like Linux, but something is always broken. (Hey don't get upset, it's just the way things are in the Open Source world.)
(There's been a short break while I rebooted into Ubuntu. Winbuilder is finished making the VistaPE iso, so I can now go home.)
I can get most things to work. I can listen to internet radio, create word docs, spreadsheet, and email to my heart's content. Using non-free "binary blobs" I can use watch Hulu, which means I now watch more TV on my computer than on my TV, and Richard Stallman would be very disappointed with me.
Enough of that. Previously, I ripped my audio disks using EAC (on windows) and grip (on Linux). I don't run windows much anymore, so I used grip. I like grip; it was quite flexible and it used cdparanoia to rip the disks. Then grip disappeared from the Ubuntu repositories because the developer hasn't worked on it for about five years, which could be a problem.
If you build grip from the source code you see some scary stuff. After running ./configure, things chug along and create a makefile, but things are not OK:
checking cdda_interface.h usability... yes
checking cdda_interface.h presence... yes
checking for cdda_interface.h... yes
checking cdda_paranoia.h usability... no
checking cdda_paranoia.h presence... yes
configure: WARNING: cdda_paranoia.h: present but cannot be compiled
configure: WARNING: cdda_paranoia.h: check for missing prerequisite headers?
configure: WARNING: cdda_paranoia.h: see the Autoconf documentation
configure: WARNING: cdda_paranoia.h: section "Present But Cannot Be Compiled"
configure: WARNING: cdda_paranoia.h: proceeding with the preprocessor's result
configure: WARNING: cdda_paranoia.h: in the future, the compiler will take precedence
configure: WARNING: ## --------------------------------------------------------------- ##
configure: WARNING: ## Report this to http://sf.net/tracker/?group_id=3714&atid=103714 ##
configure: WARNING: ## --------------------------------------------------------------- ##
checking for cdda_paranoia.h... yes
checking cdda/cdda_interface.h usability... no
checking cdda/cdda_interface.h presence... no
checking for cdda/cdda_interface.h... no
checking cdda/cdda_paranoia.h usability... no
checking cdda/cdda_paranoia.h presence... no
checking for cdda/cdda_paranoia.h... no
Yikes! No paranoia is available here for ready money. Grip doesn't look so great any more. I was going to report the error, but someone else beat me to the punch. It seems the developer has been AWOL since 2005. (He and his wife do have a cool food blog, though.)
(Yes, I have installed the libcdio-paranoia-dev package... has something changed in the last five years?)
I could use cdparanoia to rip my cds, and then invoke aoTuV or whatever to shrink them, but what a pain in the neck. Or maybe an opportunity to do some scripting?
Lucky me, someone else already did the heavy scripting and wrote rubyripper. Not yet in the Ubuntu repository, but pretty easy to install. There seem to be some .debs out there, but I decided to install from source. Instructions exist, but of course the instructions are a little off for my system, which seems be the norm.
Here's how I installed rubyripper on Ubuntu 9.10 karmic amd64:
1. install this lot:
sudo apt-get install cd-discid cdparanoia flac lame mp3gain normalize-audio ruby-gnome2 ruby vorbisgain
2. fix normalize:
Make a symlink from /usr/bin/normalize-audio to /usr/local/normalize & call it a day.
ln -s /usr/bin/normalize-audio /usr/local/normalize
You need to do something, because the Debian folks have changed the name of normalize to normalize-audio. If you're really interested, here's the reason why they changed the name, from the package's README.Debian file:
3. get/install wavegain:
Get the wavegain source code, (you can get it here: http://www.rarewares.org/others.php), compile it for Debian, install somewhere in you search path. Old school as I am, I put the executable in /usr/local/bin.
4. get/install rubyripper source code:
http://code.google.com/p/rubyripper/
I installed the program like this:
./configure --enable--lang-all --enable-gtk2 --enable-cli --prefix=/usr/local
sudo make install
5. If you dislike warnings appearing in your terminal windows, use this duct tape solution:
On karmic some programs cause an warning or error:
Gtk-Message **: Failed to load module "pk-gtk-module": (etc)
pk-gtk-module is a "GTK+ module for font installation", "This key determines if applications should be able to prompt for fonts".
Turn it off for now, it's not working. A temporary fix is changing the key in gconf-editor
/apps/gnome_settings_daemon/gtk-modules/pk-gtk-module to false.
This seems to be an Ubuntu problem, and they know about it. It will probably be cleaned up down the road.
Here's some references:
rubyripper
A secure audiodisc ripper for Linux and OS X
http://code.google.com/p/rubyripper/
HOWTO: Install Rubyripper on Ubuntu
http://ubuntuforums.org/showthread.php?t=799621
Rubyripper article on Hydrogen Audio
http://wiki.hydrogenaudio.org/index.php?title=Rubyripper
Here's how I installed rubyripper on Ubuntu 9.10 karmic amd64:
1. install this lot:
sudo apt-get install cd-discid cdparanoia flac lame mp3gain normalize-audio ruby-gnome2 ruby vorbisgain
2. fix normalize:
Make a symlink from /usr/bin/normalize-audio to /usr/local/normalize & call it a day.
ln -s /usr/bin/normalize-audio /usr/local/normalize
You need to do something, because the Debian folks have changed the name of normalize to normalize-audio. If you're really interested, here's the reason why they changed the name, from the package's README.Debian file:
2004-06-26: The package and binary names had to be changed due to namespace conflicts. The binary normalize was renamed to normalize-audio so that it keeps orthogonal with its frontend names (normalize-ogg and normalize-mp3) while not using a generic name anymore.
See more information about it here: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=250390You could hack the source code, or wait for Debian or Ubuntu to adopt the package & fix this, or become a Debian or Ubuntu developer and adopt the package & fix this or just make the symlink. You may have trouble with mono, but then, who likes mono?
3. get/install wavegain:
Get the wavegain source code, (you can get it here: http://www.rarewares.org/others.php), compile it for Debian, install somewhere in you search path. Old school as I am, I put the executable in /usr/local/bin.
4. get/install rubyripper source code:
http://code.google.com/p/rubyripper/
I installed the program like this:
./configure --enable--lang-all --enable-gtk2 --enable-cli --prefix=/usr/local
sudo make install
5. If you dislike warnings appearing in your terminal windows, use this duct tape solution:
On karmic some programs cause an warning or error:
Gtk-Message **: Failed to load module "pk-gtk-module": (etc)
pk-gtk-module is a "GTK+ module for font installation", "This key determines if applications should be able to prompt for fonts".
Turn it off for now, it's not working. A temporary fix is changing the key in gconf-editor
/apps/gnome_settings_daemon/gtk-modules/pk-gtk-module to false.
This seems to be an Ubuntu problem, and they know about it. It will probably be cleaned up down the road.
Here's some references:
rubyripper
A secure audiodisc ripper for Linux and OS X
http://code.google.com/p/rubyripper/
HOWTO: Install Rubyripper on Ubuntu
http://ubuntuforums.org/showthread.php?t=799621
Rubyripper article on Hydrogen Audio
http://wiki.hydrogenaudio.org/index.php?title=Rubyripper
Subscribe to:
Posts (Atom)


