Passed
Push — master ( 05ccdf...015759 )
by Fabio
01:12
created

ParseDict.get_date_list()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 5
1
# -*- coding: utf-8 -*-
2
3
from benedict.dicts.base import BaseDict
4
from benedict.dicts.parse import parse_util
5
from benedict.utils import type_util
6
7
from decimal import Decimal
8
9
10
class ParseDict(BaseDict):
11
12
    def __init__(self, *args, **kwargs):
13
        """
14
        Constructs a new instance.
15
        """
16
        super(ParseDict, self).__init__(*args, **kwargs)
17
18
    def _get_value(self, key, default, choices,
19
                   parser_func, parser_kwargs=None):
20
        """
21
        Get value by key, or keypath core method.
22
        If choices and value is in choices return value otherwise default.
23
        """
24
25
        # Get raw value from self.
26
        value = self.get(key, None)
27
        # If value is None return default value.
28
        if value is None:
29
            return default
30
31
        # If not of the desired type, try to parse it using parser_func.
32
        value = parser_func(value, **(parser_kwargs or {}))
33
        # If value is None after parsing return default value.
34
        if value is None:
35
            return default
36
37
        # If choices and value in choices return value otherwise default.
38
        if type_util.is_list_or_tuple(choices) and len(choices) and \
39
                value not in choices:
40
            return default
41
42
        return value
43
44
    def _get_values_list(self, key, default, separator,
45
                         parser_func, parser_kwargs=None):
46
        """
47
        Get value by key or keypath trying to return it as list of bool values.
48
        If separator is specified and value is a string it will be splitted.
49
        """
50
        if key not in self:
51
            return default or []
52
        values_list = self.get_list(key, [], separator)
53
        return [parser_func(value, **(parser_kwargs or {}))
54
                for value in values_list]
55
56
    def get_bool(self, key, default=False):
57
        """
58
        Get value by key or keypath trying to return it as bool.
59
        Values like `1`, `true`, `yes`, `on` will be returned as `True`.
60
        """
61
        return self._get_value(
62
            key, default, [True, False], parse_util.parse_bool)
63
64
    def get_bool_list(self, key, default=None, separator=','):
65
        """
66
        Get value by key or keypath trying to return it as list of bool values.
67
        If separator is specified and value is a string it will be splitted.
68
        """
69
        return self._get_values_list(
70
            key, default, separator, parse_util.parse_bool)
71
72
    def get_date(self, key, default=None, format=None, choices=None):
73
        """
74
        Get value by key or keypath trying to return it as date.
75
        If format is not specified it will be autodetected.
76
        If choices and value is in choices return value otherwise default.
77
        """
78
        return self._get_value(
79
            key, default, choices, parse_util.parse_date,
80
            {'format': format})
81
82
    def get_date_list(self, key, default=None, format=None, separator=','):
83
        """
84
        Get value by key or keypath trying to return it as list of date values.
85
        If separator is specified and value is a string it will be splitted.
86
        """
87
        return self._get_values_list(
88
            key, default, separator, parse_util.parse_date,
89
            {'format': format})
90
91
    def get_datetime(self, key, default=None, format=None, choices=None):
92
        """
93
        Get value by key or keypath trying to return it as datetime.
94
        If format is not specified it will be autodetected.
95
        If choices and value is in choices return value otherwise default.
96
        """
97
        return self._get_value(
98
            key, default, choices, parse_util.parse_datetime,
99
            {'format': format})
100
101
    def get_datetime_list(self, key, default=None, format=None, separator=','):
102
        """
103
        Get value by key or keypath trying to return it as list of datetime values.
104
        If separator is specified and value is a string it will be splitted.
105
        """
106
        return self._get_values_list(
107
            key, default, separator, parse_util.parse_datetime,
108
            {'format': format})
109
110
    def get_decimal(self, key, default=Decimal('0.0'), choices=None):
111
        """
112
        Get value by key or keypath trying to return it as Decimal.
113
        If choices and value is in choices return value otherwise default.
114
        """
115
        return self._get_value(
116
            key, default, choices, parse_util.parse_decimal)
117
118
    def get_decimal_list(self, key, default=None, separator=','):
119
        """
120
        Get value by key or keypath trying to return it as list of Decimal values.
121
        If separator is specified and value is a string it will be splitted.
122
        """
123
        return self._get_values_list(
124
            key, default, separator, parse_util.parse_decimal)
125
126
    def get_dict(self, key, default=None):
127
        """
128
        Get value by key or keypath trying to return it as dict.
129
        If value is a json string it will be automatically decoded.
130
        """
