Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / beat_config_example.py
Last active April 10, 2019 21:26
The Python Celery Cookbook: Small Tool, Big Possibilities
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),
},
}
@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_example.py
Created April 10, 2019 12:55
The Python Celery Cookbook: Small Tool, Big Possibilities
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)
@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 / long_running_task_example.py
Last active April 12, 2019 13:09
The Python Celery Cookbook: Small Tool, Big Possibilities
@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)
@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(
(('[email protected]', ), 'Celery cookbook test', 'test', {}),
countdown=15 * 60
)
send_mail_task.apply_async(
(('[email protected]', ), 'Celery cookbook test', 'test', {}),