mirror of
https://gitlab.com/bramw/baserow.git
synced 2025-04-16 01:54:12 +00:00
67 lines
1.8 KiB
Python
67 lines
1.8 KiB
Python
def extract_allowed(values, allowed_fields):
|
|
"""
|
|
Returns a new dict with the values of the key names that are in the allowed_fields.
|
|
The other keys are ignored.
|
|
|
|
Example:
|
|
object_1 = {
|
|
'value_1': 'value',
|
|
'value_2': 'value'
|
|
}
|
|
|
|
extract_allowed(object_1, ['value_1'])
|
|
>> {'value_1': 'value'}
|
|
|
|
:param values: A dict containing the values.
|
|
:type dict:
|
|
:param allowed_fields: A list containing the keys of the values that need to be
|
|
extracted from the values.
|
|
:type allowed_fields: list
|
|
:return: The extracted values.
|
|
:rtype: dict
|
|
"""
|
|
|
|
allowed_values = {}
|
|
for field in allowed_fields:
|
|
if field in values:
|
|
allowed_values[field] = values[field]
|
|
|
|
return allowed_values
|
|
|
|
|
|
def set_allowed_attrs(values, allowed_fields, instance):
|
|
"""
|
|
Sets the attributes of the instance with the values of the key names that are in the
|
|
allowed_fields. The other keys are ignored.
|
|
|
|
Examples:
|
|
class Tmp(object):
|
|
value_1 = 'value'
|
|
value_2 = 'value'
|
|
|
|
object_1 = {
|
|
'value_1': 'value_2',
|
|
'value_2': 'value_2'
|
|
}
|
|
|
|
tmp = set_allowed_attrs(object_1, ['value_1'], Tmp())
|
|
tmp.value_1
|
|
>> 'value_2'
|
|
tmp.value_2
|
|
>> 'value'
|
|
|
|
:param values: The dict containing the values.
|
|
:type values: dict
|
|
:param allowed_fields: A list containing the keys of the value that need to be set
|
|
on the instance.
|
|
:type allowed_fields: list
|
|
:param instance: The instance of which the attributes must be updated.
|
|
:type instance: object
|
|
:return: The updated instance.
|
|
"""
|
|
|
|
for field in allowed_fields:
|
|
if field in values:
|
|
setattr(instance, field, values[field])
|
|
|
|
return instance
|