-1

What is the best way to modify this function to allow for multiple 'filters'? the 'filter_column' parameter can be one or more values. Each value would need to be passed into the request.filters argument.

def create_data_policy(dataset_id, filter_column, filter_value, user_id):
    filter = PolicyFilter()
    filter.column = filter_column
    filter.operator = FilterOperator.EQUALS
    filter.values = [filter_value]
    request = Policy()
    request.filters = [filter]
    request.name = f'Filter | {filter_value}'
    request.type = PolicyType.USER
    request.users = [user_id]

This function works fine if I define a single filter_column and a list of filter_values

create_data_policy(dataset_id=123, filter_column='employee_name', filter_value='Jane', user_id='1243')

What if I want to create multiple filters? The request.filters accepts a list of potential filters.

request.filters = [filter1, filter2, filter3]

Edit - here is the solution I implemented using kwargs. Does it make sense how I looped through the key, value and appended them to a list?

def test_func(dataset_id, **kwargs):
    final_pdp = []
    for key, value in kwargs.items():
        pdp_filter = f'{key}_filter'
        pdp_filter = PolicyFilter()
        pdp_filter.column = key
        pdp_filter.operator = FilterOperator.EQUALS
        pdp_filter.values = [f'{key}: {value}']
        pdp_filter.values = value
        final_pdp.append(pdp_filter)
    pdp_request = Policy()
    pdp_request.filters = final_pdp
    pdp_request.type = PolicyType.USER
    pdp = create_pdp(dataset_id, pdp_request)
    print(f"Created a Personalized Data Policy (PDP): {pdp['id']}")

test_func(dataset_id='test', project=['Test'], location=['NY'])
trench
  • 5,075
  • 12
  • 50
  • 80

2 Answers2

1

Try using *args and **kwargs.

>>> def f(*args):
...     print(args)
...
>>> f(1, 2, 3, 4)
(1, 2, 3, 4)

>>> def f(**kwargs):
...     print(kwargs)
...
>>> f(a=1, b=2, c=3)
{'a': 1, 'b': 2, 'c': 3}
chris.mclennon
  • 966
  • 10
  • 25
  • Thanks, I never wrapped my head around args and kwargs, but I tested it out (admittedly, I just kept printing the results over and over until it looked right). I pasted my final solution up top, if you don't mind taking a look. – trench Sep 14 '17 at 13:41
  • @trench Without knowing full context, the edit largely looks okay to me. I notice a few things that are not entirely related to the kwargs: You set `pdp_filter.values = [f'{key}: {value}']` and then immediately overwrite it to `pdp_filter.values = value`. Same with `pdp_filter = f'{key}_filter'` and `pdp_filter = PolicyFilter()`. For `PolicyFilter()`, can the constructor accept column, operator, and values there so you can make your object in one statement instead of 4? – chris.mclennon Sep 14 '17 at 19:07
  • 1
    At the end I think it would be slightly more readable if it looked something like: `final_pdb = [PolicyFilter(column=key, operator=FilterOperator.EQUALS, values=value) for key, value in kwargs.items()]` – chris.mclennon Sep 14 '17 at 19:13
  • Much better and cleaner, thanks a lot. – trench Sep 18 '17 at 16:48
1

Have you considered a dictionary? Yo could pass a dictionary with the column names as keys and the list of filters as values.

lexotero
  • 66
  • 4