-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.py
More file actions
30 lines (23 loc) · 889 Bytes
/
methods.py
File metadata and controls
30 lines (23 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def encode_dictionary(params):
"""converts the values of a dictionary to unicode string."""
if isinstance(params, dict):
new_dict = params.copy()
charset = params.get('charset', None)
for key, val in params.items():
if not isinstance(val, basestring):
continue
new_dict[key] = convert_to_unicode(val, charset)
else:
raise TypeError('Parameters must be a dictionary')
return new_dict
def convert_to_unicode(text, charset='utf-8'):
'''returns unicode object of text.'''
if isinstance(text, unicode):
return text
if isinstance(text, str):
try:
return unicode(text, charset, 'replace')
except (TypeError, LookupError):
return unicode(text, 'utf-8', 'replace')
else:
raise TypeError('Text must be a string or unicode object.')