Skip to content

Instantly share code, notes, and snippets.

View pybites's full-sized avatar

Pybites pybites

View GitHub Profile
import concurrent.futures
import os
import re
from timeit import timeit
import requests
from tqdm import tqdm
URLS = 'urls'
# cars.py
from datetime import date
class Car:
def __init__(self, model, year):
self.model = model
self.year = year
self._mileage = []
# from Fluent Python 2nd ed
class Averager():
def __init__(self):
self.series = []
def __call__(self, new_value):
self.series.append(new_value)
total = sum(self.series)
#!/usr/bin/env python3.9
import argparse
import importlib
import inspect
import pydoc
def get_callable(arg):
module_str, name = arg.rsplit(".", 1)
module = importlib.import_module(module_str)
>>> from datetime import datetime
>>> def today(dt=datetime.now()):
... print(dt)
...
>>> today()
2020-12-12 22:18:26.432268
# oops
>>> today()
2020-12-12 22:18:26.432268
>>> today()
import os
filename = 'homework.txt'
if not os.path.exists(filename):
with open(filename, 'w'): pass
answer1 = """1).
X | 3 | X
____|____|____
from importlib import import_module
from keyword import kwlist
import builtins
from typing import Dict, List
scores = {
"builtin": 1,
"keyword": 2,
"module": 3,
}
import asyncio
import os
import aiofiles
import aiohttp
# saved links from https://pybit.es/archives in urls
URLS = [u.rstrip() for u in open('urls', 'r').readlines()]
async def fetch(session, url):
"""Script to retrieve new titles from O'Reilly Media (formerly Safari Books Online)"""
from collections import namedtuple
from pathlib import Path
from datetime import datetime, timedelta
from urllib.request import urlretrieve
from xml.etree.ElementTree import parse
RSS_FEED = "https://www.oreilly.com/feeds/recently-added.rss"
NOW = datetime.now()
DT_FMT = "%a, %d %b %Y %H:%M:%S"
import cProfile
from functools import wraps
from pstats import Stats, SortKey
from time import time
def timing(f):
"""A simple timer decorator"""
@wraps(f)
def wrapper(*args, **kwargs):