Skip to content

Instantly share code, notes, and snippets.

View scardine's full-sized avatar

Paulo Scardine scardine

View GitHub Profile
@scardine
scardine / project_name.conf
Last active December 21, 2015 15:29
Django + FCGI + Virtualenv on SUSE/RH. I use Ubuntu, but some customers have annoying policies about linux distros. Can't run Python 2.7 with mod_wsgi because the customer already has another python app using 2.6; my solution was resort to FCGI. Kind of like it, performance is not bad at all.
<VirtualHost *:80>
ServerAdmin admin@project_name.com
ServerName project_name.com
ServerAlias www.project_name.com
# DocumentRoot: not the django project root, just to be safe
DocumentRoot /var/app/project_name/fcgi
# Logs
ErrorLog /var/log/apache2/project_name-error_log
@scardine
scardine / topojson.py
Created August 23, 2013 14:39
Experiments trying to implement topojson in Python using shapely.
from shapely.geometry import shape, Point, MultiPoint
import math
import json
def get_bounds(geometries):
"""Returns bounding box of geometries. Implementation creates a MultiPoint
from the boxes of all polygons in order to get the result"""
points = []
for g in geometries:
@scardine
scardine / check_django_password.php
Last active December 16, 2015 22:49
Check django passwords in PHP (django-1.3)
function check_django_password($plaintext, $hashed) {
$parts = explode('$', $hashed);
if(count($parts) != 3) return FALSE;
if($parts[0] == 'sha1' && sha1($parts[1] . $plaintext)) == $parts[2])
return TRUE;
if($parts[0] == 'md5' && md5($parts[1] . $plaintext)) == $parts[2])
return TRUE;
return FALSE;
}
@scardine
scardine / download_certs.py
Created April 22, 2013 21:19
Download dos certificados Raiz da autoridade certificadora brasileira.
import sh
import os
import re
from path import path
home = os.environ['HOME']
p = path.joinpath(home, 'Documents', 'pki', 'nfe', 'certs')
if not p.isdir():
p.makedirs()
@scardine
scardine / quebra_linhas.py
Created April 17, 2013 20:29
Algorit. para quebrar linhas dada uma largura.
def quebra_linha(frase, largura):
linha = ""
linhas = []
for palavra in frase.split():
largura_atual = text_extents(linha + palavra)
if largura_atual > largura:
if not linha:
raise Exception('Frase não cabe nesta largura')
linhas.append(linha)
linha = palavra
@scardine
scardine / gist:5269738
Created March 29, 2013 09:15
Exemplo de pycairo.
import cairo
pol = 72.0
mm = pol / 25.4
surface = cairo.PDFSurface('teste.pdf', 210*mm, 297*mm)
ctx = cairo.Context(surface)
ctx.set_source_rgb (0, 0, 0)
@scardine
scardine / validate_cnpj.py
Created November 27, 2012 00:12
CNPJ (Brazilian tax id) format validation
#-------------------------------------------------------------------------------
# Name: validate_cnpj
# Purpose: check formal validity of a cnpj number (brazilian tax id)
# this only checks if the digits match, not if the number is
# valid at Receita Federal (Brazilian IRS)
#
# Author: Paulo Scardine <[email protected]>
#
# Created: 26/11/2012
# Copyright: (c) PauloS 2012
@scardine
scardine / marker.php
Created November 1, 2012 21:17
Generate a marker with a given background and foreground colour.
<?php
/* .htaccess
RewriteRule markers?/([0-9a-fA-F]{6})/([0-9a-fA-F]{6})/([^/]+)/marker.png markers/index.php?bg=$1&fg=$2&icon=$3
*/
header('Content-type: image/png');
// read parameters: icon file, foreground and background colors
$bgc = sscanf(empty($_GET['bg']) ? 'FFFFFF' : $_GET['bg'], '%2x%2x%2x');
$fgc = sscanf(empty($_GET['fg']) ? '000000' : $_GET['fg'], '%2x%2x%2x');
@scardine
scardine / PuTTY-Solarized.reg
Created October 1, 2012 16:22
Putty-Solarized
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\Default%20Settings]
"Colour0"="101,123,131"
"Colour1"="88,110,117"
"Colour2"="253,246,227"
"Colour3"="238,232,213"
"Colour4"="238,232,213"
"Colour5"="101,123,131"
"Colour6"="7,54,66"
@scardine
scardine / urllib2_with_cookies.py
Created September 25, 2012 16:47
Exemplo de URLLib2 com cookies
import urllib2, cookielib
url = 'https://www2.itm.tur.br/th/default.aspx'
txheaders = {'User-agent' : 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
req = urllib2.Request(url, None, txheaders)