Archive for the ‘Python’ Category

A Good Python Interview Question? Do You Know the Answer?

Tuesday, July 3rd, 2012

Do you guys think this would this be a good Python interview question?

Given this code:

>>> l=[1]
>>> a,b,c = None, None, None
>>> for var, val in zip((a,b,c),l):
... var=val

What will the value of “a” be?

I came across something similar today and I realized it requires a pretty solid understanding of how names/variables work in Python.

If you’re not sure about the answer, try reading up on Python names here and then try to answer it. If you still don’t know, try running it.

Python – How to use MySQLdb when MySQL Isn’t Installed Locally

Monday, November 21st, 2011

It turns out there’s a new MySQL connection library called MySQL Connector.

I installed it and then I just imported that into my script and everything else seemed to work normally. At least for my simply import script.

import mysql.connector as MySQLdb

I believe this library is designed for those who want to connect to MySQL on another server. It doesn’t seem to require any MySQL libraries to be installed.

When I tried to install mySQLdb it complained with the following message. I’m guessing the problem was that it requires MySQL to be installed:

File "/Users/pinerog/Downloads/MySQL-python-1.2.3/", line 24, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

Python zipfile – Fixing Error: Unable to unarchive “..” into “Downloads”

Wednesday, October 19th, 2011

I made a zip file in Python and tried to open it on my mac. I got the error mentioned in the title.

Investigating further, I got this information from running:

lm1:Downloads pinerog$ unzip -t

  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of or, and cannot find, period.

Well, silly me, I forgot to close the zip file I made in Python. Here is the correct code for generating a zip file. Make sure you close the zip file object!

import zipfile
import glob, os

file = zipfile.ZipFile("", "w")
for name in glob.glob("samples/*"):
    file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)

Python – Excel – How to merge cells with xlwt

Thursday, October 13th, 2011

The sheet object has this method:


Style and label are optional.

There is an example of its use here:

MySQL – Is there a limit to the number of items in an “in” clause

Monday, August 8th, 2011

I couldn’t find any documentation on this, so I wrote a quick Python script to test this:

Test how many items can be in MySQL's in clause

Table I'm using for testing:
        id bigint NOT NULL,
        PRIMARY KEY (id)
import MySQLdb

conn = MySQLdb.connect(host = 'localhost',
    user = 'root', passwd = '*****', db = 'scratch', charset='latin1')
curs = conn.cursor()

#test maximum sql in clause
def test_sql_in():
    test_up_to = 20000
    curs.execute('truncate table INTEST')
    for i in range(1,test_up_to):
        curs.execute('INSERT INTO INTEST (id) VALUES (%s)', (i,))
    for i in range(1,test_up_to):
        in_str = ','.join([str(v) for v in range(1,i+1)])
        select_sql = "select * from INTEST where id in (%s)" % in_str
        rows = curs.fetchall()
        assert len(rows) == i
        if i % 100 == 0: print i


It ran fine. I tried it up to 20,000 items. Though it does get pretty slow, even with the index.

Note: I only tried this on MySQL 5.1.55 on Fedora 14.

Your homework:

  • How does it run for other MySQL engines such as MyISAM?
  • Can it handle a higher number like 1 million?