text: from _csv import register_dialect, QUOTE_ALL
  import csv
  import codecs
  import cStringIO
  from HTMLParser import HTMLParser
  from itertools import groupby
  from django.conf import settings
  
  
  def currency_converter(amount, year):
      return amount * settings.OPENAID_CURRENCY_CONVERSIONS[year]
  
  def eur_to_usd_converter(amount, year):
      return amount / settings.OPENAID_CURRENCY_CONVERSIONS[year]
  
  
  def sanitize_get_param(param_type, param, default, top=None, length=None):
      # simple sanitize of GET parameter and converting to param_type
      # if fails: return default value
      # if top is defined and the value is > than top: return default
      if not param:
          return default
      if length:
          if len(param) > length:
              param = param[:length]
      try:
          value = param_type(param)
      except ValueError:
          return default
      else:
          if top:
              if value > top:
                  return default
              else:
                  return value
          return value
  
  
  class UTF8Recoder:
      """
      Iterator that reads an encoded stream and reencodes the input to UTF-8
      """
  
      def __init__(self, f, encoding):
          self.reader = codecs.getreader(encoding)(f)
  
      def __iter__(self):
          return self
  
      def next(self):
          return self.reader.next().encode("utf-8")
  
  
  class UnicodeDictReader:
      """
      A CSV reader which will iterate over lines in the CSV file "f",
      which is encoded in the given encoding.
      """
  
      def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
          f = UTF8Recoder(f, encoding)
          self.reader = csv.DictReader(f, dialect=dialect, **kwds)
  
      def next(self):
          row = self.reader.next()
          return dict((k, unicode(s, "utf-8")) for k, s in row.iteritems() if s is not None)
  
      def __iter__(self):
          return self
  
  
  class UnicodeDictWriter(object):
      def __init__(self, f, fieldnames, dialect=csv.excel, encoding="utf-8", **kwds):
          # Redirect output to a queue
          self.queue = cStringIO.StringIO()
          self.writer = csv.DictWriter(self.queue, fieldnames, dialect=dialect, **kwds)
          self.stream = f
          self.encoder = codecs.getincrementalencoder(encoding)()
  
      def writerow(self, D):
          try:
              self.writer.writerow(dict([(k,v.encode("utf-8")) for k,v in D.items()]))
          except UnicodeEncodeError:
              import pprint
              pprint.pprint(D['geography'])
              return
          # Fetch UTF-8 output from the queue ...
          data = self.queue.getvalue()
          data = data.decode("utf-8")
          # ... and reencode it into the target encoding
          data = self.encoder.encode(data)
          # write to the target stream
          self.stream.write(data)
          # empty queue
          self.queue.truncate(0)
  
      def writerows(self, rows):
          for D in rows:
              self.writerow(D)
  
  
  class ExcelSemicolon(csv.excel):
      """Extends excel Dialect in order to set semicolon as delimiter."""
      delimiter = ';'
      quoting = QUOTE_ALL
  
  
  register_dialect("excel_semicolon", ExcelSemicolon)
  
  
  class UnicodeWriter:
      """
      A CSV writer which will write rows to CSV file "f",
      which is encoded in the given encoding.
      """
  
      def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
          # Redirect output to a queue
          self.queue = cStringIO.StringIO()
          self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
          self.stream = f
          self.encoder = codecs.getincrementalencoder(encoding)()
  
      def writerow(self, row):
          self.writer.writerow([s.encode("utf-8") for s in row])
          # Fetch UTF-8 output from the queue ...
          data = self.queue.getvalue()
          data = data.decode("utf-8")
          # ... and reencode it into the target encoding
          data = self.encoder.encode(data)
          # write to the target stream
          self.stream.write(data)
          # empty queue
          self.queue.truncate(0)
  
      def writerows(self, rows):
          for row in rows:
              self.writerow(row)
  
  
  class MLStripper(HTMLParser):
      def __init__(self):
          self.reset()
          self.fed = []
  
      def handle_data(self, d):
          self.fed.append(d)
  
      def get_data(self):
          return ' '.join(self.fed)
  
  
  def strip_tags(html):
      s = MLStripper()
      s.feed(html)
      return s.get_data()
  
  
  # converts a qset into a dict using the key field
  def convert2dict(queryset, key_field):
      if type(key_field) == tuple or type(key_field) == list:
          if len(key_field) == 0:
              raise Exception
          else:
              indice_keygen = lambda x: tuple(x[key] for key in key_field)
  
      else:
          indice_keygen = lambda x: x[key_field]
  
      return dict((k, list(v)) for k, v in groupby(queryset, key=indice_keygen))
  