Login

Roman Numeral Filter

Author:
Paperface
Posted:
April 16, 2007
Language:
Python
Version:
.96
Score:
1 (after 3 ratings)

Template filter to convert integer or long integer into roman numeral with support for upper and lower case numerals.

Requires python-roman package on Debian which apparently comes from http://www.diveintopython.org/

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""Roman library comes from python-roman package on Debian which apparently comes from \
http://www.diveintopython.org/"""

from roman import toRoman

def romanize_filter(value, args=None):
    """Change int or long into Roman Numeral all other types are passed out
    You can add an argument like this:
        ...
        {{ object.id|romanize:"upper" }}
        ...
    For upper case roman numerals. The tag defaults to lowercase numerals for
    no good reason other than I prefer the look of them."""
    if isinstance(value, int) or isinstance(value, long):
        if args != None:
            if args.lower() == "upper":
                return toRoman(value)
            else:
                return toRoman(value).lower()
        else:
            return toRoman(value).lower()
    else:
        return value
register.filter('romanize', romanize_filter)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.