pandora/pandora/item/utils.py

53 lines
1.2 KiB
Python
Raw Normal View History

2009-06-08 16:08:59 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
#
2010-12-22 15:17:38 +00:00
from decimal import Decimal
2009-06-08 16:08:59 +00:00
import re
import unicodedata
2009-06-08 16:08:59 +00:00
2010-08-24 17:16:33 +00:00
2010-12-22 15:17:38 +00:00
def parse_decimal(string):
string = string.replace(':', '/')
if '/' not in string:
string = '%s/1' % string
d = string.split('/')
return Decimal(d[0]) / Decimal(d[1])
2011-01-01 11:44:42 +00:00
2010-08-24 17:16:33 +00:00
def plural_key(term):
return {
'country': 'countries',
}.get(term, term + 's')
2011-01-01 11:44:42 +00:00
2011-01-03 19:45:56 +00:00
def sort_string(string):
string = string.replace(u'Þ', 'Th')
2011-02-01 13:19:34 +00:00
#pad numbered titles
string = re.sub('(\d+)', lambda x: '%010d' % int(x.group(0)), string)
2011-01-03 19:45:56 +00:00
return unicodedata.normalize('NFKD', string)
2010-09-03 13:28:44 +00:00
def sort_title(title):
2011-10-11 19:30:43 +00:00
title = title.replace(u'Æ', 'Ae')
2010-11-26 17:16:57 +00:00
if isinstance(title, str):
title = unicode(title)
2011-01-03 19:45:56 +00:00
title = sort_string(title)
2011-10-11 19:05:11 +00:00
#title
title = re.sub(u'[\'!¿¡,\.;\-"\:\*\[\]]', '', title)
2010-09-03 13:28:44 +00:00
return title.strip()
2011-01-13 19:40:50 +00:00
def get_positions(ids, pos):
'''
>>> get_positions([1,2,3,4], [2,4])
{2: 1, 4: 3}
'''
positions = {}
for i in pos:
try:
positions[i] = ids.index(i)
except:
pass
return positions