How to conveniently get bool value back from cypher query through driver api?

For example:

with driver.session() as session:
        model_exists = session.run("""CALL gds.beta.model.exists('nc-model') YIELD exists""")
        for model_exist in model_exists:
            model_exists = model_exist[0]
            break

        if model_exists:
            print("Model  exists, which have to be dropped first!")
            session.run("""CALL gds.beta.model.drop('nc-model')""")

        print('Model dropped.')

In this code, is there a better way to get the single bool value out from the session's query, instead of doing this:

for model_exist in model_exists:
            model_exists = model_exist[0]
            break

Hello @lingvisa :slight_smile:

with driver.session() as session:
    model_exists = session.run("CALL gds.beta.model.exists('nc-model') YIELD exists").single()[0]
    if model_exists:
        print("Model exists, which have to be dropped first!")
        session.run("CALL gds.beta.model.drop('nc-model')")

    print('Model dropped.')

Regards,
Cobra

@cobra , I tested, if you simply use 'if model_exists' without using the for loop to extract the bool value, the if checking always returns true, which is incorrect. 'Model_exists' is a Result object, not a bool. Could you test it?

Did you see the single()[0] at the end of the row?

1 Like