Wednesday, November 18, 2009

Lunch in Boise: Sweetwater's Tropic Zone

I know...I don't usually review restaurants, but it's my blog and I figured, what the heck.

I probably wouldn't have eaten here, but my friend @ryancoates said he'd heard it was good. Actually, I was the one who invited him out, which is probably a surprise to most of you who know me. I don't "get out" much for lunch, but I had something specific I wanted to talk with Ryan about (which is another story...nothing bad, though).

I looked up the address online and according to Google Maps, it's an 18 minute walk from where I work. Beats looking for parking downtown, which I detest, so at about 11:40 a.m., I set out to hoof it to Sweetwater's. I have to say that the smell of barbecue as I walked past The Cottonwood Grille on 9th was just intoxicating, but I pressed on (I love the carbon monoxide smell of charcoal briquettes).

I encountered a few of those new "timed" electric crosswalk signals. Instead of just holding up a blinking red hand to indicate the traffic light is about to change against you, it presents a countdown from about 18 seconds (I think) down to zero, letting you know if you should walk or run or not attempt to cross the street. A minor distraction, but like I said, I don't get out much.

I love downtown Boise. Whenever I have a reason to, I really enjoy the area on foot. At least during a weekday and during daylight, it's kind of peaceful, old fashioned in a brickwork sort of way, and the way every city's downtown should be. If I had the whole afternoon to kill, I'd slow up my pace and just explore, but not today. I was on a mission.

I arrived at the restaurant just a minute or two before twelve and could see Ryan approaching. Our timing was right in synch so neither of us had to wait. The air had a bit of a snap to it when I was walking, but by the time I got to 205 N. 10th, I was warm enough. Ryan and I said our hellos and then followed the directions to enter via the lobby of the entrance next door.

Sweetwater's was very lightly attended, which is good for service but not necessarily good for business. We were seated right away and attended to in every little detail. We were given the lunch menu, plus a beverage and some other menu (I didn't look at it) to review. I'd already selected the Curried Avocado & Jasmine Rice salad from the online menu, and Ryan had some sort of blackened chicken (sorry, I didn't listen very well when he was ordering).

Service was swift, but I probably wouldn't have noticed that much, since Ryan and I were already deep in conversation (64-bit hardware at this point, but moving into Artificial Intelligence). My salad was excellent and a big plus, the portion was just right. I usually either come away from lunch stuffed or starving, but this sat with me just right. Our wait staff (and probably the manager) provided us with a bottle of hot sauce euphemistically called "The Inferno" and I tried a bit on my salad. I guess either the hot sensing part of my tongue died with age, or a lifetime of eating hot foods has made me immune, but I thought it rather tasty.

I only had water to drink, so can't testify to any of the beers they serve. I didn't have to wait, once I put out my credit card, to have it run and have the receipt for signature returned to me (waiting to settle the bill is a pet peeve of mine). Lunch customer activity had only marginally picked up by the time I was ready to pay the bill, and service was still lightning quick. It would have been interesting to have eaten there during a rush to see if this would have been affected at all.

I have to give Sweetwater's Tropic Zone top marks. Food was exotic without being overpowering and service was swift and straightforward. Exactly how I want it when I lunching with a friend and conversation turns to politics (both American and UK), religion, and driving on ice.

Sweetwater's Tropic Zone is located in downtown Boise at 205 N. 10th Street, between Bannock and Idaho. They're open Monday through Saturday, starting at 11:30 a.m. I couldn't find their closing time online. You can get to their website at http://www.sweetwaterstropiczone.com and follow them on twitter at @SweetwatersBOI.

Share/Bookmark

Sunday, November 1, 2009

Linux in a Nutshell, 6th Edition

Authors: Ellen Siever, Stephen Figgins, Robert love and Arnold Robbins
Format: Paperback, 942 pages
Publisher: O'Reilly Media; 6th edition (September 30, 2009)
ISBN-10: 0596154488
ISBN-13: 978-0596154486

I was going to swear that, with each successive edition of this book, the page count got larger and larger, but I checked, and it's stayed almost the same over the last three editions. O'Reilly says that the 4th edition was 944 pages long, but the 5th and 6th editions (the 6th being the latest) are both 942 pages. When I got my review copy in the mail and opened the box, the book seemed larger than I expected for some reason.

