This file contains hidden or 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 lxml import etree | |
| def xpath(response): | |
| response.xpath = etree.HTML(response.content).xpath | |
| return response | |
| if __name__ == "__main__": | |
| import requests | |
| r = requests.get('http://example.com', hooks=dict(response=xpath)) | |
| r.xpath('//h1/text()') |
This file contains hidden or 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 redis | |
| from redis import WatchError | |
| import time | |
| RATE = 0.1 | |
| DEFAULT = 100 | |
| TIMEOUT = 60 * 60 | |
| DEBUG = False |
This file contains hidden or 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
| def elo_ranking(winners_rank, losers_rank, k=20): | |
| expected_winner = 1 / (1 + 10 ** ((losers_rank - winners_rank) / 400)) | |
| expected_loser = 1 / (1 + 10 ** ((winners_rank - losers_rank) / 400)) | |
| winners_rank += k * (1 - expected_winner) | |
| losers_rank += k * (0 - expected_loser) | |
| return winners_rank, losers_rank |
This file contains hidden or 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
| // Set the aliases for the libraries used in the application | |
| require.config({ | |
| paths: { | |
| jquery: 'libs/jquery-min', | |
| underscore: 'libs/underscore-min', | |
| backbone: 'libs/backbone-min', | |
| request: 'libs/XMLHttpRequest', | |
| mustache: 'libs/mustache-min', | |
| templates: '../templates', | |
| collapse: 'libs/bootstrap-collapse', |
This file contains hidden or 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 PopulableForm(forms.Form): | |
| ... def populate(self, other): | |
| ... for k, v in self.cleaned_data.items(): | |
| ... if hasattr(other, k): | |
| ... setattr(other, k, v) | |
| ... else: | |
| ... try: | |
| ... other[k] = v | |
| ... except TypeError: | |
| ... continue |
This file contains hidden or 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 flask import Flask | |
| app = Flask(__name__) | |
| app.config['SERVER_NAME'] = 'flasksubs.local:5000' | |
| app.config['SUB_DOMAINS'] = { | |
| 'good': 'good sub domain', | |
| 'ok': 'ok sub domain' | |
| } | |
| app.config['NO_SUB_DOMAIN_MSG'] = 'no sub domain found' |
This file contains hidden or 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 operator import methodcaller | |
| >>> from random import randint | |
| >>> class Foo(object): | |
| ... def __init__(self): | |
| ... self.number = randint(0, 10) | |
| ... def get_number(self): | |
| ... return self.number | |
| ... | |
| >>> lfoos = [Foo() for i in xrange(10)] | |
| >>> for foo in lfoos: |
This file contains hidden or 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
| var Game = (function() { | |
| var settings = { | |
| // payout which is a multiple. | |
| payout: 2, | |
| // probability of getting a payout represented fraction. | |
| probability: .50, | |
| }; | |
| var init = function(options) { | |
| for (var key in options) { |
This file contains hidden or 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 importlib | |
| IMPORT_CACHE = {} | |
| def cache_import(path): | |
| """Import an object from a specified `path`.""" | |
| mod, imp = path.rsplit('.', 1) | |
| if mod in IMPORT_CACHE: | |
| return getattr(IMPORT_CACHE[mod], imp) | |
| mod_ = importlib.import_module(mod) |
This file contains hidden or 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
| %load_ext autoreload | |
| %autoreload 2 |
OlderNewer