comparison SrvTxtUtils.py @ 514:c55e376be01b

search works even with unicode...
author casties
date Tue, 28 Feb 2012 19:46:37 +0100
parents 6f116b86a226
children f8a5f63eafc0
comparison
equal deleted inserted replaced
513:67095296c95a 514:c55e376be01b
9 import urllib 9 import urllib
10 import urllib2 10 import urllib2
11 import logging 11 import logging
12 12
13 13
14 srvTxtUtilsVersion = "1.3" 14 srvTxtUtilsVersion = "1.4"
15 15
16 def getInt(number, default=0): 16 def getInt(number, default=0):
17 """returns always an int (0 in case of problems)""" 17 """returns always an int (0 in case of problems)"""
18 try: 18 try:
19 return int(number) 19 return int(number)
24 """returns element idx from array or default (in case of problems)""" 24 """returns element idx from array or default (in case of problems)"""
25 try: 25 try:
26 return array[idx] 26 return array[idx]
27 except: 27 except:
28 return default 28 return default
29
30 def unicodify(s):
31 """decode str (utf-8 or latin-1 representation) into unicode object"""
32 if not s:
33 return u""
34 if isinstance(s, str):
35 try:
36 return s.decode('utf-8')
37 except:
38 return s.decode('latin-1')
39 else:
40 return s
41
42 def utf8ify(s):
43 """encode unicode object or string into byte string in utf-8 representation.
44 assumes string objects to be utf-8"""
45 if not s:
46 return ""
47 if isinstance(s, str):
48 return s
49 else:
50 return s.encode('utf-8')
29 51
30 def getText(node, recursive=0): 52 def getText(node, recursive=0):
31 """returns all text content of a node and its subnodes""" 53 """returns all text content of a node and its subnodes"""
32 if node is None: 54 if node is None:
33 return '' 55 return ''