Python Category

PyCon 2012 Beginner Python Solutions

sample.py import types import unittest class SampleTest(unittest.TestCase): def test_example(self): “”” An example of how to do the tests “”” # Variables # # Create a variable “name“ that is assigned to the string # “Matt”. # ================================ name = “Matt” self.assert_(isinstance(name, types.StringType)) self.assertEquals(name, ‘Matt’) if __name__ == ‘__main__’: unittest.main() variables.py import unittest class TestVariables(unittest.TestCase): def […]

Rate this:

Read More

Python send email Gmail

Below is sample code for sending email through Gmail using Python import smtplib; fromaddr = ’email@gmail.com’ toaddrs = ’email@gmail.com’ msg = ‘My first email message from Python’ username = ’email@gmail.com’ password = ‘###’ server = smtplib.SMTP(‘smtp.gmail.com:587’) server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit()

Rate this:

Read More

Twitter Python API Tutorial

Playing around with the Twitter Python API. Download: wget http://code.google.com/p/python-twitter/downloads/detail?name=python-twitter-0.8.2.tar.gz Extract: tar -xvf python-twitter-0.8.2.tar.gz Install Twitter (navigate inside the Twitter extracted folder) sudo python setup.py install Make sure Python Setuptool is installed apt-get install python-setuptools Install dependent files httplib2-0.7.2$ sudo python setup.py install simplejson-2.3.2$ sudo python setup.py install simplegeo-python-oauth2-a83f4a2$ python setup.py build simplegeo-python-oauth2-a83f4a2$ sudo python […]

Rate this:

Read More

Python writing reading large datasets script

Below is a script to read and write to and from large datasets saved as csv files. Writing datasets to an csv file. import csv #writing data into csv file writer = csv.writer(open(‘dataset.csv’, ‘wb’, buffering=0)) writer.writerows([ (‘GOOG’, ‘Google Inc.’, 123.44, 0.32, 0.09), (‘YHOO’, ‘Yahoo! Inc.’, 2.33, 99.23, 0.123), (‘IBM’, ‘IBM Inc.’, 223.44, 212.32, 6.42) ]) […]

Rate this:

Read More