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 unittest | |
| import mock # or `from unittest import mock` in py3.3+ | |
| import undertest1 | |
| import undertest2 | |
| class MockDemoTestCase(unittest.TestCase): | |
| # we can directly patch givestring's module namespace. |
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
| #!/usr/bin/env python | |
| """ | |
| Find failed login attempts in auth log, save results in a sqlite database, | |
| and print a report. | |
| """ | |
| from __future__ import print_function | |
| import os | |
| import re |
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 re | |
| from ConfigParser import NoOptionError | |
| class ValidationFailed(Exception): | |
| def __init__(self, reason): | |
| self.reason = reason | |
| def __str__(self): | |
| return repr(self) |
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 os | |
| class ProgressLineReader(object): | |
| def __init__(self, filepath, mode='r'): | |
| self.path = filepath | |
| self.total_size = os.stat(filepath).st_size | |
| self.already_read = 0 | |
| self.fobj = open(filepath, mode) | |
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 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 |
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 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 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
| # 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 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 random | |
| def default_diceroller(): | |
| return random.randint(1, 6) | |
| class CrapsGame(object): | |
| _diceroller = default_diceroller | |
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 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 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 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') |