But if the book isn't getting larger and thus, filled with more recent and updated information about Linux, why publish subsequent editions? For that matter, can you call a book that's approaching 1000 pages a "Desktop Quick Reference?" To try and answer my first question, I took a look at the back cover and saw, "This updated edition offers a tighter focus on Linux system essentials, as well as more coverage of new capabilities..." Yes, the Linux kernel continues to evolve and thus, what you need to know about Linux system administration continues to change as well. While most of the common shell commands won't change, there are new ones that you'll need to know (and if you've bought this book or are intending to, you belong to that class of person who needs to know). Also, virtualization is huge these days and you can now manage Linux servers via Xen and VMware.

Linux in a Nutshell is considered a classic by anyone's standards, so it's expected to review well. In fact, the prior editions have reviewed extremely well so, in this case, turning in a bad review on the latest edition would mean that the authors and publisher must have completely rewritten the book and done a poor job of it. Fortunately, that's not the case here. Linux in a Nutshell, 6th Edition is a worthy successor to those editions that have come before it. You have three main reasons to buy it. First, you have the reason I've already mentioned; keeping up to date on the latest changes to Linux. Second, you've completely worn out your older copy of the book and need something that isn't hanging in rags. Third, you are new to Linux and need to buy a reference guide for the first time.

In a literary sense, some books are considered classics and almost legends. That may be a bit more rare in technical circles (is there such as thing as the equivalent of A Tale of Two Cites or To Kill a Mockingbird when it comes to Linux?), but it's not entirely unheard of. If the world of Linux has such a book, it's probably Linux in a Nutshell.

All that said, you will be disappointed in this book if you don't know what to expect, and I've read comments from people who were quite disappointed with this book. Hence writing reviews. Here's what not to expect. This is not a tutorial. It will not explain, step-by-step how to use Linux, particularly for the home or office desktop user (and with Ubuntu 9.10 (Karmic Koala) having just been released, a large number of people are thinking of the Linux desktop). If you try to read this book cover-to-cover, you won't find it amusing or entertaining. It would be like trying to read an encyclopedia set or dictionary from A to Z. Yes, it would be very informative, but organized in a very rigid manner, and it will not read like a narrative.

This book is a reference in the same manner as the aforementioned encyclopedia and dictionary. You look up only what you need to know. It presupposes that the reader have some familiarity with Linux, at least enough to understand what information they require and how to use the book to find it. The list of shell commands runs from page 33 to page 503, for instance and is in alphabetical order. On the other hand, chapters such as The Bash Shell do contain narrative and explanatory components, so you do get more than raw shell commands and arguments. Still, it is not a good book as your first exposure to Linux documentation or as an introduction to topics like the bash shell, the vi text editor, or the gawk programming language. It would be like trying to learn English by reading a dictionary. A dictionary is better utilized once you know at least the basics of the language and want to pick up something specific.

Both Git and Subversion version control system commands are well covered in the later chapters, but as I said, you'd better know the basics first, rather than expect to learn them for the first time here. As promised, Virtualization Command-Line Tools sits in the last chapter of the book, giving you fingertip access to the commands and options involving KVM, VMware, and Xen virtualization tools.

This book's 6th Edition is indeed a worthy inheritor to those that have come before it and carries on the tradition of providing A Desktop Quick Reference for Linux shell commands and utilities. If you've owned a prior edition, this update is now available as a replacement. If you are learning Linux administration for the first time and have the basics down, you're ready to buy this edition as your first experience to Linux in a Nutshell.

Share/Bookmark

Tuesday, October 27, 2009

Fahrenheit 451 Revisited: Do Firemen Still Dream of Igniting Books?

Fahrenheit 451"One last thing," said Beatty. "At least once in his career, every fireman gets an itch. What do the books say, he wonders. Oh, to scratch that itch, eh? Well, Montag, take my word for it, I've had to read a few in my time, to know what I was about, and the books say nothing! Nothing you can teach or believe. They're about nonexistent people, figments of imagination, if they're fiction. And if they're nonfiction, it's worse, one professor calling another an idiot, one philosopher screaming down another's gullet. All of them running about, putting out the stars and extinguishing the sun. You come away lost." -From Fahrenheit 451

Last July, after reading a notice of an American classic to be re-published as a graphic novel, I wrote a bit of commentary on Ray Bradbury's novel Fahrenheit 451 called Fahrenheit 451: "Digital" is the Fire in Which we Burn. I was lamenting the day when books were a serious form of entertainment and source of information, even during the advent of the age of television. Then I chanced upon Bradbury's novel at my local library and found myself wondering if I'd ever read it. I assumed I had, but I couldn't really remember. Well, there was one way to fix the problem.

This work was first published as a novel in 1953, the year before I was born. More than half a century later, this work still stands on solid legs, but perhaps not quite the legs that Bradbury first built for his book. The book isn't so much about censorship or even about television replacing books and other print media, as it is about the dumbing down of humanity. If the world of ideas and debate make people unhappy, take them away and replace them with the equivalent of Prozac for the masses. Let the government do the worrying. You don't have to concern yourself about a thing.

Fifty-six years later, the government isn't burning books, but there's a question as to whether or not they've been marginalized. While Bradbury unsuccessfully predicted interactive television in the home, with screens on all four walls (he did seem to capture flat-screened TVs, though), he couldn't have possibly imagined Cable and Satellite TV, iPods, eBooks, blogging, twitter, texting, YouTube, gaming, and the Internet (and on and on). We haven't replaced print with TV, we've replaced few information sources with many, and with sources that produce content in all its myriad forms, at a rate that accelerates faster than the dizzying velocities of the cars and jets of Montag's (the book's protagonist) world.

