Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Mike Owens http://mike.filespanker.com/

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Bootstrap Django templates out-of-framework

This is the quickest way I've found to bootstrap Django templates without using the entire framework. This is suited for "plain-python" apps without using all the Django types. You'll probably want to cache the Template returns for each given name, but here's the basics.

   1  
   2  # Kick off django config machinery first
   3  from django.conf import settings
   4  settings.configure(TEMPLATE_DIRS=("/whatever/templates",))
   5  
   6  import django.template
   7  import django.template.loader
   8  
   9  
  10  def render(name, *values):
  11      ctx = django.template.Context()
  12      for d in values:
  13          ctx.push()
  14          ctx.update(d)
  15  
  16      t = django.template.loader.get_template(name)
  17      return t.render(ctx)
  18  
  19  print render('layout.tmpl', dict(title='User'), dict(name='Bob', gender='M'))

Convert an integer to English written form

English is a peculiar language. english_number() takes an integer, and returns the long, written form most famously used on checks and the like. Algorithm inspired by some old HP online docs.

   1  
   2  to_19 = ( 'zero',  'one',   'two',  'three', 'four',   'five',   'six',
   3            'seven', 'eight', 'nine', 'ten',   'eleven', 'twelve', 'thirteen',
   4            'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' )
   5  tens  = ( 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety')
   6  denom = ( '',
   7            'thousand',     'million',         'billion',       'trillion',       'quadrillion',
   8            'quintillion',  'sextillion',      'septillion',    'octillion',      'nonillion',
   9            'decillion',    'undecillion',     'duodecillion',  'tredecillion',   'quattuordecillion',
  10            'sexdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' )
  11  
  12  # convert a value < 100 to English.
  13  def _convert_nn(val):
  14      if val < 20:
  15          return to_19[val]
  16      for (dcap, dval) in ((k, 20 + (10 * v)) for (v, k) in enumerate(tens)):
  17          if dval + 10 > val:
  18              if val % 10:
  19                  return dcap + '-' + to_19[val % 10]
  20              return dcap
  21  
  22  # convert a value < 1000 to english, special cased because it is the level that kicks 
  23  # off the < 100 special case.  The rest are more general.  This also allows you to
  24  # get strings in the form of 'forty-five hundred' if called directly.
  25  def _convert_nnn(val):
  26      word = ''
  27      (mod, rem) = (val % 100, val // 100)
  28      if rem > 0:
  29          word = to_19[rem] + ' hundred'
  30          if mod > 0:
  31              word = word + ' '
  32      if mod > 0:
  33          word = word + _convert_nn(mod)
  34      return word
  35  
  36  def english_number(val):
  37      if val < 100:
  38          return _convert_nn(val)
  39       if val < 1000:
  40           return _convert_nnn(val)
  41      for (didx, dval) in ((v - 1, 1000 ** v) for v in range(len(denom))):
  42          if dval > val:
  43              mod = 1000 ** didx
  44              l = val // mod
  45              r = val - (l * mod)
  46              ret = _convert_nnn(l) + ' ' + denom[didx]
  47              if r > 0:
  48                  ret = ret + ', ' + english_number(r)
  49              return ret
  50  
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS