James Corden
James Corden

Reputation: 17

Python IF statement when number of IFs is unknown

So I'm not sure if the question is clear enough so let me provide an example.

If I have a database with 2 rows, I can do something like this

if something_row_1():
    #do_something
if something_row_2():
    #do_something

But there are also functions in my code that allow you to add rows to the database. So you can't know how many IFs you need at the moment you run your program.

Is there any way to do anything with that without using SQL syntax but using Python only?

EDIT: I'm using a SQLite

Upvotes: 0

Views: 345

Answers (2)

Mahesh Mogal
Mahesh Mogal

Reputation: 658

I think it is possible. You can use loop and list for this.

rows = get_list_of_rows_from_db()
do_something = get_do_something_list()
for r in range(0,len(r)):
    if rows[r]:
        do_something[r]

Upvotes: 1

Ettore Galli
Ettore Galli

Reputation: 751

I dont't know if this answers your question but you can put together a set of rules that are quite easy to maintain and provide some flexibility over the number of records.

If I have well understood you have to read database row and, for each row, test a different condition and perform a different action.

If this is the case, you can do as in the following code:

# Suppose  you read the database and get the rows
# (Adapt your code to actual rows format)
rows = [
    ("q1", "w1", "e1", "r1"),
    ("q2", "w2", "e2", "r2"),
    ("q3", "w3", "e3", "r3"),
]


# Suppose then that the index of the row says which condition to apply, .e. you know that row1 leads to condition 1 etc...

# Then you can write a "router

# You can define tests for known conditions:

def something_row_1(row):
    # FOR EXAMPLE: (return your condition)
    return row[1] == "w1"


def something_row_2(row):
    # FOR EXAMPLE: (return your condition)
    return row[2] == "e2"


# And one for all unknown conditions
def default_test(row):
    return True  # or whatever


tests = [
    something_row_1,
    something_row_2,
]


# Define the corresponding actions:
def known_action_1(row):
    # FOR EXAMPLE: (perform your own action)
    print("Action 1 for row", row)


def known_action_2(row):
    # FOR EXAMPLE: (perform your own action)
    print("Action 2 for row", row)


def default_action(row):
    # FOR EXAMPLE: (perform your own action)
    print("Default action")


actions = [
    known_action_1,
    known_action_2,
]

# You can then route your actions
def route_actions(rows):
    for i, row in enumerate(rows):
        if i < len(tests):
            # Get the test to use
            test = tests[i]
            # Get the action
            action = actions[i]
            # Do the corresponding action
        else:
            test = default_test
            action = default_action

        if test(row):
            action(row)


route_actions(rows)

The output is:

Action 1 for row ('q1', 'w1', 'e1', 'r1')
Action 2 for row ('q2', 'w2', 'e2', 'r2')
Default action

You can define known actions for known rows and default actions for new/unknown rows.

Hope this answers your question.

Upvotes: 0

Related Questions