Blogs

Python Challenge Level 13

@10 Oct 2012 20:23 Tags: python pythonchallenge

import xmlrpclib
server=xmlrpclib.ServerProxy('http://www.pythonchallenge.com/pc/phonebook.php')
print server.system.listMethods()
print server.phone('Bert')
- Comments: 0

Python Challenge Level 12

@10 Oct 2012 20:21 Tags: python pythonchallenge

info=open("evil2.gfx",'rb').read()
[open("12_f%d.png" %i, "wb").write(info[i::5]) for i in range(5)]
print 'end'
- Comments: 0

Fetch weather information from BBC using Beautifulsoup

@09 Oct 2012 16:09 Tags:

Thanks to the help of Beautiful Soup.

Beautiful Soup is a Python library designed for quick turnaround projects like screen-scraping.

It is very easy to use and speed is acceptable (out of my concern).

I want to get the weather information from BBC website (http://www.bbc.co.uk/weather/). The website content is huge, at least for me. But that doesn't really matter. With the help of Beautiful Soup, I don't have to know the exact structure of the website.

First I need to import the urllib2 and BeautifullSoup library, also the pprint for output.

import urllib2
from bs4 import BeautifulSoup
import pprint

Download the whole website content from BBC and construct a soup object.
htmlFile=urllib2.urlopen('http://www.bbc.co.uk/weather/2655984?day=1')
htmlData = htmlFile.read()

The number 2655984 stands for Belfast. When you select a different city in the BBC website (http://www.bbc.co.uk/weather/), the city number will show up in the browser address bar. 'day=0' means today and 'day=1' means tomorrow, etc..

All the weather information is in a html table, from the source of the website we can see

<table tabindex="0" class="weather moving-window three-hourly-0 one-hourly-12" 
    summary="Each column contains hourly forecast details for weather conditions, temperature, wind speed, wind direction, humidity, visibility and pressure.">

But we do not need so much information to find this table, in fact only one word 'weather' will do.
table=soup.find("table",class_="weather")

Other information like hours, temperatures and wind speeds can be find the table in the almost same way.

hours=[str(stime.span.string) for stime in table.find_all("th",class_="value")]
temperatures=[str(tem.contents[0]) for tem in table.find_all("span",class_="units-value temperature-value temperature-value-unit-c")]
windspeeds=[str(speed.contents[0]) for speed in table.find_all("span",class_="units-value windspeed-value windspeed-value-unit-kph")]

For the wind directions and weather types, code is a little different, because they are stored in table cells and sometimes the cell is empty, so I use try and catch to get values.
Of course there are better ways to do this.

winddirections=[]
for direction in table.find("tr",class_="wind-direction").find_all("td"):
    try:
        winddirections.append(str(direction.abbr.string))
    except:
        winddirections.append("")
 
weathertypes=[]
for weathertype in table.find("tr",class_="weather-type").find_all("td"):
    try:
        weathertypes.append(str(weathertype.span.img['alt']))
    except:
        weathertypes.append("")

Finally put all the data on the output.
Here is all the code.
- Comments: 0

Thunderbird and addons

@09 Oct 2012 11:31 Tags: email

Thunderbird is the best email client on Windows and Linux (I have no Mac). With so many addons, it can be changed to anyway you like.

Download Thunderbird from its website website

Features:

  • easy to start: it has an email account setup wizard when you add new accounts, which means you do not have to add the server information manually. It is all done in a while.
  • Address book: store all your contacts in one place, and by the help of Zindus, it can be synced with Gmail contacts as well.
  • Calendar: using Lightning addon, thunderbird can have a calendar and what is more, with provider for google calendar, it can be synced with Google Calendar as well.
  • Manage multi accounts in one place: for those who have multi email accounts, you do not have to log in several times to check for new emails. And emails can be moved from one account to another in thunderbird— great for emails that have limited storage and have to be cleaned time to time.

Of course, if you have only one email and that is Gmail or outlook.com, stay online then, it is better. - Comments: 0

pymex - Matlab in Python in Matlab?

@10 Mar 2012 17:08 Tags: matlab python

Original source page at http://vader.cse.lehigh.edu/~perkins/pymex.html.

Introduction

From original website:

pymex allows the programmer to use Python in Matlab.
pymex embeds a Python interpreter in Matlab, allowing Matlab programmers write parts of their scripts in Python. Programmers are also able to use Python modules in Matlab. pymex aims to be easy to use, simply pass it a string containing Python script and it will evaluate it. A special matlab module is provided to move data in between Matlab and Python (with restrictions).

I made some changes to make it work on Windows.

How to compile

1. Ensure that python is installed on the computer (2.7 is recommended).
2. Copy the pymex.cpp into the Matlab path(or work folder). Change 'c:/Python27/include/Python.h' in the 42 line of pymex.cpp, to your python header file.

#include <c:/Python27/include/Python.h>

3. Start Matlab, type
mex -setup

to setup the complier for mex and select Microsoft Visual C++ (I only test it under VC++)
4. Type
mex pymex.cpp -lpython27 -Lc:\Python27\libs
to compile the file.(change 'c:\Python27\libs' to your python libs location)
5. Try out the examples in matlab.
disp('make some variables...')
x = 2.3
y = rand(4, 3, 2)
z = 'durp durp durp'

disp('import the matlab module')
pymex('import matlab')

disp('"pull" the variables from matlab to python')
pymex('x, y, z = matlab.pull("x", "y", "z")')

disp('print the variables as seen by python')
pymex('matlab.mex_print(x, y, z)')

disp('"push" the variables from python to matlab')
pymex('matlab.push("x2", x, "y2", y, "z2", z)')

disp('test that y is equal to y2')
y == y2

How to use

pymex both embeds and extends Python. The embedding part is strait forward, the Python interpreter is embedded in Matlab. The extending part is were things get interesting. pymex provides a special matlab module which only available when running Python inside of Matlab. This module allows the programmer to move data back and forth between Matlab and Python representations. The matlab module also lets programmers print to Matlab's command window from Python.

in Matlab

function: pymex
input parameter: cell of strings with each string a python command, or just a string
output: 1 for success and 0 for fail

in python

A module named 'matlab' has been added to the python evironment. Use 'import matlab' to import.

import matlab

Three methods are available in this module:
1. matlab.mex_print(…)
Print all arguments to the Matlab command window. Works similarly to Python's normal print function.
matlab.mex_print('hello', x, y, z)

2. matlab.pull(name1, name2, …) => var1, var2, …
Import Matlab variables, move Matlab arrays to Python sequences. For each variable name given as an argument, return the value of the variable in Matlab with that name, in the same order. The Matlab variable has "caller" scope. Uses Matlab's evalin function.
x, y, z = matlab.pull("x", "y", "z")

3. matlab.push(name1, var1, name2, var2, …)
Export Matlab variables, move Python sequences to Matlab arrays. For each variable name and value pair, create a Matlab variable with that name and value. The Matlab variable has "caller" scope. Uses Matlab's assignin function.
matlab.push("x", x, "y", y", "z", z)

Download

license.txt
pymex.cpp
sample.m - Comments: 1

page 1 of 41234next »
除非特别注明,本页内容采用以下授权方式: Creative Commons Attribution-ShareAlike 3.0 License