Whenever I go to the gym to work out, music is blaring, a dozen TV sets are tuned to a dozen TV channels, almost everyone working out is listening to their iPod, and in the midst of all that, some people are either talking or texting on their mobiles. I go to the gym to try and coax my middle-aged body into continued activity for an hour or so a day, and people can't seem to unplug for even that brief a period of time. If we aren't constantly bombarded with information and entertainment every waking second of the day, what would become of us? Would we actually have to suffer alone with our own thoughts? Would we actually have to relate to other human beings? Is this the "real" Fahrenheit 451?

Print isn't dead, it's just been transformed. This blog is one incarnation...one descendant of the magazine article or the newspaper editorial. The difference is that anyone can blog for little or not cost and for some, with little or no effort. When I want to write a book (yes, I still write real books), I have to write and submit a proposal to a publisher through my agent. The publishing staff evaluates my proposal to determine if it's worth their time and money to turn my idea into a book. That is, they have to decide if there are enough people who'll pay good money to buy what I want to write. Even if the proposal is approved, the book turns out to be a collaboration between me and the various editors I work with, so it's not just all my bright ideas on paper or in eBook format.

With blogging, anyone can write anything at anytime. Of course, no audience is guaranteed, but anyone might surf onto your blog and read whatever you've written. Feedback is instantaneous via the commenting system. The main reason I named my blog A Million Chimpanzees was out of the horrible thought that writing as an art has been reduced to the lowest common denominator because there's no limit to who can blog or what can be blogged. Of course, this is also the realization of freedom of speech. Any citizen can, at any time, without limitation, post his or her ideas and statements to the Internet where anyone with access can read them. Reduce the ideas to 140 characters at a time, and blogging becomes twitter. Digitally film them, and you've got YouTube. Podcast and streaming video them and, putting it all together, you've got the cacophony I was describing a few paragraphs back.

I'm still trying to decide whether this is a good thing or not, but from the point of view of Fahrenheit 451, I think it is. To write (or otherwise create), we are compelled to think. This is the antithesis of Montag's world, where his wife Mildred spends all day pretending to be a part of a fictional, interactive "family" on her favorite TV shows (and adding 21st century technology to the mix, the "family" could well have been computer generated, rather than real, human actors). When the day is done, she wanders off to sleep with her iPod in her ear (not really, but the way Bradbury presents it, the device might well have been an iPod). Entertainment parks abound, and schools are not to educate, but to indoctrinate the next generation into a population of entertainment junkies, even as the country is on the verge of nuclear war.

