0

I am building a URL query, that allows the filtering of multiple fields. The filtering (e.g. from a form) is stored as pair values: column and FILTER. I am storing the filters as a list of pairs.

I want to join a list of paired values (column, FILTER) into a url string.

Sample:

"$filter=contains(column1%2C%27FILTER1%27)+and+contains(column2%2C%27FILTER2%27)+and+contains(column3..."
filter_lists = [['level', 'FIRST'], ['name', 'OFFICE'], ['number', 'FIVE']]
filter1 = f"contains({filter_lists[0][0]}%2C%27{filter_lists[0][1]}%27)"
filter_rest = [[f"+and+contains({item}%2C%27" for item in filter_lists[1:]] for subitem in filter_lists[0]][0]

filter_all = "".join(filter_rest)
url_str = f"$filter={filter1}{filter_all}"

url_str returns:

"$filter=contains(level%2C%27FIRST%27)+and+contains(['name', 'OFFICE']%2C%27+and+contains(['number', 'FIVE']%2C%27"

desired string:

"$filter=contains(level%2C%27FIRST%27)+and+contains(name%2C%27OFFICE%27)+and+contains(number%2C%27FIVE%27)"

Is there a simpler way instead of storing the columns and filters in a list[list]?

1
  • 1
    Simple one-liner: url_str = f"$filter={'+and+'.join(f'contains({c}%2C%27{v}%27)' for c, v in filter_lists)}". Commented Sep 24, 2024 at 10:46

3 Answers 3

0
filter_lists = [['level', 'FIRST'], ['name', 'OFFICE'], ['number', 'FIVE']]
filter1 = f"contains({filter_lists[0][0]}%2C%27{filter_lists[0][1]}%27)"
filter_rest = [[f"+and+contains({item[0]}%2C%27{item[1]}%27)" for item in filter_lists[1:]] for subitem in filter_lists[0]][0]

filter_all = "".join(filter_rest)
url_str = f"$filter={filter1}{filter_all}"

returns: $filter=contains(level%2C%27FIRST%27)+and+contains(name%2C%27OFFICE%27)+and+contains(number%2C%27FIVE%27)

Sign up to request clarification or add additional context in comments.

1 Comment

markus1998, thanks for your code sample! Would you consider adding a little more explanation for what it does/how it works? This will help both the original asker and anyone who comes across this post understand the concepts and tools behind it.
0
filter_template = "contains({}%2C%27{}%27)"
filter_conditions = [["level", "FIRST"], ["name", "OFFICE"], ["number", "FIVE"]]
formatted_filters = [filter_template.format(f, v) for f, v in filter_conditions]
filter_query = "$filter=" + "+and+".join(formatted_filters)

Output:

"$filter=contains(level%2C%27FIRST%27)+and+contains(name%2C%27OFFICE%27)+and+contains(number%2C%27FIVE%27)"

1 Comment

e-motta, thanks for your code sample! Would you consider adding a little more explanation for what it does/how it works? This will help both the original asker and anyone who comes across this post understand the concepts and tools behind it.
-1

To keep the strings more readable try to make a kind of template by using {} as placeholder in a string and evaluate them with format. See here for deeper explanations and examples of usage and below a possible solution.

filter1 = "contains({}%2C%27{}%27)".format(*filter_lists[0])

filter_template = "+and+contains({}%2C%27{}%27)"
filter_rest = (filter_template + filter_template).format(*filter_lists[1], *filter_lists[2])

url_str = f"$filter={filter1}{filter_rest}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.