Category: Python

eclipse pydev error – not finding

I just compiled and installed mysqldb for python 2.7 on my mac os 10.7. import MySQLdb as mysql def main(): conn = mysql.connect( charset=”utf8″, use_unicode=True, host=”localhost”,user=”root”, passwd=””,db=”” ) if __name__ == ‘__main__'(): main() When executing it I get the following error message Traceback (most recent call last): File “/path/to/project/Python/src/cvdv/TestMySQLdb.py”, line 4, in import MySQLdb as

Continue Reading →

Python: extract all hyperlinks from a webpage

I know they must be thousands of programs to do this, but just thought i would give it a try. Its pretty easy. I will keep editing this as and when I improve my regular expression to do this. import urllib as ulimport BeautifulSoup as bsimport re myFile = ul.urlopen(http://www.sfbay.craigslist.org/roo/)soup = bs.BeautifulSoup(myFile)#print soup.prettify()for anchor in

Continue Reading →

Python: HarvestMan

When I was researching about downloading pages and all content from a website. I got this crawler..its very simple. and you need to take a look at it. Its real cool and simple. http://code.google.com/p/harvestman-crawler/wiki/WorldsSimplestCrawler windows user if you have cygwin installed it would be helpful.

Continue Reading →

raw extraction of data from google blogger to wordpress

This is a small script I wrote to extract all posts from my blogger which was in the form a xml or aton feed and insert into a wordpress database. import feedparser import re d = feedparser.parse(“C:\Python25\programs\blog.xml”) count = len(d[‘entries’]) loopVar = 0 p = re.compile(r'[‘”]’) mainInsertString = “INSERT INTO `wp_posts` (`ID`, `post_author`,” s2 =

Continue Reading →

Facebook app development

My 0.02C that a novice facebook developers need to do, when developing a new application is look at this console given by facebook itself. the links are below: http://developers.facebook.com/tools.php Basically the above has both php and XML links for developers to easily develop. Attaching a screenshot below:

Continue Reading →

Python: Creating a DocTest / simple example

We use doctest to check whether a written method is giving the correct output or to check for the docstrings for interactive examples or for regression testing or to write documentation. Consider this example def multiplyTwoNumbers(a,b): product = a*b return product this method is used for multiplying two numbers. But there is no way to

Continue Reading →

MySQLDb inserting data into a MySQL table with variables

Of the many ways to inset values into a MySQL database, we could use the below also: insert_query = “INSERT INTO %s(symbol, date,open,high,low,close,volume) VALUES( %s,%s, %s , %s , %s , %s, %s )” insert_cursor.execute(insert_query %(symbol_Nasdaq, symbol_Nasdaq, row_Nasdaq[0], row_Nasdaq[1], row_Nasdaq[2], row_Nasdaq[3], row_Nasdaq[4], volume_fin)) The above query will most likely give you an error stating the

Continue Reading →

mysql error 11001 in mysqldb

when trying to run a query in mysqldb from python I was getting the error 11001. the best option would be run the query as below: conn = ms.connect(host=”localhost”,port=3307, user = “root”, passwd = “”, db = “xxx”) This error would be seen because I changed the port when I was installing MySql. So when

Continue Reading →

funny thing about float in python

try dividing 10/100 in python. You get 0. you can do this 1/2. You get 0. This is not an error. Python is like this. You can refer here http://www.python.org/doc/2.2.3/whatsnew/node7.html for more details. To rectify this you can do the following: import __future__ import division This will return the floating point results.

Continue Reading →

Python: Classes

Suppose you have a below class in python: class Complex: def __init__(self, realpart, imagpart): self.r = realpart self.i = imagpart >>> x = Complex(3.0, -4.5) What happens? Even though you have not given the arguments for the class, the arguments get initialized to the default constructor.Therefore 3.0 and -4.5 will be set to realpart and

Continue Reading →

Python: usage of self and __init__

I was reading this book dive into python and thought this would be helpful. So..When defining your class methods, you must explicitly list self as the first argument for each method, including __init__. When you call a method of an ancestor class from within your class, you must include the self argument. But when you

Continue Reading →

Python: Configuring paramiko on windows

1) Goto to http://twistedmatrix.com/trac/wiki/Downloads and download the pycrypto package .exe for windows/python2.5. This is needed for running paramiko. 2) Next, download the paramiko package from http://www.lag.net/paramiko/. 3) Unzip paramiko to a temporary folder, better if you unzip it to the folder where python is installed. 4) Go into the folder for paramiko. 5) Open command

Continue Reading →

Python: encode / decode problems

I was trying to insert values into  a table from a blog post. Problem code: filehandle.write(“, 1, ‘2009-06-03 17:11:58’, ‘2009-06-03 17:11:8’,’”) e = d.entries[val] content = e.content[0].value #print content filehandle.write(str(content)) Issue: Since we are trying to write a junk value in to a file, and this is not ascii supported we get the below error.

Continue Reading →