In Montag's world, people are moving in a single direction; from more to less complex thoughts and ideas. Reading and education are not only discouraged, they're illegal. Humans are not only encouraged to be mindless TV junkies, it's almost the only choice they're offered. We, on the other hand, seem to be going both ways simultaneously; a culture addicted to entertainment, and obsessed by information. What do we really want, a lobotomy, or a PhD? Then again, how much reading to we actually do on a single subject, before we bounce to the next input, and the next and the next and the...You get the idea. If it isn't short and it isn't fast, it isn't compelling enough to stick with. Change the channel or click the next link (does this sound cynical?).

One of the best parts of reading this book was reading Bradbury's commentary. He wrote about how he managed to write the novel, the origin of the ideas and the mechanics of the writing itself. He didn't own a typewriter (no PCs and word processors the 1950s), so he found what amounted to a "typewriter lab" in the basement of a building at UCLA. He could rent typewriter time for ten cents a half-hour and, with not so much as a spell checker or even a bottle of liquid paper, he set about to create his masterpiece. It's in the context of the early 1950s that Bradbury wrote the book, and being able to step outside the book itself, and into the motivation of the writer, I found another dimension to the story and some of the history of where we come from.

Ray Bradbury grew up in a world of books, in a world before television and, even though the film industry was thriving, in a world where the moving image couldn't displace the world of imagination contained within the printed page. I'm old enough to still prefer a "real" book over an eBook, and will still close my web browser to thumb through pages and enter the realm of print, on occasion. That's part of the allure, both of checking out books at the library and of reading books. Fortunately, I'm not alone. A few days ago, I came across a blog article at PBS.org called Kicking Ink: The Guilty Pleasures of Print. I couldn't help but see the parallels between the blog article and the future Bradbury imagined. Books aren't forbidden and burned in our world, but we are made to feel a little guilty about reading them because of the "carbon footprint" they leave behind. Political correctness and the advance of technology take the place of the firemen and their kerosene.

No, we don't burn books, and we don't necessarily disdain thinkers and writers, but is what we're writing (including this blog) worth reading? I hope at least some of it (including this blog) is, but what we produce is in danger of buried in the screaming snowstorm of all the digital content we're generating at warp speed and beyond. Ray Bradbury's novel, over fifty years old now, still has something to tell us. It might not be exactly the same as the original message, but it's close enough to be chilling, or maybe ironic.

All that said, time marches on, and so does the book's creator. You can even go to raybradbury.com these days and find out what he's currently writing.

Endnote: On a whim, I crafted part of the title of this article on Philip K Dick's book title, Do Androids Dream of Electric Sheep?...another novel where humanity, at least as we know it, is an endangered species.

Share/Bookmark

Sunday, October 25, 2009

Joomla! 1.5: A User's Guide plus Fundamentals of Joomla! Video

Author: Barrie M. North
Format: Paperback, 480 pages plus DVD edition
Publisher: Prentice Hall PTR; 2nd edition (June 1, 2009)
ISBN-10: 0137012314
ISBN-13: 978-0137012312

Update: I just got a heads up from Heather Fox at Prentice saying that Fundamentals of Joomla Video Training & Users Guide Package (which includes the subsequently mentioned book and DVD training video) is still available exclusively at Barnes and Noble. After January 1, 2010, the bundle will become generally available. Please disregard any contrary bits of information in the original review. Thanks.

I think this bundle (the book and DVD) was a limited offer and unfortunately, it's taken me just a bit too long get to my review to take advantage of the package. Nevertheless, the products are still available individually, and are both written and presented by Barrie North. But let's back up a bit.

According to joomla.org, "Joomla is an award-winning content management system (CMS), which enables you to build Web sites and powerful online applications. Many aspects, including its ease-of-use and extensibility, have made Joomla the most popular Web site software available. Best of all, Joomla is an open source solution that is freely available to everyone." Between those two links, if you didn't have an idea of what Joomla was before, you do now.

Like a number of other Joomla books on the market, Prentice Hall's offering is tailored for the novice, but promises enough sophistication to take the reader up to at least a "quasi-professional" level. The Preface of the book states in part, that the reader isn't assumed to have any programming or even CSS experience. It's "easy to read" and "low on technical jargon", telling me that North wrote the book for the non-web designing business professional who wants to take advantage of CMS in general and Joomla in specific, to further their business purposes and goals.

