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 logging | |
import os | |
import sys | |
import time | |
from google.appengine.ext import db | |
import instance_cache | |
import user_util |
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
""" | |
Based on cachepy.py by Juan Pablo Guereca with additional modifications | |
for thread safety and simplified to reduce time spent in critical areas. | |
Module which implements a per GAE instance data cache, similar to what | |
you can achieve with APC in PHP instances. | |
Each GAE instance caches the global scope, keeping the state of every | |
variable on the global scope. |
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 DataWriter(object): | |
"""Used by the QueryDrainerPipeline to coalesce intermediate results | |
into their final resting place. | |
""" | |
def write_metadata(self, metadata): | |
raise NotImplemented() | |
def write_result(self, data): | |
"""Writes all data.""" |
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 random | |
import logging | |
def map(data): | |
try: | |
# Generate a random key from 1..10 | |
key = random.randint(1, 10) | |
logging.info("%s %s", key, data) | |
yield (key, data) |
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 ContentRevisionsInputReader( | |
third_party.mapreduce.input_readers.InputReader): | |
"""Mapper that loads the latest commit's revisions from the datastore.""" | |
def __init__(self, kinds, start, step, current=0): | |
# List of content kinds to filter on | |
self._kinds = kinds | |
# Start index in snapshot array | |
self._start = start | |
# Stride through the snapshot array | |
self._step = step |
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 UserData(db.Model): | |
birthdate = db.DateProperty(indexed=False) | |
def compute_birthdate_str(self): | |
if self.birthdate: | |
return self.birthdate.isoformat() | |
return self.birthdate | |
# You will probably need to add this to index.yaml |
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
# Here's how I got this to work: | |
# 1. Download this client library into your GAE project: | |
# https://developers.google.com/api-client-library/python/start/installation#appengine | |
# https://code.google.com/p/google-api-python-client/downloads/detail?name=google-api-python-client-gae-1.2.zip&can=2&q= | |
# 2. Copy this file from the GAE SDK installed on your development machine | |
# google/appengine/tools/appengine_rpc_httplib2.py | |
# 3. Modify the import statements as necessary | |
# 4. Create a secrets.py file that defines a appcfg_refresh_token property | |
# 5. Obtain the refresh token by | |
# Calling appcfg.py list_versions . --oauth2, this will open a browser so you can login with your Google Account |
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 YouTubeQueryMasterPipeline(pipeline.Pipeline): | |
def run(self, user_ids): | |
"""Launches worker pipeline jobs to query the YouTube API. | |
Arguments: | |
user_ids: The user_ids of stored OAuth2 credentials. | |
""" | |
try: | |
for user_id in user_ids: |
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 MixPanel(object): | |
def get_page_view_funnel(self, content_urls): | |
# Build up the events array. Each "event" is a step in the funnel | |
events = [] | |
for cu in content_urls: | |
events.append({ | |
"event": "Page View", | |
"selector": 'properties["Page View Page"] == "%s"' % (cu,), | |
}) |
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
public function checkBuyOrders($currentBuyPrice) { | |
$sqlWhere = Array('status' => 'ACTIVE', 'action' => 'BUY', "$currentBuyPrice <= `at_price`"); | |
foreach ($this->context->orders->findAll()->where($sqlWhere) as $order) { //double check the price for each before buying | |
try { | |
$userAssociatedWithOrder = $this->context->authenticator->getUser($order->user_id); | |
if(!empty($order->at_price) && !empty($order->amount) && $userAssociatedWithOrder->coinbase_access_token){ //his Coinbase API tokens are set | |
$totalBuyPrice = $this->context->coinbase->user($order->user_id)->order($order)->getBuyPrice($order->amount); | |
if ($totalBuyPrice !== NULL && $totalBuyPrice->subtotal->amount <= $order->at_price * $order->amount) { | |
$result = $this->context->coinbase->user($order->user_id)->order($order)->buy($order->amount); //Buy the coins | |
$this->context->orders->findAll()->get($order->order_id)->update(Array('status' => 'EXECUTED')); //Update order status |
OlderNewer