Posts

Showing posts from October, 2012

django not found problem in mac - eclipse - pydev

I am using mac air now (I still love ubuntu). I have installed django, eclipse, pydev in my Mac OS X. Then I tried to create a new django project in eclipse and got the error "django not found". Is says that it failed to import django. But in my terminal, I can import django with no problem: >>> import django >>>  This was a bit confusing at the beginning. Then I found that I need to add a new path in the eclipse > preference > pydev > interpreter-python (just add the directory where I unzipped Django-1.4.1.tar.gz).  You can follow this blog post for step by step installation instructions. Update: This stackoverflow post - Why doesn't Pydev Find Django discuss the same issue.

MySQLdb Warning: Field x doesn't have a default value

Today while I was working on a Python program that uses mysql database, I got a new type of warning while inserting data into a table: /usr/lib/python2.7/dist-packages/MySQLdb/cursors.py:206: Warning: Field 'id' doesn't have a default value   r = r + self.execute(query, a) Then I looked into the mysql table and found that the field 'id' was supposed to have a default value (primary key, auto increment). But somehow it didn't have. That was the reason of the warning and it got vanished after I fixed the table. Hope my experience will save your time if you get into this problem. Happy coding! :)

mysqldb cursor.fetchall returns tuple not list

In Python MySQLdb module, cursor.fetchall() returns a tuple. For example: >>> query = "SELECT name FROM table ..." >>> cursor.execute(query) >>> result = cursor.fetchall() >>> result (('autos',), ('books',), ('health care products',), ('sports equipment',)) >>> But often we require the result to be a list instead of tuple. I don't know any straight forward way to do this. So I used the following code: >>> li = [x[0] for x in result] >>> li ['autos', 'books', 'health care products', 'sports equipment'] >>> Please let me know if there is any better way!

An Introduction to Interactive Programming in Python | Free Online Course

The course titled "An Introduction to Interactive Programming in Python" just started on coursera . The course is eight weeks long and free to attend. It consists of video lectures, quizes and mini projects. There is also a forum to discuss things. From their website: This class is eight weeks long. For each week, you will watch two sets of videos (part a and part b) and then complete one quiz for each set. These quizzes are due on Tuesday/Thursday, respectively. The main task for each week is to complete a mini-project that is due on Saturday. You will then be asked to assess your peer's mini-projects on the following Sunday/Monday.   Here is the syllabus in detail: Week 1 Topics: Expressions, variables, functions, conditionals Mini-project: "Rock-Paper-Scissors-Lizard-Spock" game Week 2 Topics: Event-driven programming, local and global variables, buttons and input fields Mini-project: "Guess the Number" game Week 3 Topics: The ca

mysqldb problem | update query not working

I had a problem executing an update query. I was using MySQLdb to deal with MySQL from Python. If the query runs successfully then cursor.execute() returns 1. So, I used a print statement and it printed 1, but when I checked the table in mysql database using phpmyadmin, I couldn't see the update in the table. print cursor.execute("UPDATE products SET category = %s WHERE upc = %s", (category, upc)) Then I Googled for a few minutes and found this link: http://stackoverflow.com/questions/1028671/python-mysqldb-update-query-fails So, the solution I used is the following: conn.autocommit(True)