0

I'm sorry in advance for my English, it is not my native speaking language. I'm running this in Python

I have a XML-RPC function and, as part of its parameters, there are some filters that you can pass to retrieve specific information. It goes something like this:

function(arg1,arg2,[[[condition1],[condition2],[condition3]]])

The conditions are optional, but I want to know if there is a way to pass n-amount of conditionals according to user request. For example, if the user wants the information from a specific day, it would go in [condition1], but if they want also the info from another non-adjacent day, they would pass that as [[[condition1],[condition2]]]. Furthermore, each condition would be a specific that but those two dates might have a lot of info, so it would be slow if it retrieve info in two different processes and also if it wants a lot of days the server might block the consult so it would be better to add parameters according what the user wants.

The user won't pass things directly to the filter, rather, they would use the filter through a UI and then the values will be parsed. I have implemented it this way to limit the possibility of a user breaking my database.

In the dates scenario, I've tried to check the distance between dates and use and if to try to retrieve the info with as few calls as possible, but when the user passes other parameters like a name, a boolean or a combination of them it gets more tricky to solve it with an if.

I thought maybe something like checking the filters the user wants, arranging the correctly in a string, and finally using it as the parameter might work, but I don't know how to parse it. Something like this:

The User input will be

date: 2019/03/28

Published: True

price: > 850

So then the function would be written as:

function(arg1,arg2,[[[date='2019/03/28'],[Published=True],[price>850]]])

But if the user only wants two filters, the function needs to be written like this:

function(arg1,arg,[[[Published=True],[price>850]]])

So I don't know how to add n parameters corresponding the n filters the user want

1
  • add date as a list, so one day is a one item list, while more days is a more itme list Commented Mar 27, 2023 at 16:16

1 Answer 1

3

I might be a little quick to answer, but this feels like Kwargs would do everything you require. Kwargs, or Keyword arguments, allow you to pass an undefined amount of parameters. It's very similar to a dictionary.

https://book.pythontips.com/en/latest/args_and_kwargs.html

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

2 Comments

The function is already defined with it as function(*args) My problem is rather that, for the conditions of what to search, I don't know how to "autowrite" them when I call the function. For example: if the user has 2 conditions for search I need to pass them separately with an specific format so it would look like this: function([date,'=','23-02-01'],[Published,'=',True]) But if the user wants three conditions then it would look like: function([date,'=','23-02-01'],[Published,'=',True],[Paid,'=',True])
I thought that maybe I could parse those filters as a string so no matter how many filters the user wants I still can format them but I'm not sure how to parse that string as the args of the function

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.