131
        return self._get_value(
132
            key, default or {}, None, parse_util.parse_dict)
133
134
    def get_email(self, key, default='', choices=None, check_blacklist=True):
135
        """
136
        Get email by key or keypath and return it.
137
        If value is blacklisted it will be automatically ignored.
138
        If check_blacklist is False, it will be not ignored even if blacklisted.
139
        """
140
        return self._get_value(
141
            key, default, choices, parse_util.parse_email,
142
            {'check_blacklist': check_blacklist})
143
144
    def get_float(self, key, default=0.0, choices=None):
145
        """
146
        Get value by key or keypath trying to return it as float.
147
        If choices and value is in choices return value otherwise default.
148
        """
149
        return self._get_value(
150
            key, default, choices, parse_util.parse_float)
151
152
    def get_float_list(self, key, default=None, separator=','):
153
        """
154
        Get value by key or keypath trying to return it as list of float values.
155
        If separator is specified and value is a string it will be splitted.
156
        """
157
        return self._get_values_list(
158
            key, default, separator, parse_util.parse_float)
159
160
    def get_int(self, key, default=0, choices=None):
161
        """
162
        Get value by key or keypath trying to return it as int.
163
        If choices and value is in choices return value otherwise default.
164
        """
165
        return self._get_value(
166
            key, default, choices, parse_util.parse_int)
167
168
    def get_int_list(self, key, default=None, separator=','):
169
        """
170
        Get value by key or keypath trying to return it as list of int values.
171
        If separator is specified and value is a string it will be splitted.
172
        """
173
        return self._get_values_list(
174
            key, default, separator, parse_util.parse_int)
175
176
    def get_list(self, key, default=None, separator=','):
177
        """
178
        Get value by key or keypath trying to return it as list.
179
        If separator is specified and value is a string it will be splitted.
180
        """
181
        return self._get_value(
182
            key, default or [], None, parse_util.parse_list,
183
            {'separator': separator})
184
185
    def get_list_item(self, key, index=0, default=None, separator=','):
186
        """
187
        Get list by key or keypath and return value at the specified index.
188
        If separator is specified and list value is a string it will be splitted.
189
        """
190
        values = self.get_list(key, None, separator)
191
        if values:
192
            try:
193
                value = values[index]
194
                return value
195
            except IndexError:
196
                return default
197
        else:
198
            return default
199
200
    def get_phonenumber(self, key, country_code=None, default=None):
201
        """
202
        Get phonenumber by key or keypath and return a dict
203
        with different formats (e164, international, national).
204
        If country code is specified (alpha 2 code),
205
        it will be used to parse phone number correctly.
206
        """
207
        return self._get_value(
208
            key, default or {}, None, parse_util.parse_phonenumber,
209
            {'country_code': country_code})
210
211
    def get_slug(self, key, default='', choices=None):
212
        """
213
        Get value by key or keypath trying to return it as slug.
214
        If choices and value is in choices return value otherwise default.
215
        """
216
        return self._get_value(
217
            key, default, choices, parse_util.parse_slug)
218
219
    def get_slug_list(self, key, default=None, separator=','):
220
        """
221
        Get value by key or keypath trying to return it as list of slug values.
222
        If separator is specified and value is a string it will be splitted.
223
        """
224
        return self._get_values_list(
225
            key, default, separator, parse_util.parse_slug)
226
227
    def get_str(self, key, default='', choices=None):
228
        """
229
        Get value by key or keypath trying to return it as string.
230
        Encoding issues will be automatically fixed.
231
        If choices and value is in choices return value otherwise default.
232
        """
233
        return self._get_value(
234
            key, default, choices, parse_util.parse_str)
235
236
    def get_str_list(self, key, default=None, separator=','):
237
        """
238
        Get value by key or keypath trying to return it as list of str values.
239
        If separator is specified and value is a string it will be splitted.
240
        """
241
        return self._get_values_list(
242
            key, default, separator, parse_util.parse_str)
243
244
    def get_uuid(self, key, default='', choices=None):
245
        """
246
        Get value by key or keypath trying to return it as valid uuid.
247
        If choices and value is in choices return value otherwise default.
248
        """
249
        return self._get_value(
250
            key, default, choices, parse_util.parse_uuid)
251
252
    def get_uuid_list(self, key, default=None, separator=','):
253
        """
254
        Get value by key or keypath trying to return it as list of valid uuid values.
255
        If separator is specified and value is a string it will be splitted.
256
        """
257
        return self._get_values_list(
258
            key, default, separator, parse_util.parse_uuid)
259