Skip to content

Instantly share code, notes, and snippets.

@walterrenner
walterrenner / unicode.py
Created October 11, 2017 12:26
Dealing with unicode and strings in Python
# Deal exclusively with unicode objects as much as possible
# by decoding things to unicode objects when you first get them and
# encoding them as necessary on the way out.
# https://stackoverflow.com/a/6048203
>>> s = 'abc'
>>> type(s)
<type 'str'>
>>> u = u'abc' # note the u prefix
@walterrenner
walterrenner / split_excel.py
Created September 13, 2017 07:38
Split large Excel files into chunks of n
import argparse
import xlrd
import xlwt
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
@walterrenner
walterrenner / german-iso-3166.csv
Last active March 21, 2024 10:51 — forked from malteos/german-iso-3166.csv
German ISO-3166 Country Codes CSV (deutsche Ländercodes)
AF Afghanistan
EG Ägypten
AL Albanien
DZ Algerien
AD Andorra
AO Angola
AI Anguilla
AQ Antarktis
AG Antigua und Barbuda
GQ Äquatorial Guinea
@walterrenner
walterrenner / hack.md
Created January 4, 2016 08:36
some useful bash commands

count history commands

history | cut -c8- | sort | uniq -c | sort -rn | head
@walterrenner
walterrenner / gist:f8e8d6b06a1f0c79bea6
Created December 17, 2015 10:30 — forked from abrookins/gist:1933635
A Sublime Text 2 Django project file with a test runner build system
{
"folders":
[
{
"path": "django_project_dir"
},
{
"path": "lib/python2.7"
}
],
@walterrenner
walterrenner / readme.md
Created October 14, 2015 12:55
Use project related mirgation modules

Keep your custom Migrations in your project

A proof of concept.

If you cant't/won't use the apps Migration files this workaround can be helpful for you.

We assume you have a clean Database without any Migrations applied and a Project named my_project that you are working on. Add the below snippet in your settings.py right after INSTALLED_APPS.

This tells django to use migration files from the module in my_project.migrations.<appname>.migrations.

@walterrenner
walterrenner / tests.py
Created August 26, 2015 19:32
Minimal django TestCase
from django.test import TestCase
from .models import Thing
# Create your tests here.
class TestModels(TestCase):
def test_saving_a_model(self):
thing = Thing.objects.create()
thing.save()
@walterrenner
walterrenner / command.py
Created August 21, 2015 11:54
print django project information
from compat import import_string
from django.conf import settings
for app_name in settings.INSTALLED_APPS:
app = __import__(app_name)
print "Information for " + app_name
try:
print "verion: " + app.__version__
except Exception as e:
print "version: " + str(e)
@walterrenner
walterrenner / models.py
Created January 29, 2015 15:52
get_admin_url() for any model instance
from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType
from django.db import models
class MyModel(models.Model):
def get_admin_url(self):
content_type = ContentType.objects.get_for_model(self.__class__)
return urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(self.id,))
import atexit
import os
import sys
try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind('tab:complete')