Skip to content

Instantly share code, notes, and snippets.

View tkrajina's full-sized avatar
🏠
Working from home

Tomo Krajina tkrajina

🏠
Working from home
View GitHub Profile
# -*- coding: utf-8 -*-
import logging as mod_logging
import cartesius.main as mod_cartesius
import cartesius.charts as mod_charts
import cartesius.colors as mod_colors
import srtm as mod_srtm
import gpxpy.geo as mod_geo
mod_logging.basicConfig(level=mod_logging.DEBUG,
@tkrajina
tkrajina / pyctags.sh
Created April 15, 2014 04:44
ctags for python projects
ctags -R . $(python -c "import sys;print(' '.join(sys.path))")
@tkrajina
tkrajina / mysite.com
Created April 17, 2014 12:45
Minimal apache mod_wsgi config
<VirtualHost *:80>
ServerName mysite.com
#ServerAlias ...
ServerAdmin webmaster@mysite.com
DocumentRoot .../projects/mysite
#Alias /robots.txt .../projects/qreminder-on-email/static/robots.txt
#Alias /favicon.ico .../projects/qreminder-on-email/static/favicon.ico
@tkrajina
tkrajina / django_paginator_with_raw_query.py
Created April 25, 2014 18:35
Django paginator with RawQuerySet
# Dummy model:
class DbCounter(models.Model):
count = models.IntegerField()
def get_count(table, where):
# Check strings for sql injections here before doing this!
return DbCounter.objects.raw('select 1 as id, count(*) as count from %s where %s' % (table, where))[0].count
# View:
def index(request):
@tkrajina
tkrajina / extract_unicode_words.py
Created April 27, 2014 05:33
Extract unicode words from string
# -*- coding: utf-8 -*-
import unicodedata as mod_unicodedata
all_unicode = ''.join(unichr(i) for i in xrange(65536))
UNICODE_LETTERS = ''.join(c for c in all_unicode if mod_unicodedata.category(c)=='Lu' or mod_unicodedata.category(c)=='Ll')
UNICODE_LETTERS_AND_NUMBERS = '1234567890' + UNICODE_LETTERS
def extract_unicode_words(text, allowed_chars=None):
if text.__class__ != unicode:
@tkrajina
tkrajina / django_model_wordwrap.py
Created May 5, 2014 19:58
Automatically wordwrap django model string fields
import textwrap as mod_textwrap
import django.db.models.fields as mod_fields
def wordwrap_fields(model):
"""
When models are not created through forms (so standard form validation didn't prevent
too long strings), this can be used to automatically wordwrap.
"""
for field in model._meta.fields:
@tkrajina
tkrajina / setuplogging.py
Last active August 29, 2015 14:03
Setup logging
import logging
import logging.handlers
import os.path as path
def setup_logging(file_name, log_directory=None, stream=None, max_bytes=None,
backup_count=None):
"""
Log directory may be a file in that directory.
"""
f = logging.Formatter(fmt='%(levelname)s:%(name)s: %(message)s '
@tkrajina
tkrajina / xml_traversing.go
Last active August 29, 2015 14:04
Traversing XML in GO
package main
import (
"encoding/xml"
"fmt"
"os"
)
func main() {
f, _ := os.Open("test_files/gpx1.1_with_all_fields.gpx")
@tkrajina
tkrajina / gist:5252035
Created March 27, 2013 05:58
Minimalistic javascript assert with stacktrace.
if(!window.assert) {
window.assert = function() {
if(!arguments.length || arguments < 2) {
alert('Invalid assert, use assert(expression, message1, message2, ...)')
return
}
var expression = arguments[0]
if(!expression) {
var message = ''
for(i = 1; i < arguments.length; i++) message += ' ' + arguments[i]
@tkrajina
tkrajina / gist:5418085
Created April 19, 2013 04:07
OpenStreetMap images layer on Google Map
map.mapTypes.set("OSM", new google.maps.ImageMapType({
getTileUrl: function(coord, zoom) {
var imagesPerUnit = Math.pow(2, zoom);
// Stitch horizontaly:
var x = coord.x < 0 ? imagesPerUnit + (coord.x % imagesPerUnit) : coord.x;
var x = x >= imagesPerUnit ? 0 : x;
// Do not stitch verticaly, if overflow -- don't show:
if(coord.y < 0 || coord.y >= imagesPerUnit)