Skip to content

Instantly share code, notes, and snippets.

View rturowicz's full-sized avatar

Rafal Turowicz rturowicz

  • Poland
View GitHub Profile
@rturowicz
rturowicz / des.py
Last active December 15, 2015 07:59
data ecryption (des)
from Crypto.Cipher import DES
crypt_key = 'secret'
def crypt(what):
k = DES.new(crypt_key, DES.MODE_ECB)
return k.encrypt(what.zfill(32))
def decrypt(what):
@rturowicz
rturowicz / django_messages.py
Created March 23, 2013 14:35
django - messages
# settings.py
MIDDLEWARE_CLASSES = (
'django.contrib.messages.middleware.MessageMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
@rturowicz
rturowicz / django_managers.py
Last active December 15, 2015 07:59
django - multiple / custom managers
# managers.py
class ModelClassApprovedOnlyManager(models.Manager):
def get_query_set():
self.get_query_set().filter(is_approved=True)
# models.py
class ModelClass(models.Model):
# ...
all_objects = models.Manager() # first manager used in admin
objects = ModelClassApprovedOnlyManager() # second manager used by "objects"
@rturowicz
rturowicz / django_pdf.py
Last active December 15, 2015 07:59
django trml2pdf - create pdf from template
from django.utils.encoding import smart_str
import trml2pdf
filename = "advert-%s.pdf" % id
response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
t = loader.get_template('print/pdf/advert.rml')
@rturowicz
rturowicz / nntp.py
Created March 24, 2013 13:29
usenet
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import nntplib
import string
from email.parser import Parser
SERVER = "news.home.net.pl"
GROUP = "pl.comp.lang.python"
@rturowicz
rturowicz / substr.sql
Last active December 15, 2015 08:38
postgresql: tsearch2 indexed substrings
alter table towar add ftssubnpelna tsvector;
CREATE INDEX towar_ftssubnpelna_idx ON towar USING gin(ftssubnpelna);
CREATE or replace FUNCTION text_to_substrings(tekst text) RETURNS text AS $$
substrings = []
for tmp in tekst.split():
for i in range(len(tmp)):
if i >= 3:
substrings.append(tmp[0:i])
return ' '.join(substrings)
@rturowicz
rturowicz / rules.sql
Last active December 15, 2015 08:59
postgresql: view rules (some kind of "live views")
CREATE TABLE users (
id int not null,
email varchar(255) NOT NULL,
pass varchar(255) NOT NULL,
nick varchar(128) NOT NULL,
PRIMARY KEY(id)
);
CREATE OR REPLACE FUNCTION dodaj_usr (int, varchar) RETURNS boolean AS
'
#!/bin/bash
# +----------------------------------------------------------------------+
# | |
# | Mount the root file system / with the option noatime |
# | |
# | By Philipp Klaus <http://blog.philippklaus.de> |
# | Tip found on <http://blogs.nullvision.com/?p=275> |
# | |
# +----------------------------------------------------------------------+
@rturowicz
rturowicz / utf8.py
Last active December 15, 2015 19:49
read / write utf-8 files
import codecs
infile = codecs.open("file", "r", "utf-8")
line = infile.readline()
while line:
print line
@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]()