View gist:1766187
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 urlparse | |
import httplib | |
from copy import copy | |
try: | |
from cStringIO import StringIO | |
except: | |
from StringIO import StringIO | |
from django import forms | |
from django.core.files import File | |
from django.core.files.temp import NamedTemporaryFile |
View gist:3720255
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 | |
from copy import copy | |
_map = {':': u'\u0416', "'": u'\u044d', '"': u'\u042d', '{': u'\u0425', '[': u'\u0445', '.': u'\u044e', ']': u'\u044a', ',': u'\u0431', '^': ',', 'a': u'\u0444', '&': '.', 'c': u'\u0441', 'b': u'\u0438', 'e': u'\u0443', 'd': u'\u0432', 'g': u'\u043f', 'f': u'\u0430', 'i': u'\u0448', 'h': u'\u0440', 'k': u'\u043b', 'j': u'\u043e', 'm': '\xd1\x8c', 'l': u'\u0434', 'o': u'\u0449', 'n': u'\u0442', 'q': u'\u0439', 'p': u'\u0437', 's': u'\u044b', 'r': u'\u043a', 'u': u'\u0433', 't': u'\u0435', 'w': u'\u0446', 'v': u'\u043c', 'y': u'\u043d', 'x': u'\u0447', ';': u'\u0436', 'z': u'\u044f', '}': u'\u042a', '<': u'\u0411', '>': u'\u042e'} | |
letters_map = copy(_map) | |
letters_map.update(dict([(k.upper(), v.upper()) for k, v in _map.iteritems() if k.upper() != k and not k in letters_map])) | |
letters_map.update(dict([(k, v) for v, k in _map.iteritems() if not k in letters_map])) | |
letters_map.update(dict([(k.upper(), v.upper()) for v, k in _map.iteritems() if k.upper() != k and not k in letters_ |
View date_range.py
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, timedelta | |
class date_range(object): | |
def __init__(self, since, until, step=timedelta(days=1)): | |
assert isinstance(since, (datetime, date)) | |
assert isinstance(until, (datetime, date)) | |
assert isinstance(step, timedelta) |
View dev_smtpd.py
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
#!/usr/bin/env python | |
import argparse | |
import asyncore | |
import smtpd | |
import time | |
import os | |
SMTPD_MAIN_CLASSES = [ | |
smtpd.DebuggingServer.__name__, |
View beat_config_example.py
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 celery.schedules import crontab | |
CELERY_BEAT_SCHEDULE = { | |
'monday-statistics-email': { | |
'task': 'myproject.apps.statistics.tasks.monday_email', | |
'schedule': crontab(day_of_week=1, hour=7), | |
}, | |
} |
View celery_retries_example.py
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
@celery_app.task(bind=True, default_retry_delay=10 * 60) | |
def send_mail_task(self, recipients, subject, template, context): | |
message = render_template(f'{template}.txt', context) | |
html_message = render_template(f'{template}.html', context) | |
try: | |
send_mail( | |
subject=subject, | |
message=message, | |
from_email=settings.DEFAULT_FROM_EMAIL, | |
recipient_list=recipients, |
View get_task_result_example.py
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 celery import group | |
@celery_app.task | |
def calculate_service_provider_task(user_id, provider_id): | |
user = User.objects.get(pk=user_id) | |
provider = ServiceProvider.objects.get(pk=provider_id) | |
return calculate_service_provider(user, provider) |
View get_task_result_in_view_example.py
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 view(request): | |
find_job = find_best_service_provider_for_user.delay(request.user.pk) | |
# do other stuff | |
calculations_results = find_job.get().join() | |
# process calculations_results and send response |
View long_running_task_example.py
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
@celery_app.task | |
def send_good_morning_mail_task(offset=0, limit=100): | |
users = User.objects.filter(is_active=True).order_by('id')[offset:offset + limit] | |
for user in users: | |
send_good_morning_mail(user) | |
if len(users) >= limit: | |
send_good_morning_mail_task.delay(offset + limit, limit) |
View postponed_task_example.py
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 | |
send_mail_task.apply_async( | |
(('noreply@example.com', ), 'Celery cookbook test', 'test', {}), | |
countdown=15 * 60 | |
) | |
send_mail_task.apply_async( | |
(('noreply@example.com', ), 'Celery cookbook test', 'test', {}), |
OlderNewer