Skip to content

Instantly share code, notes, and snippets.

@Quard
Quard / dev_smtpd.py
Created April 15, 2014 06:36
SMTP server on python for development with storing emails.
#!/usr/bin/env python
import argparse
import asyncore
import smtpd
import time
import os
SMTPD_MAIN_CLASSES = [
smtpd.DebuggingServer.__name__,
@Quard
Quard / gist:1766187
Created February 8, 2012 06:48
Widget for upload file/image by selecting from local disk or from URL
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
@Quard
Quard / gist:3720255
Created September 14, 2012 06:43
en<->ru letters replacer
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_
@Quard
Quard / date_range.py
Created August 15, 2013 08:04
simple date range iterator
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)
@Quard
Quard / celery_retries_example.py
Created April 10, 2019 12:54
The Python Celery Cookbook: Small Tool, Big Possibilities
@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,
@Quard
Quard / get_task_result_in_view_example.py
Created April 10, 2019 12:56
The Python Celery Cookbook: Small Tool, Big Possibilities
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
@Quard
Quard / postponed_task_example.py
Created April 10, 2019 12:57
The Python Celery Cookbook: Small Tool, Big Possibilities
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', {}),
@Quard
Quard / primary_celery_example_use.py
Created April 10, 2019 13:16
The Python Celery Cookbook: Small Tool, Big Possibilities
send_mail_task.delay(('noreply@example.com', ), 'Celery cookbook test', 'test', {})
@Quard
Quard / queues_config_example.py
Created April 10, 2019 13:16
The Python Celery Cookbook: Small Tool, Big Possibilities
CELERY_TASK_ROUTES = {
'myproject.apps.mail.tasks.send_mail_task': {'queue': 'mail', },
}
@Quard
Quard / run_queues_example.sh
Created April 10, 2019 13:17
The Python Celery Cookbook: Small Tool, Big Possibilities
celery -A myproject worker -l info -Q celery
celery -A myproject worker -l info -Q mail