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 Tuple(tuple): | |
def __getattr__(self, name): | |
def _int(val): | |
try: | |
return int(val) | |
except ValueError: | |
return False | |
if not name.startswith('_') or not _int(name[1:]): | |
raise AttributeError("'tuple' object has no attribute '%s'" % 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
# !pip install python-hn | |
import itertools | |
from hn import search_by_date | |
results = search_by_date('Who is hiring?', ask_hn=True, author='whoishiring', hits_per_page=1000) | |
who_is_hiring = (r for r in results if 'ask hn: who is hiring?' in r['title'].lower()) | |
for elem in itertools.islice(who_is_hiring, 10): | |
print(elem['title']) |
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
# !pip install python-hn | |
import itertools | |
from hn import search_by_date | |
results = search_by_date('Who is hiring?', ask_hn=True, author='whoishiring', hits_per_page=1000) | |
who_is_hiring = (r for r in results if 'ask hn: who is hiring?' in r['title'].lower()) | |
for elem in itertools.islice(who_is_hiring, 10): | |
print(elem['title']) |
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
{ | |
"category_name": "General", | |
"todos": [ | |
{ | |
"task": "My TODO Task", | |
"due_on": null, | |
"status": "pending", | |
"description": null | |
}, | |
{ |
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
""" | |
You find yourself doing this all the time? | |
>>> car = Car.objects.get(id=1) | |
>>> if c.user != request.user: | |
>>> return HttpResponse("This car doesn't belong to you") | |
>>> c.brand = request.POST.get('brand_name') | |
>>> c.save() | |
Better to turn it into: |
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
from concurrent.futures import ProcessPoolExecutor | |
class CheckOnlyIntegers: | |
def __init__(self, fn): | |
self.fn = fn | |
def __call__(self, *args): | |
if not all([type(arg) == int for arg in args]): | |
raise ValueError("Invalid param is not an integer") |
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 functools | |
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor | |
def check_only_integers(fn): | |
"""Very simple decorator that prevents other than integers to be passed | |
to the decorated function. Just for demonstration purposes. | |
""" | |
@functools.wraps(fn) | |
def wrapped(*args): |
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 get_repo_stars(org, repo): | |
url = 'https://api.github.com/repos/{org}/{repo}'.format( | |
org=org, repo=repo) | |
print("GET ", url) | |
resp = requests.get(url) | |
return resp.json()['stargazers_count'] | |
params = [ | |
('requests', 'requests'), | |
('requests', 'httpbin'), |
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
""" | |
This one "Doesn't" work. Seems like `map` doesn't play well with | |
`as_completed`. | |
""" | |
import time | |
import random | |
from concurrent.futures import ThreadPoolExecutor, as_completed | |
def my_sleep(n=1000): |
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
from datetime import datetime, date, time, timedelta | |
from pytz import timezone | |
START_DATE = date(2017, 10, 9) | |
START_TIME = time(8, 0) | |
START_DATETIME = datetime.combine(START_DATE, START_TIME) | |
INTERVAL_IN_HOURS = 2 | |
OPTION_TEMPLATE = "{start} - {end} ({tz})" |