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
import re | |
import urllib | |
def uca_sort(l, locale='en', _re_nums=re.compile('<tt class="count">(\d+):</tt>')): | |
"""Uses the ICU demo site to sort a list""" | |
url = 'http://demo.icu-project.org/icu-bin/locexp' | |
url += '?_=' + locale + '&d_=en&x=col' | |
data = 'str=' + '\n'.join(l) | |
html = urllib.urlopen(url, data).read() | |
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
from xml.etree import ElementTree | |
tree = ElementTree.fromstring( | |
"""<?xml version="1.0"?> | |
<result> | |
<mediaid>[MediaID]</mediaid> | |
<source>[SourceFile]</source> | |
<status>[MediaStatus]</status> | |
<description>[ ErrorDescription]</description> <!-- Only in case of Status = Error --> | |
<format> |
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
import urllib | |
import re | |
import uuid | |
from django.utils import simplejson | |
def google_translate(text, source='en', target='fr'): | |
subs = {} | |
for arg in re.findall('(\{.+?\})', text): |
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
for i in range(0, len(x)): | |
for j in range(0, len(x)): | |
indice = i * len(x) + j | |
value = x[i] ** (len(x) - j) | |
M.put(indice, value) |
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
class Topic(BaseModel): | |
author = db.ReferenceProperty(User, required=True, collection_name='topics') | |
title = db.StringProperty() | |
body = db.TextProperty() | |
@classmethod | |
def topics(cls): | |
topics = cls.all().order('-updated').fetch(100) | |
prefetch_refprop(topics, cls.author) | |
return topics |
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
f = open('/path/csv.txt', 'r') | |
entities = [] | |
for line in f: | |
arg1, arg2, arg3 = line.split(',') | |
data = models.SomeData(arg1=arg1, arg2=arg2, arg3=arg3) | |
entities.append(data) | |
if len(entities) > 40: | |
db.put(entities) | |
entities = [] |
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
### app.yaml ### | |
- url: /upload | |
script: main.py | |
login: admin | |
### views.py ### | |
class BlobstoreUpload(BaseHandler): | |
def post(self): | |
key = self.get_argument('key') |
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
class Votable(BaseModel): | |
points = db.IntegerProperty(default=1) | |
score = db.FloatProperty() | |
up_votes = db.StringListProperty() # contains user key_names | |
down_votes = db.StringListProperty() # contains user key_names | |
def update_score(self): | |
"""Adapted from reddit's algorithm | |
http://code.reddit.com/browser/r2/r2/lib/db/sorts.py?rev=4778b17e939e119417cc5ec25b82c4e9a65621b2 | |
""" |
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
<html> | |
<body> | |
<a href="javascript: | |
(function() { | |
var el=document.createElement('div'), | |
b=document.getElementsByTagName('body')[0], | |
otherlib=false, | |
msg=''; |
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
import timeit | |
t = timeit.Timer(stmt="""\ | |
def test(pwd, n_iter): | |
for i in range(n_iter): | |
pwd = hashlib.sha1(pwd).hexdigest() | |
test('hello', 50000) | |
""", setup='import hashlib') | |
print t.timeit(100) / 100 |