/usr/lib/python3/dist-packages/python_utils/converters.py is in python3-python-utils 2.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | from __future__ import (absolute_import, division, print_function,
unicode_literals)
import re
import six
def to_int(input_, default=0, exception=(ValueError, TypeError), regexp=None):
'''
Convert the given input to an integer or return default
When trying to convert the exceptions given in the exception parameter
are automatically catched and the default will be returned.
The regexp parameter allows for a regular expression to find the digits
in a string.
When True it will automatically match any digit in the string.
When a (regexp) object (has a search method) is given, that will be used.
WHen a string is given, re.compile will be run over it first
The last group of the regexp will be used as value
>>> to_int('abc')
0
>>> to_int('1')
1
>>> to_int('abc123')
0
>>> to_int('123abc')
0
>>> to_int('abc123', regexp=True)
123
>>> to_int('123abc', regexp=True)
123
>>> to_int('abc123abc', regexp=True)
123
>>> to_int('abc123abc456', regexp=True)
123
>>> to_int('abc123', regexp=re.compile('(\d+)'))
123
>>> to_int('123abc', regexp=re.compile('(\d+)'))
123
>>> to_int('abc123abc', regexp=re.compile('(\d+)'))
123
>>> to_int('abc123abc456', regexp=re.compile('(\d+)'))
123
>>> to_int('abc123', regexp='(\d+)')
123
>>> to_int('123abc', regexp='(\d+)')
123
>>> to_int('abc', regexp='(\d+)')
0
>>> to_int('abc123abc', regexp='(\d+)')
123
>>> to_int('abc123abc456', regexp='(\d+)')
123
>>> to_int('1234', default=1)
1234
>>> to_int('abc', default=1)
1
>>> to_int('abc', regexp=123)
Traceback (most recent call last):
...
TypeError: unknown argument for regexp parameter: 123
'''
if regexp is True:
regexp = re.compile('(\d+)')
elif isinstance(regexp, six.string_types):
regexp = re.compile(regexp)
elif hasattr(regexp, 'search'):
pass
elif regexp is not None:
raise TypeError('unknown argument for regexp parameter: %r' % regexp)
try:
if regexp:
match = regexp.search(input_)
if match:
input_ = match.groups()[-1]
return int(input_)
except exception:
return default
def to_float(input_, default=0, exception=(ValueError, TypeError),
regexp=None):
'''
Convert the given `input_` to an integer or return default
When trying to convert the exceptions given in the exception parameter
are automatically catched and the default will be returned.
The regexp parameter allows for a regular expression to find the digits
in a string.
When True it will automatically match any digit in the string.
When a (regexp) object (has a search method) is given, that will be used.
WHen a string is given, re.compile will be run over it first
The last group of the regexp will be used as value
>>> '%.2f' % to_float('abc')
'0.00'
>>> '%.2f' % to_float('1')
'1.00'
>>> '%.2f' % to_float('abc123.456', regexp=True)
'123.46'
>>> '%.2f' % to_float('abc123', regexp=True)
'123.00'
>>> '%.2f' % to_float('abc0.456', regexp=True)
'0.46'
>>> '%.2f' % to_float('abc123.456', regexp=re.compile('(\d+\.\d+)'))
'123.46'
>>> '%.2f' % to_float('123.456abc', regexp=re.compile('(\d+\.\d+)'))
'123.46'
>>> '%.2f' % to_float('abc123.46abc', regexp=re.compile('(\d+\.\d+)'))
'123.46'
>>> '%.2f' % to_float('abc123abc456', regexp=re.compile('(\d+(\.\d+|))'))
'123.00'
>>> '%.2f' % to_float('abc', regexp='(\d+)')
'0.00'
>>> '%.2f' % to_float('abc123', regexp='(\d+)')
'123.00'
>>> '%.2f' % to_float('123abc', regexp='(\d+)')
'123.00'
>>> '%.2f' % to_float('abc123abc', regexp='(\d+)')
'123.00'
>>> '%.2f' % to_float('abc123abc456', regexp='(\d+)')
'123.00'
>>> '%.2f' % to_float('1234', default=1)
'1234.00'
>>> '%.2f' % to_float('abc', default=1)
'1.00'
>>> '%.2f' % to_float('abc', regexp=123)
Traceback (most recent call last):
...
TypeError: unknown argument for regexp parameter
'''
if regexp is True:
regexp = re.compile('(\d+(\.\d+|))')
elif isinstance(regexp, six.string_types):
regexp = re.compile(regexp)
elif hasattr(regexp, 'search'):
pass
elif regexp is not None:
raise TypeError('unknown argument for regexp parameter')
try:
if regexp:
match = regexp.search(input_)
if match:
input_ = match.group(1)
return float(input_)
except exception:
return default
def to_unicode(input_, encoding='utf-8', errors='replace'):
'''Convert objects to unicode, if needed decodes string with the given
encoding and errors settings.
:rtype: unicode
>>> to_unicode(b'a')
'a'
>>> to_unicode('a')
'a'
>>> to_unicode(u'a')
'a'
>>> class Foo(object): __str__ = lambda s: u'a'
>>> to_unicode(Foo())
'a'
>>> to_unicode(Foo)
"<class 'python_utils.converters.Foo'>"
'''
if isinstance(input_, six.binary_type):
input_ = input_.decode(encoding, errors)
else:
input_ = six.text_type(input_)
return input_
def to_str(input_, encoding='utf-8', errors='replace'):
'''Convert objects to string, encodes to the given encoding
:rtype: str
>>> to_str('a')
b'a'
>>> to_str(u'a')
b'a'
>>> to_str(b'a')
b'a'
>>> class Foo(object): __str__ = lambda s: u'a'
>>> to_str(Foo())
'a'
>>> to_str(Foo)
"<class 'python_utils.converters.Foo'>"
'''
if isinstance(input_, six.binary_type):
pass
else:
if not hasattr(input_, 'encode'):
input_ = six.text_type(input_)
input_ = input_.encode(encoding, errors)
return input_
|