That said, the book does teach the technical aspects of how to create a Joomla site from scratch but you start out at ground zero by being introduced to the concepts of web pages, CSS, and Joomla itself. Depending on your level of experience, the first chapter may be something you want to bypass, or at least just skim through. If you have web experience but no Joomla experience, Chapter 2 will tell you where to go to find and download Joomla, then how to install it. You are wisely directed to the XAMPP site to take care of the Apache, MySQL, and PHP side of things, but other options are also presented. Of course, you are also provided with links to acquire the required Joomla installation software.

While I don't consider the Chapter 2 instructions to be outside of a reasonably intelligent person's ability to comprehend, if you really are a completely non-technical person, you might want to take some time to read through the chapter and make sure you have all the steps down, before going through the installation process for both XAMPP and Joomla. This should save you a bit of time and frustration. However, for those with "power user" and up skill sets relative to web design or development, this shouldn't be too much of a chore.

Beyond this point, the book is fairly linear as far as assisting the reader in building the various skills required to create and manage a Joomla site, from basic administration, to organizing content, to creating menus, and so forth. Chapter 10 pulls together all that you should have learned in the prior chapters by letting you build a trial site. In this case, it's a school site, complete with content for students, parents, teachers, other staff, and so on. I suppose this would be the cherry on the cake if you're a school administrator in need of a website, but if you're not, the practice you gain can be applied to just about any organization with a bit of tweaking. If you really need another example, Chapter 11 teaches you how to build a restaurant site. The appendixes offer additional resources, including where to get help, case studies of actual commercial sites powered by Joomla, and more.

The Fundamentals of Joomla! video training DVD and 128 page study guide is based on North's Joomla! 1.5: A User's Guide so, if you have the book, you have all the content the DVD and accompanying booklet contains, just in a different form. On the one hand, this can seem redundant, but if you learn best by accessing more than one learning mode (text and video, for example) using them together will be especially handy. This method could be used either by the individual, or in a school classroom or business training venue. The video content itself might seem a little "light", especially if you have no familiarity with Joomla at all, but coupled with the 480 page primary text, it is a golden learning opportunity.

Share/Bookmark

Tuesday, October 20, 2009

Web 2.0 Expo New York Announces 2009 Keynotes and Event Program

Me: Not that O'Reilly needs my help in promoting their conferences, but I did receive this in my email from Maureen Jennings and I thought I'd pass it along. Who knows? Maybe a trip to New York will be on your schedule next month.

Sebastopol, CA, October 20, 2009 — Today Web 2.0 Expo New York announced this year's keynotes and new program elements. O'Reilly Media, Inc. and TechWeb, co-producers of Web 2.0 Expo and Web 2.0 Summit, welcome a lineup of distinguished keynote speakers including Jay Adelson, Chris Brogan, Caterina Fake, Jascha Franklin-Hodge, Beth Novak and Kevin Rose, in addition to an expanded program with first time sessions and a New York Launch Pad. Web 2.0 Expo New York returns on November 16-19, 2009 to the Javits Convention Center. More information is available at ny.web2expo.com.

"The Power of Less is about how the principles of Web 2.0 can turn constraints into opportunities and drive innovation," said Jennifer Pahlka, co-chair of Web 2.0 Expo. "We are inspired by the canonical examples of this power, such as Twitter's character limit, but also by the products and services launching now, in a time of less, that will change the world."

Web 2.0 Expo New York 2009 brings Launch Pad to New York for the first time and adds a series of new sessions to the program including a track on the topic of Government 2.0. On the heels of a successful Gov 2.0 Summit, the Gov 2.0 sessions will further illuminate how transparency, participation and collaboration can break down silos and increase efficiencies on the government level. In addition, the event presents a brand new Bootcamp program. These full day intensive programs, taking place Monday, November 16, are titled "Good Design Faster," "Search as Strategy" and "Communilytics: Applied Community Analytics" and will further cover design and online communities, respectively, as they relate to successful business practices.

Web 2.0 Expo New York will feature influential keynotes and speakers, detailed workshops, a multi-track conference, an "unconference" program called Web2Open, a first-time New York Launch Pad start-up program, a major tradeshow and many rich networking opportunities and events. Conference tracks include: Landscape & Strategy, Design & User Experience, Social Media, Development, Fundamentals, Web 2.0 at Work, Government 2.0, Mobile, Performance and Analytics. The event targets developers, designers, entrepreneurs, marketers, and business professionals embracing Web 2.0 technologies.

