I am working on translating some code from JavaScript into Python.
In the Javascript I am seeing a lot of processing of multiple conditions inside of a return statement. Here's an example:
return '(' +
Object.entries(query).map((kv) => {
const key = getCols(kv[0], table)
if (kv[0] === '$or') {
return '(' + getQuery(kv[1], table).join(' OR ') + ')'
} else if (kv[0] === '$null') {
return format('%s IS NULL', kv[1])
} else if (kv[0] === '$notnull') {
return format('%s IS NOT NULL', kv[1])
} else if (kv[0] === '$not') {
return 'NOT (' + getQuery(kv[1], table) + ')'
} else if (kv[0] === '$arrayany') {
return Object.entries(kv[1])
.map((x) => {
return format('%s=ANY(%L)', getCols(x[0], table), value(x[1]))
})
.join(' AND ') +
')'
I haven't seen such complex return statements in Python and am not sure how to build an equivalent statement taking into account all the mappings and joins. What is a valid Pythonic way to have such deeply nested return statements?