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 requests | |
class QuotesAPIClient: | |
def __init__(self, baseurl, session): | |
self._baseurl = baseurl | |
self._session = session | |
@classmethod | |
def default(cls, api_token): | |
session = requests.Session() |
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 collections | |
from twisted.internet import protocol | |
class LineParser(object): | |
def __init__(self, delimiter, max_length): | |
self.delimiter = delimiter | |
self.max_length = max_length | |
self._buffer = b'' |
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 six | |
class TypedField(object): | |
def __init__(self, classinfo): | |
self._classinfo = classinfo | |
self.fieldname = None # Set by the metaclass | |
def __get__(self, instance, owner): | |
try: |
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
web: twistd -n web -p $PORT --class=miscserver.resource |
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 asyncio | |
import itertools | |
import aiohttp | |
import async_timeout | |
async def fetch_with_response_delay(session, delay): | |
if not 0 <= delay <= 10: | |
raise ValueError('Delay must be between 0 and 10 inclusive') |
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 sqlite3 | |
conn = sqlite3.connect(':memory:') | |
conn.execute('CREATE TABLE user (user_id INTEGER PRIMARY KEY, name TEXT)') | |
conn.commit() | |
cursor = conn.cursor() | |
for name in ['alice', 'bob', 'carol', 'david']: | |
cursor.execute('INSERT INTO user (name) VALUES (?)', (name,)) |
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 | |
def default_diceroller(): | |
return random.randint(1, 6) | |
class CrapsGame(object): | |
_diceroller = default_diceroller | |
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
# Original | |
def process_item(self, item, spider): | |
try: | |
with self.conn: | |
query = 'INSERT INTO articles VALUES (?, ?, ?, ?, ?,(CURRENT_TIMESTAMP))' | |
try: | |
args = (item['url'], item['title'], item['intro'], item['body'], item['publication_date']) | |
except KeyError as kerr: | |
if kerr.args[0] == 'intro': | |
args = (item['url'], item['title'], "", item['body'], item['publication_date']) |
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 sys | |
import itertools | |
import datetime | |
from twisted.internet import task, defer | |
from twisted.python import log | |
import treq | |
def main(reactor, urls, chunksize): |
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
def investigate_loop() | |
i = 0 | |
numbers = [] | |
while i < 6: | |
print "At the top i is %d" % i | |
numbers.append(i) | |
i = i + 1 | |
print "Numbers now: ", numbers |
NewerOlder