Web 2.0 Expo New York 2009 welcomes industry leading companies that are participating and exhibiting in this year's show including Diamond sponsors: IBM and Microsoft; Silver sponsors: Awareness, blueKiwi, Jive Software, Layered Technologies, Inc., Neustar Inc., OpenText, Opera Software, Overtone, Qtask, Rackspace, and Sony Ericsson.

To learn more about the 2009 Web 2.0 Expo New York or to register, visit: http://www.web2expo.com/ny

View selected presentations from Web 2.0 Expo New York 2008 at: http://www.web2expo.com/webexny2008/public/schedule/proceedings

To see articles, blogs, and other coverage of last year's event, visit:
http://www.web2expo.com/webexny2009/public/content/news-coverage

For a look at photos from New York 2008, see:
http://www.flickr.com/photos/x180/sets/72157607322639138/

To apply for a media pass, visit: http://www.web2expo.com/webexny2009/public/content/media

To read the O'Reilly Radar, visit: http://radar.oreilly.com/

If you have ideas about areas you'd like to see included at the conference, send a note to: webexpo-idea@oreilly.com

If you'd like to stay up to date on information relating to Web 2.0, sign up for the conference newsletter (login required): http://elists.oreilly.com/#conferences

About TechWeb

TechWeb, the global leader in business technology media, is an innovative business focused on serving the needs of technology decision-makers and marketers worldwide. TechWeb produces the most respected and consumed media brands in the business technology market. Today, more than 13.3* million business technology professionals actively engage in our communities created around our global face-to-face events Interop, Web 2.0, Black Hat and VoiceCon; online resources such as the TechWeb Network, Light Reading, Intelligent Enterprise, InformationWeek.com, bMighty.com, and The Financial Technology Network; and the market leading, award-winning InformationWeek, TechNet Magazine, MSDN Magazine, Wall Street & Technology magazines. TechWeb also provides end-to-end services ranging from next-generation performance marketing, integrated media, research, and analyst services. TechWeb is a division of United Business Media, a global provider of news distribution and specialist information services with a market capitalization of more than $2.5 billion. *13.3 million business decision-makers: based on # of monthly connections.

About O'Reilly
O'Reilly Media spreads the knowledge of innovators through its books, online services, magazines, and conferences. Since 1978, O'Reilly Media has been a chronicler and catalyst of cutting-edge development, homing in on the technology trends that really matter and spurring their adoption by amplifying "faint signals" from the alpha geeks who are creating the future. An active participant in the technology community, the company has a long history of advocacy, meme-making, and evangelism.

O'Reilly conferences bring together forward-thinking business and technology leaders, shaping ideas and influencing industries around the globe. For over 25 years, O'Reilly has facilitated the adoption of new and important technologies by the enterprise, putting emerging technologies on the map.

Sunday, October 11, 2009

Python: Converting from one temperature scale to another is easy, but what about eight?

In just about any class or set of tutorials involving beginning programming, there's usually a problem or set of problems having to do with converting temperatures. Just about everyone has had to write a wee bit of code to convert Fahrenheit to Celsius or vice versa. That's not much of a chore. However, there are eight major temperature scales available (though some are around now only for their historical value), according to wikipedia. What if you had to write a program that let a user convert a temperature from any of these scales to any of these scales?

Do you have any idea how many if statements you'd have to write? On the previously referenced wikipedia page, there are eight tables (one for each temperature scale) so converting from any of scales to any of scales would, in theory, require 8 times 7 or 56 if statements (it wouldn't be 8 times 8 because you wouldn't convert from a scale to itself). That's an awful lot of coding and there's got to be an easier way.

Actually, there's more than one way to make it easier, but this is a tutorial that is, or should be, within the grasp of a relative newbie to Python. I took a stab at it and came up with what I think is a pretty straightforward bit of programming that doesn't require a lot of typing.

Oh. I forgot. There is a specific requirement for the solution to the problem. The code must convert from the source scale temp to central or canonical scale, such as Celsius, and then must convert that value from Celsius (for example) to the target scale temp. Here's what I came up with. I don't claim this is the best possible solution, but I don't think there's anything fundamentally wrong with it, either (I checked with a higher power to be sure). See what you think. Here it is:

