Skip to content

Instantly share code, notes, and snippets.

View rturowicz's full-sized avatar

Rafal Turowicz rturowicz

  • Poland
View GitHub Profile
@rturowicz
rturowicz / django_email.py
Last active May 14, 2021 20:01
django - send email
from django.http import HttpResponse
from django.core.mail import send_mail
from django.template import Context
from django.template import loader
from socket import error as SocketError
subject = 'subject'
c = Context({
'from': 'user@host.com',
@rturowicz
rturowicz / enginx_zf.conf
Last active March 3, 2021 19:30
vhost - nginx (ZF)
# appserver
server {
listen 80;
server_name zftemplate.desktop;
access_log /var/log/nginx/zftemplate.desktop.access.log;
error_log /var/log/nginx/zftemplate.desktop.error.log;
root /home/user/WorkspacePHP/zftemplate/public;
rewrite ^/([0-9]*)/css/(.*) /css/$2;
rewrite ^/([0-9]*)/js/(.*) /js/$2;
@rturowicz
rturowicz / plpythonu.sql
Last active February 2, 2019 20:28
postgresql: example use of python procedural language
-- query with stored plan
CREATE or replace FUNCTION pybench1(id int) RETURNS text AS '
if (SD.has_key("plan")):
plan = SD["plan"]
else:
plan = plpy.prepare("SELECT * FROM pagetimer pt, pagebrowser pb WHERE pt.idtimer = $1 and pt.idtimer = pb.idtimer", ["int4"])
SD["plan"] = plan
rec = plpy.execute(plan, [id])
if (rec.nrows() > 0):
@rturowicz
rturowicz / page.py
Last active December 19, 2018 11:50
django - intermediate admin page
# admin.py: admin action definition
def make_copy(self, request, queryset):
form = None
if 'apply' in request.POST:
form = CopyPageForm(request.POST)
if form.is_valid():
issue = form.cleaned_data['issue']
# send to rabbitmq (celery task)
import pika
import simplejson
credentials = pika.credentials.PlainCredentials('user', 'pass')
connection = pika.BlockingConnection(
pika.ConnectionParameters(
host='localhost',
virtual_host='/vhost_name',
credentials=credentials

tmux shortcuts & cheatsheet

start new:

tmux

start new with session name:

tmux new -s myname
@rturowicz
rturowicz / hashes.py
Last active December 23, 2015 12:18
compare dictionaries content
from hashlib import md5
from cPickle import dumps
def make_hashes(data):
ret = [md5(dumps(data)).hexdigest()]
ret.extend([
md5(dumps(v)).hexdigest() for v in data.values()
])
@rturowicz
rturowicz / mysql_functions.sql
Last active December 17, 2015 14:29
postgresql: mysql functions
CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITHOUT TIME ZONE)
RETURNS BIGINT
LANGUAGE SQL
IMMUTABLE STRICT
AS 'SELECT EXTRACT(EPOCH FROM $1)::bigint;';
CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(TIMESTAMP WITH TIME ZONE)
RETURNS BIGINT
LANGUAGE SQL
IMMUTABLE STRICT
@rturowicz
rturowicz / scaled.py
Last active December 16, 2015 17:41
django - ImageField scaled-down on the fly
# models.py: overwritten model save method
def save(self, *args, **kwargs):
if self.cover:
img_string = StringIO()
pil_image = Image.open(self.cover.file)
if pil_image:
res_image = pil_image.resize((1034, 1400))
res_image.save(img_string, pil_image.format)
# original image replaced by thumbnail
self.cover.file = InMemoryUploadedFile(
@rturowicz
rturowicz / switch.py
Created April 5, 2013 16:44
switch implementation
'''switch implementation'''
def a(what):
print what
def switch(what):
try:
{'1' : lambda : a('one'),
'2' : lambda : a('two')
}[what]()