What if there are times when you cannot guarantee or prove that you have 100% control over the type of a value?
Is there a convenient way to write something like type inference? If anyone knows, please let me know. Is it possible to convert dict to struct? What is good? I felt that the ambiguous type was annoying **, so I decided to think again.
Suppose you have dict data called test
as in the example below.
I want to check if there is a 1
in the key
of each value of the test
variable.
test_dict_key_value_type.py
test = {
0: {'key': [1, 2, 3]},
1: {'key': None},
2: {'key': []},
3: {},
4: None,
}
#Pattern 1 Use is instance to check type and use get to determine
for k, v in test.iteritems():
if v is not None and isinstance(v.get('key'), list) and 1 in v.get('key'):
print k, ':found!'
#Pattern 2 Prepare a function to redefine the data
def set_default_dict_value_type(test, dict_value_key, dict_value_type):
for k, v in test.iteritems():
if v is None or not isinstance(v.get(dict_value_key), dict_value_type):
test[k] = {
dict_value_key: dict_value_type()
}
return test
# new_Since all the key values of test are dict, they can be used only with the get method.
for k, v in set_default_dict_value_type(test.copy(), 'key', list).iteritems():
if 1 in v.get('key'):
print k, ':found!'
#Pattern 3 Try and ignore
for k, v in test.iteritems():
try:
if 1 in v.get('key'):
print k, ':found!'
except:
pass
Try to run
bash
$ python test_dict_key_value_type.py
0 :found!
0 :found!
0 :found!
In this case, if you think about it carefully, the problem is that the definition of data is ambiguous **. So I came to the conclusion that ** redefining the data type would solve the problem **. In other words, if the type is different from the schedule, it is considered that the data is corrupted, and it is okay to redefine the type. In the above case, Case 2
is the preferred method at this stage.
I made a sample module that checks list and dict. Are there any improvements yet?
data_type.py
#!/usr/bin/env python
def set_dict_key_value(dic, dict_value_key, dict_value_type):
"""
:param dict dic: dict data to test
:param str dict_value_key: dict value key name
:param Type dict_value_type: Type Object ex. dict, list...
:rtype: dict
:return: redefined dict data
ex.
>>> test = {
0: {'key': [1, 2, 3]},
1: {'key': None},
2: {'key': []},
3: {},
4: None,
}
>>> set_dict_key_value(test, 'key', list)
{
0: {'key': [1, 2, 3]},
1: {'key': []},
2: {'key': []},
3: {'key': []},
4: {'key': []}
}
"""
new_dic = dic.copy()
for k, v in new_dic.iteritems():
if v is None or not isinstance(v.get(dict_value_key), dict_value_type):
new_dic[k] = {dict_value_key: dict_value_type()}
return new_dic
def set_list_value(lst, list_value_type, default_none_value=None):
"""
:param list lst: list data to test
:param Type list_value_type: Type Object ex. dict, list,,,
:param not None clean_list_default_value: Any type of data except for None
:rtype: list
:return: redefined list data
ex.
>>> a = [None, 1, 'str', False, 1.0, {1: 0}]
>>> set_list_value(a, dict, {})
[{}, {}, {}, {}, {}, {1: 0}]
"""
return map(
lambda v: v if isinstance(v, list_value_type) else list_value_type(),
lst if default_none_value is None else
clean_list_none_value(lst, default_none_value)
)
def clean_list_none_value(lst, default_none_value):
"""
Replace None with defalut_none_value
:param list lst: list data to test
:param not None default_none_value: Any type of data except for None
:rtype: list
:return: cleaned list data
"""
return map(lambda v: default_none_value if v is None else v, lst)
Recommended Posts