I have a JSON "database" - it's a python list of JSON objects:
[{'_id': 'TRANSACTION0', 'Offer': {'From': 'merchant1', 'To': 'customer1', 'Item': 'Car', 'Price': 1000, 'Timestamp': 2}, 'Accept': {'Quantity': 1, 'Address': '123 Fake Street', 'Timestamp': 5}},
{'_id': 'TRANSACTION1', 'Offer': {'From': 'merchant1', 'To': 'customer2', 'Item': 'Computer', 'Price': 500, 'Timestamp': 5}},
{'_id': 'TRANSACTION2', 'Offer': {'From': 'merchant3', 'To': 'customer3', 'Item': 'Garbage bin', 'Price': 10, 'Timestamp': 0}, 'Accept': {'Quantity': 2, 'Address': '456 MadeUp Road', 'Timestamp': 1}},
{'_id': 'TRANSACTION3', 'Offer': {'From': 'merchant2', 'To': 'customer1', 'Item': 'Car', 'Price': 2000, 'Timestamp': 3}, 'Accept': {'Quantity': 2, 'Address': 'The White House', 'Timestamp': 3}},
{'_id': 'TRANSACTION4', 'Offer': {'From': 'merchant3', 'To': 'customer3', 'Item': 'Pens', 'Price': 2, 'Timestamp': 0}, 'Accept': {'Quantity': 4, 'Address': 'Houses of Parliment', 'Timestamp': 1}},
{'_id': 'TRANSACTION5', 'Offer': {'From': 'merchant4', 'To': 'customer1', 'Item': 'Headphones', 'Price': 200, 'Timestamp': 4}},
{'_id': 'TRANSACTION6', 'Offer': {'From': 'merchant1', 'To': 'customer2', 'Item': 'Water Bottle', 'Price': 1, 'Timestamp': 1}, 'Accept': {'Quantity': 3, 'Address': 'Timbuktu', 'Timestamp': 14}},
{'_id': 'TRANSACTION7', 'Offer': {'From': 'merchant2', 'To': 'customer3', 'Item': 'Laptop', 'Price': 900, 'Timestamp': 0}},
{'_id': 'TRANSACTION8', 'Offer': {'From': 'merchant4', 'To': 'customer1', 'Item': 'Chair', 'Price': 80, 'Timestamp': 3}, 'Accept': {'Quantity': 1, 'Address': 'Mordor', 'Timestamp': 3}},
{'_id': 'TRANSACTION9', 'Offer': {'From': 'merchant3', 'To': 'customer3', 'Item': 'Garbage bin', 'Price': 5, 'Timestamp': 2}, 'Accept': {'Quantity': 2, 'Address': 'The wall', 'Timestamp': 2}}]
My intention is to use queries, which will be stored in dictionaries, against this database. In this example, the dictionary contains:
a_dict = {"query1": "'Offer' and 'Accept'"}
Note that the dictionary will contain more queries, and also more complicated queries (e.g. (cond1 and cond2) or (cond2 and cond3)
), but I need to understand why Python is doing what it's doing (and how to overcome it) as opposed to solely what the solution is.
I need to have something which evaluates and runs query1
correctly. My wrongful implementation is currently:
if (eval(a_dict["query1"]) + "in i"):
This is the same as:
if 'Offer' and 'Accept' in i:
Due to short-circuiting, this evaluates to only checking whether Accept
is in i
. In this example, everytime there is an Accept
there is an Offer
, but this may not always be the case.
A rightful if statement would be:
if 'Offer' in i and 'Accept' in i:
However, this isn't easily composable from the type of potential queries I would have. Ideally, I'd like to have an elegant solution which was "plug and play", similar to my eval
if statement given above.
Is there anyway to be able take a particular query from a dictionary, plug that into an if
statement, and then run that if
statement as I'm intending (under the assumption that all the queries make logical sense)?
https://www.python.org/dev/peps/pep-0308/ This article says FAQ 4.16 gives alternatives, but I can't seem to find it anywhere