Updates from October, 2009 Toggle Comment Threads | Keyboard Shortcuts

  • Jeff Forman 7:13 pm on October 21, 2009 Permalink
    Tags: , python, sqlite   

    Do or do not…the use of Try, with Python and sqlite 

    I’ve been hacking around writing random Django apps and scripts lately and came across something I had been meaning to look into over the past couple weeks. What happens when you select a row of data from a database (mysql, sqlite, whatever), that comes back empty and you try to iterate over it? Example: select * from authinfo where username = “foo”, and the user ‘foo’ does not exist.

    At first I thought:

    if db_query.fetchone()[0]:
    

    would work, but this only gives an error “TypeError: ‘NoneType’ object is unsubscriptable.”

    Then I remembered the try/except syntax, which worked great in this instance.

    #!/usr/bin/env python
    import sqlite3
    
    username = "testuser1"
    password = "foobar"
    
    conn = sqlite3.connect("/home/jforman/testsqlite.db")
    c = conn.cursor()
    db_query = c.execute("select username from authinfo where username = '%s'" % username)
    
    try:
     db_output = db_query.fetchone()[0].strip()
     print "output: %s" % db_output
    except TypeError:
     print "no user found"
    
    c.close()
    conn.close()
    

    Am I expecting too much that Python can handle iterating over an empty line (and just skip the whole thing) ? Or is this just how it’s done, where every database query-related line is done in a try stanza?

     
    • Michael Marineau 8:57 pm on October 21, 2009 Permalink | Reply

      It’s a little cleaner to use an if rather than a try/except:

      db_query = c.execute(“blah”)
      row = db_query.fetchone()

      if row:
      print “row 1: %s” % row[0]
      else:
      print “nothing found”

    • Steve Laniel 7:49 am on October 22, 2009 Permalink | Reply

      Right; I second Mike. Jeff, the thing to remember is that an empty list evaluates to False. That’s why “if row: foo” works.

      In C and C++ and Perl, you could compress those rather nicely:

      if( row = db_query.fetchone() ) {
      doSomethingWith(row);
      }
      else {
      doSomethingElse();
      }

      One happy consequence of putting the row inside the if() is that the row goes out of scope when you leave the if/else. Whereas in Python, that row is sticking around even when you’re done with it.

    • Jeff Forman 7:54 am on October 22, 2009 Permalink | Reply

      @Mike and @Steve,

      I could have swore I tried doing what you guys suggested and throwing it in an if/else block, but I got that type error. Yay computer languages. Good to know for next time.

  • Jeff Forman 3:49 pm on September 19, 2009 Permalink
    Tags: , , sushi   

    O Ya for Sushi? Oh Yeah 

    I had RSVP’d for Ignite Boston 6, hosted by O’Reilly which was downtown this year at Fidelity’s Headquarters. Since it was right around dinner time, I figured I would grab some dinner downtown, maybe run over to Chinatown before jumping back to the Financial District. I had looked around, and noticed the O Ya was around the corner. O Ya being ranked as one of the country’s best Sushi resturaunts last year by the NY Times. I had heard the hype on the local food blogs, Chowhound and other places, and was curious about about the food. Knowing it was expensive, I wanted to go myself before bringing M and dropping a mortgage payment on dinner.

    Luckily I had dressed in jeans and a dress shirt, so while I ended up being a little under-dressed, I did not feel uncomfortable among the other clientèle who walked in later. The restaurant was empty, it being 530pm which is on the earlier side of dinner. The hostess took what I thought was an inordinate amount of time trying to “fit me in” but I obliged respectively and was seated at the bar. I was given the menu, and of course offered omakase, chef’s choice. While scanning the menu I noticed that the prices were indeed very high, even for sushi, and decided to put myself at the whim of the Chef for $75. This in my mind was how I would keep myself within a price range, and explore things I would not have otherwise selected on my own.

    The hit list (from the receipt, I didn’t have a chance to take pictures or write down each dish specifically):

    • Hamachi N O Ya
    • Salmon Tataki
    • Warm Eel O Ya
    • La Ratte Potato Chip
    • Shiitaki Mushroom O Ya
    • Kumamoto Oyster O Ya
    • Shima Aji Sea Urchin
    • Salmon O Ya
    • Hamachi Viet O Ya
    • Tuna Tataki O Ya
    • Foie Gras O Ya

    I must say that the sushi was excellent, every dish visually stunning, and even better on the way down. Most of the dishes were cooked or heated in some way, which caught me off guard from other sushi I have enjoyed. Having had omakase previously while on vacation in Los Angeles, the fact that this presentation was more cooked than I expected, made it no less satisfying. The only dish I felt was a let down was a piece of hamachi covered with a home made potato chip. While the dish was good, I felt that while at a restaurant that carried such prestige, this just seemed like a pretty-good potato chip on to pof a piece of tuna and some rice. It felt boring, more of a “a potato chip, really?” kind of moment. The only other nit was the use of basil. While very fresh and refreshing, it overpowered the dishes it accompanied, and I found myself removing it as courses went on.

    The last course was the most memorable since it was the most different. Foie Gras seared with some balsamic vinaigrette and chocolate, on top of a simple sea-weed rolled piece of rice,  paired with a sample of an 8 year aged sake. While having more of a syrupy consistency and being heavier than most sakes I have had, is still extremely sweet, with a strong hint of raisins. A perfect combination with the very savory foie gras it had been paired along side. This sake, worth noting and buying on my own for consumption at home, is called Hanahato Kijoshu.

    I thoroughly enjoyed my meal, and left full but not stuffed from the portion size I had decided on. I felt the omakase kept my budget in check. While scanning the menu during my meal, it would be very easy to blow a $100 or even $150 per person going a la carte selecting sushi or other cooked entrees from O Ya. This is definitely a special occasion meal, and somewhere I put on my list to hit once or twice a year if my budget and appetite desire.

     
  • Jeff Forman 10:28 pm on August 25, 2009 Permalink
    Tags: stuttering   

    How not to help a stutterer, the National Grid Way 

    For those who know me, know I stutter. It comes and goes, not really any one situation or context where I am more fluent than others. But one situation has always given me problems, the phone. I don’t know what it is, whether I can’t see the person’s face on the other end to help me along, or just the fact that I feel rushed, and don’t want them to wait, the words are just more difficult to formulate and get out.

    Over the weekend my fiance and I moved into our new condo, which brought all the necessary steps of transferring utility services from one location to another. Most of them I handled online, with a couple emails here and there, but when it came to National Grid, they failed to help out this stutterer. I initiated the electric service transfer, but through the online process, it asked me for my account number (being that I was a previous National Grid customer in NY, five years ago, I did not have that previous account number handy anymore. Seriously?), so I was forced to get on the phone. My fiance helped me out by taking most of the call, after giving her my SSN and other relevant information.

    As we’re getting through the sharing of information, the gentlemen on the other end of the phone at National Grid asks to speak to me, to verify that the information I’ve given my fiance is valid and that she is allowed to open the account in my name. Now while most people might find this preposterous, stutterers have trouble saying their name at times. The most common phrase I might ever have to say in my entire life, and it’s not an unconcious motion. So I struggled to get it out, and answer his few questions. Feeling kind of pissed off after my fiance had told the gentleman flat out “My fiance stutters, but I have his personal information and he’s sitting next to me to answer any questions I might not know.”

    I can understand the security implications of not verifying that she in fact is authorized to open the account in my name, but cut me some slack. Talking is difficult for me at times, and you just gave me one more hoop I have to jump through to keep my lights on. I’m the biggest fan when it comes to online account management,I can’t imagine what it’s like to be unable to speak at all and have to go through these situations.

     
  • Jeff Forman 2:08 pm on August 18, 2009 Permalink
    Tags: ,   

    There’s wine in Southern California! 

    I have been meaning to write this post for a while, but as you can imagine, work and life got in the way. Anyways..Back in May, I went out with M to a friend’s wedding in Woodland Hills, CA. just north of Los Angeles. Given that we had all of Saturday to kill before the Sunday wedding, we jumped in the car and headed up The 101 towards Santa Barbara and the Santa Ynez valley, where there are a ton of wineries. Thanks to the help of a coworker who did his undergrad degree at UCSB, I had a full list of wineries to visit.

    The drive up there is nothing short of spectacular:

    Pacific Ocean off of the 101

    Pacific Ocean off of the 101

    After a good sandwich lunch in Santa Barbara, we jumped back in the car and continued up the more rural roads towards Santa Ynez and Los Olivos, where the wineries are concentrated. It just so happened that a few weeks ago, wild fires had charred some of the woodland around Santa Barbara. It really hit home having seen in person all the videos you see on TV. But on to the wine.

    The damage:

    • Curtis winery – 2005 Syrah
    • Fess Parker – 2007 Viognier
    • Roblar Winery – No wine
    • Beckman – 2007 Estate Grenache
    • Bridlewood – 2005 Syrah English Pleasure, 2005 Syrah Blue Roan
    • and others that I forgot to write down.

    For me, the list was a big surprise. Normally I am not a big fan of white wine, but after tasting several wineries Viogniers, they were excellent. A lot of character, not the normal flat taste I find I get with Chardonnay. A very buttery finish which I also found in the Syrah and Grenache’s that we picked up. Winies were busy with people of all ages, from college students on up to retirees in wine tour busses. I didn’t find a lot of the wineries on top of each other, which I felt was the case in Napa. You had to get back in your car, and drive to the next winerie, which was a ways down the road.

    Being that we live in Massachusetts, the wine came back with us on the plane instead of being shipped (Thanks MA tax law).

    If lost, follow the signs

    If lost, follow the signs

    Having been winery hopping in Upstate New York, Washington, Napa, and Long Island, Southern California had a very different feel to it. A lot more relaxed, but still enough people to not feel odd that you were the only patron at a winery. And while I love my seasons in Boston with snow on the ground, the weather in Southern California can’t be beat. If I or friends of mine ever find themselves in the Central/Southern California area, I highly recommend taking a ride up here and tasting the wine. If not just for the wine, but the view too.

     
  • Jeff Forman 6:06 pm on July 2, 2009 Permalink
    Tags: reading   

    It only took me six months to read War and Peace. 

    Like a lot of people here in Boston, I take the T (subway) to and from work. From where I live along the Green line into Cambridge is about a forty-five minute jaunt. Some people read the newspaper, others read books, and others cram for a biology test they are on their way to. While I could get through the paper in a couple stops (for me, it’s currently an 18 stop trip from home to work) and then have nothing else to do. So I challenged myself, time to read one of those classic books that would keep me occupied for more than a week or two. It was in Costco back in January that I saw War and Peace, a recent edition released 1135 pages in length. It was cheap, I picked it up, and decided to challenge myself to see if I could read it, and actually get through it.

    Seven months later I am realizing that while I like to read, and it definitely keeps me occupied, I need to actually read books I like, and not just try to challenge myself. In short, the book is about the 1812 invasion of Russia by Napoleon. The language was well written, I found I could follow it, while not getting bogged down by the endless footnotes and end notes explaining popular culture back then. The only problem I had with the book was the near endless list of characters. I couldn’t keep track of a few characters, and follow their development before more people were added to the story.

    I bid you farewell War and Peace. You have lived in my messenger bag for 7 months along with my laptop and often my lunch to and from work. I’ll be glad to pawn this off onto someone who could enjoy it more than I could. I’m going back to my books that are a couple hundred pages, that while keep me occupied, are a bit more entertaining and to my liking.

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel