This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8
import urllib
from xml.etree import ElementTree
from dateutil import parser as date_parser
def parse_app_feed():
free_url = "http://itunes.apple.com/jp/rss/topfreeapplications/limit=10/xml"
atom_ns = 'http://www.w3.org/2005/Atom'
itunes_ns = 'http://itunes.apple.com/rss'
xml_string = urllib.urlopen(free_url).read()
#xml = ElementTree(file=urllib.urlopen(free_url))
xml = ElementTree.fromstring(xml_string)
xml_updated = date_parser.parse(
xml.find('./{%s}updated' % atom_ns).text)
xml_uri = xml.find('./{%s}id' % atom_ns).text
for e in xml.findall('.//{%s}entry' % atom_ns):
uri = e.find('./{%s}id' % atom_ns).text
name = e.find('./{%s}name' % itunes_ns).text
title = e.find('./{%s}title' % atom_ns).text
category = e.find('./{%s}category' % atom_ns).attrib['term']
description = e.find('./{%s}summary' % atom_ns).text
release_at = date_parser.parse(
e.find('./{%s}releaseDate' % itunes_ns).text)
artist_name = e.find('./{%s}artist' % itunes_ns).text
artist_uri = e.find('./{%s}artist' % itunes_ns).attrib['href']
price = float(e.find('./{%s}price' % itunes_ns).attrib['amount'])
currency = e.find('./{%s}price' % itunes_ns).attrib['currency']
icon_uri = e.findall('./{%s}image' % itunes_ns)[-1].text
screen_uri = [_ent for _ent in e.findall('./{%s}link' % atom_ns) \
if 'image' in _ent.attrib['type']][0].text
print '%s\t%s\t%s' % (name, category, description[:10])
if __name__ == '__main__':
parse_app_feed()
Tapic Games FREE FOR A
! Games テレビ朝日系列「お願
Kozeni Lite Games コインをタップして消
整形マニア Entertainment iPhoneで美容整
Chariso(Bike Rider) Games ☆今だけ無料☆
Find My iPhone Utilities iPhone、iPa
ガイラルディア Games 無料で遊べる王道系の
ZOZOTOWN Lifestyle 日本最大級のファッシ
電卓少女 Entertainment 『電卓少女』で計算萌
Ringtone Maker - Make free ringtones from your music! Music ☆ iPodの中の曲
Today I installed R (vesion2.11.1 2010.5.31) from mac installer.
R is an enviroment for statistical computing and graphics.
Python has some packages for R like rpy and rpy2.
$ python Desktop/correlation_sample.py
Pearson's product-moment correlation
data: x and y
t = -1.4559, df = 8, p-value = 0.1835
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.8440680 0.2415684
sample estimates:
cor
-0.4576683
from fixture import DataSet
class EntryData(DataSet):
class great_monday:
title = "Monday Was Great"
body = """\
Monday was the best day ever. I got up (a little late, but that's OK) then I ground some coffee.
Mmmm ... coffee! I love coffee. Do you know about
<a href="http://www.metropoliscoffee.com/">Metropolis</a> coffee? It's amazing. Delicious.
I drank a beautiful cup of french pressed
<a href="http://www.metropoliscoffee.com/shop/coffee/blends.php">Spice Island</a>, had a shave
and went to work. What a day!
"""
class CommentData(DataSet):
class monday_liked_it:
entry = EntryData.great_monday
comment = """\
I'm so glad you have a blog because I want to know what you are doing everyday. Heh, that sounds
creepy. What I mean is it's so COOL that you had a great Monday. I like Mondays too.
"""
class monday_sucked:
entry = EntryData.great_monday
comment = """\
Are you serious? Mannnnnn, Monday really sucked.
"""
import sys
import os
import optparse
from fixture import GoogleDatastoreFixture
from fixture.style import NamedDataStyle
def main():
p = optparse.OptionParser(usage="%prog [options]")
default = "/tmp/dev_appserver.datastore"
p.add_option("--datastore_path", default=default, help=(
"Path to datastore file. This must match the value used for "
"the same option when running dev_appserver.py if you want to view the data. "
"Default: %s" % default))
default = "/tmp/dev_appserver.datastore.history"
p.add_option("--history_path", default=default, help=(
"Path to datastore history file. This doesn't need to match the one you use for "
"dev_appserver.py. Default: %s" % default))
default = "/usr/local/google_appengine"
p.add_option("--google_path", default=default, help=(
"Path to google module directory. Default: %s" % default))
(options, args) = p.parse_args()
if not os.path.exists(options.google_path):
p.error("Could not find google module path at %s. You'll need to specify the path" % options.google_path)
groot = options.google_path
sys.path.append(groot)
sys.path.append(os.path.join(groot, "lib/django"))
sys.path.append(os.path.join(groot, "lib/webob"))
sys.path.append(os.path.join(groot, "lib/yaml/lib"))
from google.appengine.tools import dev_appserver
import blog
from tests import datasets
config, explicit_matcher = dev_appserver.\
LoadAppConfig(os.path.dirname(__file__), {})
dev_appserver.SetupStubs(
config.application,
clear_datastore=False, # just removes the files when True
datastore_path=options.datastore_path,
history_path=options.history_path,
blobstore_path=None, # 追加 KeyError: 'blobstore_path' を避ける
login_url=None)
datafixture = GoogleDatastoreFixture(env={'EntryData': blog.Entry,
'CommentData': blog.Comment})
data = datafixture.data(datasets.CommentData, datasets.EntryData)
data.setup()
print "Data loaded into datastore %s" % \
(options.datastore_path or "[default]")
if __name__ == '__main__':
main()
tests/test_entries.py
#coding:utf-8
import unittest
from fixture import GoogleDatastoreFixture
from webtest import TestApp
import blog
from datasets import CommentData, EntryData
from django.utils import simplejson
datafixture = GoogleDatastoreFixture(env={'EntryData': blog.Entry,
'CommentData': blog.Comment})
class TestListEntries(unittest.TestCase):
def setUp(self):
self.app = TestApp(blog.application)
self.data = datafixture.data(CommentData, EntryData)
self.data.setup()
def tearDown(self):
self.data.teardown()
def test_entries(self):
response = self.app.get("/entries")
assert simplejson.dumps(EntryData.great_monday.title) in response
assert simplejson.dumps(EntryData.great_monday.body) in response
assert simplejson.dumps(CommentData.monday_liked_it.comment) \
in response
assert simplejson.dumps(CommentData.monday_sucked.comment) \
in response
$ nosetests -v --with-gae
test_entries (tests.test_entries.TestListEntries) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.296s
OK
A DataSet class is loaded via some storage medium, say, an object that implements a Data Mapper or Active Record pattern. A Fixture is an environment that knows how to load data using the right objects. Behind the scenes the rows and columns of the DataSet are simply passed to the storage medium so that it can save the data.
The Fixture class is designed to support many different types of databases and other storage media by hooking into 3rd party libraries that know how to work with that media. There is also a section later about creating your own Fixture.
There are a couple ways to test a database-backed application. You can create mock objects and concentrate entirely on unit testing individual components without testing the database layer itself, or you can simply load up sample data before you run a test. Thanks to sqlite in-memory connections, the latter may be more efficient than you think.
But it’s easy enough to insert data line by line in code, right? Or simply load a SQL file? Yes, but this has two major downsides: you often have to worry about and manage complex chains of foreign keys manually; and when referencing data values later on, you either have to copy / paste the values or pass around lots of variables.
The fixture module simplifies this by breaking the process down to two independent components:
DataSet
Defines sets of sample data
Fixture
Knows how to load data