• AgriCatch source released

    AgriCatch-logoHello everyone!
    Today is a great day to release some projects to the wild!

  • Customizing Django Serializers

    django-logo-small
    Django Serializers are suppose to take entries from your DB
    and serialize in either Json or XML.
    The problem is, they’re not very customizable,
    and their defaults are not what I was looking for.

  • Why I chose Python over PHP

    Or: Why I chose Django over YiiPython over PHP

  • Templating in Django

    I've been trying to make a template layout for Django that makes sense, something similar to existing PHP frameworks, where the head section is in one file, and the body is in another.

  • Slugifying German string with Python

    I'm working on this project that has some German titles, and a lot of irregularities inside them
    (question marks, underscores, etc.)
    Because I have to refer the object (in this case product) by some sort of understandable name, I've decided to slugify the title
    (basically turn "THIS EXAMPLE!!" to "this-example") While I'm already doing that, I wanted to cleverly switch the "ä,ö,ü,ß" to "ae,oe,ue,ss" accordingly.
    I'm pretty sure this might be useful to someone, and it's pretty harmless to share it so...
    this gist was born, and the code is:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    # -*- coding: utf-8 -*-
    def replace_all(text, dic):
        for replace_with, replace_what in dic.iteritems():
            if type(replace_what) is list:
                for letter in replace_what:
                    text = text.replace(letter,replace_with)
            else:
                text = text.replace(replace_what, replace_with)
        return text
    #example input: ssdfSERFS - ^#$%43 GSDG  ____ :VKbm sdF öüoäßßEF_____-___ d a s d _e <>DFGDFG????
    #example output: ssdfserfs-43-gsdg-vkbm-sdf-oeueoaessssef-d-a-s-d-e-dfgdfg
    def slugify(string,params = []):
        import re
        #unescape html entity signs (e.g. & -> &) & lowercase string
        import HTMLParser
        string = HTMLParser.HTMLParser().unescape(string).lower()
        translation_dic = {
               'ae' : ['Ä','ä','Ä','ä'],
               'oe' : ['Ö','ö','Ö','ö'],
               'ue' : ['Ü','ü','Ü','ü'],
               'ss' : ['ß','ß'],
        }
        string = replace_all(string,translation_dic)
        string = re.sub(r'[^a-zA-Z0-9\_\-\s]','',string)
        string = re.sub(r'[\s]{2,}',' ',string)
        string = string.replace(' ','-').replace('_','-').strip(' -')
        string = re.sub(r'[\-]{2,}','-',string)
        return string