Coverage for /Users/tobyqin/src/eztools/eztools/converters.py : 27%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import re
3import six
6def to_int(input_, default=0, exception=(ValueError, TypeError), regexp=None):
7 r'''
8 Convert the given input to an integer or return default
10 When trying to convert the exceptions given in the exception parameter
11 are automatically catched and the default will be returned.
13 The regexp parameter allows for a regular expression to find the digits
14 in a string.
15 When True it will automatically match any digit in the string.
16 When a (regexp) object (has a search method) is given, that will be used.
17 WHen a string is given, re.compile will be run over it first
19 The last group of the regexp will be used as value
21 >>> to_int('abc')
22 0
23 >>> to_int('1')
24 1
25 >>> to_int('abc123')
26 0
27 >>> to_int('123abc')
28 0
29 >>> to_int('abc123', regexp=True)
30 123
31 >>> to_int('123abc', regexp=True)
32 123
33 >>> to_int('abc123abc', regexp=True)
34 123
35 >>> to_int('abc123abc456', regexp=True)
36 123
37 >>> to_int('abc123', regexp=re.compile(r'(\d+)'))
38 123
39 >>> to_int('123abc', regexp=re.compile(r'(\d+)'))
40 123
41 >>> to_int('abc123abc', regexp=re.compile(r'(\d+)'))
42 123
43 >>> to_int('abc123abc456', regexp=re.compile(r'(\d+)'))
44 123
45 >>> to_int('abc123', regexp=r'(\d+)')
46 123
47 >>> to_int('123abc', regexp=r'(\d+)')
48 123
49 >>> to_int('abc', regexp=r'(\d+)')
50 0
51 >>> to_int('abc123abc', regexp=r'(\d+)')
52 123
53 >>> to_int('abc123abc456', regexp=r'(\d+)')
54 123
55 >>> to_int('1234', default=1)
56 1234
57 >>> to_int('abc', default=1)
58 1
59 >>> to_int('abc', regexp=123)
60 Traceback (most recent call last):
61 ...
62 TypeError: unknown argument for regexp parameter: 123
63 '''
65 if regexp is True:
66 regexp = re.compile(r'(\d+)')
67 elif isinstance(regexp, six.string_types):
68 regexp = re.compile(regexp)
69 elif hasattr(regexp, 'search'):
70 pass
71 elif regexp is not None:
72 raise TypeError('unknown argument for regexp parameter: %r' % regexp)
74 try:
75 if regexp:
76 match = regexp.search(input_)
77 if match:
78 input_ = match.groups()[-1]
79 return int(input_)
80 except exception:
81 return default
84def to_float(input_, default=0, exception=(ValueError, TypeError), regexp=None):
85 r'''
86 Convert the given `input_` to an integer or return default
88 When trying to convert the exceptions given in the exception parameter
89 are automatically catched and the default will be returned.
91 The regexp parameter allows for a regular expression to find the digits
92 in a string.
93 When True it will automatically match any digit in the string.
94 When a (regexp) object (has a search method) is given, that will be used.
95 WHen a string is given, re.compile will be run over it first
97 The last group of the regexp will be used as value
99 >>> '%.2f' % to_float('abc')
100 '0.00'
101 >>> '%.2f' % to_float('1')
102 '1.00'
103 >>> '%.2f' % to_float('abc123.456', regexp=True)
104 '123.46'
105 >>> '%.2f' % to_float('abc123', regexp=True)
106 '123.00'
107 >>> '%.2f' % to_float('abc0.456', regexp=True)
108 '0.46'
109 >>> '%.2f' % to_float('abc123.456', regexp=re.compile(r'(\d+\.\d+)'))
110 '123.46'
111 >>> '%.2f' % to_float('123.456abc', regexp=re.compile(r'(\d+\.\d+)'))
112 '123.46'
113 >>> '%.2f' % to_float('abc123.46abc', regexp=re.compile(r'(\d+\.\d+)'))
114 '123.46'
115 >>> '%.2f' % to_float('abc123abc456', regexp=re.compile(r'(\d+(\.\d+|))'))
116 '123.00'
117 >>> '%.2f' % to_float('abc', regexp=r'(\d+)')
118 '0.00'
119 >>> '%.2f' % to_float('abc123', regexp=r'(\d+)')
120 '123.00'
121 >>> '%.2f' % to_float('123abc', regexp=r'(\d+)')
122 '123.00'
123 >>> '%.2f' % to_float('abc123abc', regexp=r'(\d+)')
124 '123.00'
125 >>> '%.2f' % to_float('abc123abc456', regexp=r'(\d+)')
126 '123.00'
127 >>> '%.2f' % to_float('1234', default=1)
128 '1234.00'
129 >>> '%.2f' % to_float('abc', default=1)
130 '1.00'
131 >>> '%.2f' % to_float('abc', regexp=123)
132 Traceback (most recent call last):
133 ...
134 TypeError: unknown argument for regexp parameter
135 '''
137 if regexp is True:
138 regexp = re.compile(r'(\d+(\.\d+|))')
139 elif isinstance(regexp, six.string_types):
140 regexp = re.compile(regexp)
141 elif hasattr(regexp, 'search'):
142 pass
143 elif regexp is not None:
144 raise TypeError('unknown argument for regexp parameter')
146 try:
147 if regexp:
148 match = regexp.search(input_)
149 if match:
150 input_ = match.group(1)
151 return float(input_)
152 except exception:
153 return default
156def to_unicode(input_, encoding='utf-8', errors='replace'):
157 '''Convert objects to unicode, if needed decodes string with the given
158 encoding and errors settings.
160 :rtype: unicode
162 >>> to_unicode(b'a')
163 'a'
164 >>> to_unicode('a')
165 'a'
166 >>> to_unicode(u'a')
167 'a'
168 >>> class Foo(object): __str__ = lambda s: u'a'
169 >>> to_unicode(Foo())
170 'a'
171 >>> to_unicode(Foo)
172 "<class 'python_utils.converters.Foo'>"
173 '''
174 if isinstance(input_, six.binary_type):
175 input_ = input_.decode(encoding, errors)
176 else:
177 input_ = six.text_type(input_)
178 return input_
181def to_str(input_, encoding='utf-8', errors='replace'):
182 '''Convert objects to string, encodes to the given encoding
184 :rtype: str
186 >>> to_str('a')
187 b'a'
188 >>> to_str(u'a')
189 b'a'
190 >>> to_str(b'a')
191 b'a'
192 >>> class Foo(object): __str__ = lambda s: u'a'
193 >>> to_str(Foo())
194 'a'
195 >>> to_str(Foo)
196 "<class 'python_utils.converters.Foo'>"
197 '''
198 if isinstance(input_, six.binary_type):
199 pass
200 else:
201 if not hasattr(input_, 'encode'):
202 input_ = six.text_type(input_)
204 input_ = input_.encode(encoding, errors)
205 return input_