21.01.2015 Views

color version - PET: Python Entre Todos - Python Argentina

color version - PET: Python Entre Todos - Python Argentina

color version - PET: Python Entre Todos - Python Argentina

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using additional libraries and virtualenv<br />

Author: Tomas Zulberti<br />

>>> msg = MIMEText(" This is the e-mail's content... ")<br />

>>> msg['From'] = USERNAME<br />

>>> msg['To'] = USERNAME<br />

>>> msg['Subject'] = "This is the e-mail's subject"<br />

>>> mailServer.sendmail("tzulberti@gmail.com", "tzulberti@gmail.com", msg.as_string())<br />

Lets read a web page’s content.<br />

The subjects I’m going to talk about are:<br />

• <strong>Python</strong>’s standard library<br />

• What to do when something isn’t on that library.<br />

• Installing additional libraries<br />

• Using virtualenv to solve the problems that show up<br />

<strong>Python</strong> comes with around 350 usable libraries. http://docs.python.org/library/index.html<br />

There’s a bit of everything:<br />

• Stuff to work with mails<br />

• Fetching web pages<br />

• Interacting with the operating system<br />

• Working with compressed files<br />

• Parsing different types of documents (xml, json, etc…)<br />

AND MANY MANY MORE….<br />

But first of all, lets see some examples of things you can do with <strong>Python</strong>’s built-in<br />

batteries.<br />

Lets send an e-mail using a GMail account:<br />

>>> import smtplib<br />

>>> from email.MIMEText import MIMEText<br />

>>> USERNAME = "tzulberti@gmail.com" # Put your username here<br />

>>> PASSWORD = "not-gonna-tell" # Put your password here<br />

>>> mailServer = smtplib.SMTP('smtp.gmail.com',587)<br />

>>> mailServer.ehlo()<br />

>>> mailServer.starttls()<br />

>>> mailServer.ehlo()<br />

>>> mailServer.login(USERNAME, PASSWORD)<br />

>>> from urllib2 import urlopen<br />

>>> response = urlopen("http://python.org.ar/pyar/")<br />

>>> html_content = response.read()<br />

>>> print html_content<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Inicio - PyAr - <strong>Python</strong> <strong>Argentina</strong><br />

<br />

Or work with compressed files<br />

>>> import zipfile<br />

>>> compressed_file = zipfile.ZipFile("THA_FILE.zip", "r")<br />

>>> for name in compressed_file.namelist():<br />

... print name<br />

>>> compressed_file.extract()<br />

But python’s standard library doesn’t have it all:<br />

• It doesn’t know how to read some file formats (yaml, mp3, video)<br />

• Web Frameworks (pylons, django, web2py)<br />

• Using APIs from Youtube, Deliciosus, etc….<br />

We’ve seen that the standard library can do many things, but there are some that are<br />

easier if done with third-party libraries:<br />

• Web frameworks save us from some repetitive work.<br />

• API bindings save us the work of reading a lot of documentation on the services<br />

they wrap.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!