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
package main
import (
"fmt"
"reflect"
)
type Test struct {
}
@tkrajina
tkrajina / convert_b_exercises.py
Created February 11, 2016 16:09
Convert b exercises
# -*- coding: utf-8 -*-
import sys
f = open(sys.argv[1])
string = f.read()
f.close()
parts = string.strip().split("*")
translation = parts[-1].strip()
@tkrajina
tkrajina / inject.go
Created February 10, 2016 06:25
Golang inject example
package main
import (
"fmt"
"time"
"github.com/facebookgo/inject"
)
type EmailSender struct {
@tkrajina
tkrajina / remove_accents.go
Created January 31, 2016 08:21
Golang remove accents
package main
import (
"fmt"
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
package main
// From https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
import (
"encoding/json"
"fmt"
"strings"
)
@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 / 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 / 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 / django_raw_sql_without_model.py
Last active November 27, 2023 18:55
Django raw sql without model
from django.db import connection
# If using cursor without "with" -- it must be closed explicitly:
with connection.cursor() as cursor:
cursor.execute('select column1, column2, column3 from table where aaa=%s', [5])
for row in cursor.fetchall():
print row[0], row[1], row[3]
@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: