forked from mattgiguere/WeatherGame
/
pyToParse.py
214 lines (137 loc) · 6.27 KB
/
pyToParse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- encoding: utf-8 -*-
# Made by http://www.elmalabarista.com
# This will teach about how use parse.com as your database
# About
# parse.com is a SAAS that provide a ready-to-use NOSQL backend
# and related services, great for quick prototypes. Also, can be used
# from several plataforms and languages
# Official documentation
# https://parse.com/docs/
import os, sys
from utils import printTitle, printSubTitle, printExplain, printTab, printError
printTitle("Parse.com is a NOSQL in the cloud")
printExplain("You need to create a account at parse.com. Is free")
print "https://parse.com/#signup"
printExplain("And create a sample APP")
print "https://parse.com/apps/new"
printExplain("Change the values below to the ones supplied by Parse for your application")
# Are in the 'settings' tab of each app
APPLICATION_ID = "aOmfYWxfdaqrD9aMOtp7a3UinrfOAqNMyVxIjLzm"
REST_API_KEY = "AkHGq6xQCd67e8Tj9xcXY3PirvzZstur7DBaURuX"
if APPLICATION_ID == "APPLICATION_ID_HERE":
printTitle("You need to create a parse app and supply the auth values")
sys.exit(-1)
printExplain("We will use the ParsePy library")
# Install with pip install git+https://github.com/dgrtwo/ParsePy.git
from parse_rest.connection import register, ParseBatcher
# Alias the Object type to make clear is not a normal python Object
from parse_rest.datatypes import Object as ParseObject
printSubTitle("First register the app")
register(APPLICATION_ID, REST_API_KEY)
printSubTitle("Parse is a NOSQL database.")
# https://en.wikipedia.org/wiki/NoSQL
printExplain("So, you not need to pre-create the data schema, and can drop/add data & columns at will")
anyObject = ParseObject()
printExplain("Simple set a value to the object. No need to exist before..")
anyObject.title = 'Hello world'
anyObject.score = 100
def saveToParse(anyObject):
print "Saving..."
anyObject.save()
print "Done!"
saveToParse(anyObject)
printExplain("Parse automatically add the nexts read-only fields on save:")
printSubTitle("objectId")
printExplain("The objectId is the primary-key of the objects. Is the way to identify this object")
print "The objectId is: ", anyObject.objectId
printSubTitle("objectId")
printExplain("The objectId is the primary-key of the objects. Is the way to identify this object")
print "The objectId is: ", anyObject.objectId
printSubTitle("createdAt")
printExplain("The createdAt is the date (UTC) when this object was created")
print "The object was created on: ", anyObject.createdAt
printSubTitle("updatedAt")
printExplain("The updatedAt is the last date (UTC) the object was modified")
print "The object last update on: ", anyObject.updatedAt
print "When the object is created, both dates are the same"
printExplain('You can add new fields anytime')
anyObject.otherField = True
anyObject.score = 200
saveToParse(anyObject)
print "The object last update on: ", anyObject.updatedAt
printExplain("In the parse.com Data Browser, you see that is created a table called 'Object'")
print "To have a better name, you need to subclass"
printExplain("If you run this several times, the object will be duplicated")
print """
In contrast with Sql databases, you need to code the constrains/validations yourself"
And check if a object will duplicate or not the data before to save. Parse.com not
have built-in functionality to constrain the data to avoid duplicated objects or anything
related at all. Only provide the storage, and the default fields
"""
printTitle("Query the data store")
printSubTitle("Query for exact object")
print "You ask for a exact object, you need to query by objectId"
findObject = ParseObject.Query.get(objectId=anyObject.objectId)
print "The object with objectId = ", anyObject.objectId, ' exist? ', not(findObject is None)
printSubTitle("To get all the objects, use all()")
# The queryset return a generator. I need to convert to list to count it
# This is not the best way. Ask only for the data you really need
print "Exist %d objects now " % len(list(ParseObject.Query.all()))
printSubTitle("Like Django, Querysets can have constraints added by appending the name of the filter operator")
print "The list of constrains is at https://www.parse.com/docs/rest#queries-constraints"
print "Objects with score>=100 ", len(list(ParseObject.Query.filter(score__gte=100)))
printTitle("A simple news app")
printExplain("Let's build some classes to store news")
class Source(ParseObject):
pass
class Article(ParseObject):
pass
printExplain("Get the news from the yahoo rss feed.")
# Feedparser is the popular option for get RSS/Atom feeds
# http://pythonhosted.org/feedparser/
import feedparser
d = feedparser.parse('http://news.yahoo.com/rss/')
printSubTitle('Downloading news from %s' % d.feed.title)
print d.feed.link
printExplain("To model a one-t-many relation, create a Source object and point it to each article")
printExplain("First get the sources that are already created")
sourcesQry = Source.Query.all()
sources = {source.href: source for source in sourcesQry}
def createSource(title, href):
if href in sources:
return sources[href]
source = Source(**locals())
saveToParse(source)
sources[source.href] = source
return source
def createArticle(title, description, source, date):
article = Article(**locals())
return article
articles = []
for entry in d.entries:
# Is a real new with source? The rrs return images and other stuff
if 'source' in entry:
source = createSource(**entry.source)
articles.append(createArticle(
title=entry.title,
description=entry.description,
source=source,
date=entry.published
))
printExplain("To save several objects, use the batcher")
batcher = ParseBatcher()
batcher.batch_save(articles)
print "Our news sources:"
for source in sources.values():
printTab(source.title)
print "The news from ", sources.values()[0].title
for new in Article.Query.filter(source=sources.values()[0]):
printSubTitle(new.title)
print new.description
printTitle("Conclusion")
print """
Parse.com provide a easy way of store/query data, with not admin skills.
Is not a replacemente for a proper Sql database (like postgresql or sql server)
but provide a flexible data model apropiated for quick development and/or scalable
acces to data, where the data must be denormalized anyway..
"""