t = float(raw_input("Please enter the temp: "))
source = raw_input("Please enter the source temp scale: ")
target = raw_input("Please enter the target temp scale: ")

if t < 1000000.0:
    if source == "k":
        cel = t -273.15
    elif source == "c":
        cel = t
    elif source == "f":
        cel = (t - 32) * 5.0 / 9.0
    elif source == "r":
        cel = (t - 491.67) * 5.0 / 9.0
    elif source == "d":
        cel = 100.0 - t * 2.0 / 3.0
    elif source == "n":
        cel = t * 100.0 / 33.0
    elif source == "re":
        cel = t * 5.0 / 4.0
    elif source == "ro":
        cel = (t - 7.5) * 40.0 / 21.0
    if target == "k":
        answer = cel + 273.15
    elif target == "c":
        answer = cel
    elif target == "f":
        answer = cel * 9.0 / 5.0 + 32.0
    elif target == "r":
        answer = (cel + 273.15) * 9.0 / 5.0
    elif target == "d":
        answer = (100.0 - cel) * 3.0 / 2.0
    elif target == "n":
        answer = cel *  33.0 / 100.0
    elif target == "re":
        answer = cel * 4.0 / 5.0
    elif target == "ro":
        answer = cel * 21.0 / 40.0 + 7.5

print answer
I found all of the formulas for conversions at wikipedia, too. You can copy this code into a text editor and save it as something like covert.py, then run it to see how it works for you. Share/Bookmark

Monday, October 5, 2009

Python: input, raw_input, and inadvertently treating integers as strings

This is a "newbie" mistake, but these little details do tend to get in the way, which is why I'm posting my wee Python tutorial. If you know Python or programming, even reasonably well, this will probably seem way too simple to you. Just giving you a "heads up" so you don't waste your time.

I encountered a lesson that teaches storing conditionals using booleans. The code was presented like this:

young = age < 45 

slim = bmi < 22.0 
if young and slim: 
risk = "low" 
elif young and not slim: 
risk = "medium" 
elif not young and slim: 
risk = "medium" 
elif not young and not slim: 
risk = "high"
Of course, you can't really run this. If you try, you get this:
Traceback (most recent call last): 
File "assign_bool.py", line 1, in  
young = age < 45 
NameError: name 'age' is not defined 
I figured the solution was to modify the program to let me input the age and bmi, then print the results. I modified the code like this:
age = raw_input("Please enter your age: ")
bmi = raw_input("Please enter your bmi: ")

young = age < 45
slim = bmi < 22.0
if young and slim:
    risk = "low"
elif young and not slim:
    risk = "medium"
elif not young and slim:
    risk = "medium"
elif not young and not slim:
    risk = "high"

print risk
Like I said, if you have any real programming experience at all, you can already see my mistake. If not, read on. My modified code happily returned prompts for my age and bmi. Being scrupulously honest, I entered my correct age. I have no idea when my bmi is, so I made up a value. When I pressed enter, the value returned was "high". Just to make sure the program was working properly, I entered a young age and a low bmi. Egad! The return value was still "high". In fact, no matter what values I entered, "high" was always returned. This wasn't right. On the fly, I tried this change:
age = input("Please enter your age: ")
bmi = input("Please enter your bmi: ")
It seems very simple (and it is). Using raw_input, the data is always interpreted as a string, rather than the INT type I needed for the program to work. Since strings, rather than integers where input, Python worked it's way through the various tests and, since none of the other conditions matched, printed the result of the final clause which of course, was "high". input interprets what you enter as a python expression, which works, but isn't the best solution, since it can return a wide variety of object types. What's really needed is a way to input something that's always interpreted as a number. I did a bit of research and came up with the following solution, changing the first two lines of my program accordingly:
age = int(raw_input("Please enter your age: "))
bmi = int(raw_input("Please enter your bmi: "))
Now the program will always expect integers to be input for age and bmi. If you try to enter a string now, you'll get something like this:
Please enter your age: Fred
Traceback (most recent call last):
File "assign_bool.py", line 1, in 
age = int(raw_input("Please enter your age: "))
ValueError: invalid literal for int() with base 10: 'Fred'
Play with the different input types and see for yourself. Share/Bookmark