logoalt Hacker News

wis11/20/20241 replyview on HN

Yea, In my opinion using Python's list comprehension is more readable and code checkable.

Here's the usage example from the README:

  from leopards import Q
  l = [{"name":"John","age":"16"}, {"name":"Mike","age":"19"},{"name":"Sarah","age":"21"}]
  filtered= Q(l,{'name__contains':"k", "age__lt":20})
  print(list(filtered))
Versus:

  [x for x in l if ('k' in x['name'] and int(x['age']) < 20)]
Outputs:

  [{'name': 'Mike', 'age': '19'}]
Also from the readme:

  > Even though, age was str in the dict, as the value of in the query dict was int, Leopards converted the value in dict automatically to match the query data type. This behaviour can be stopped by passing False to convert_types parameter.
I don't like this default behavior.

Replies

WesleyJohnson11/20/2024

That's a bit biased, no? The actual comparison should be:

  filtered = Q(l,{'name__contains':"k", "age__lt":20})
Verus:

  filtered = [x for x in l if ('k' in x['name'] and int(x['age']) < 20)]
show 1 reply