(weighted_dbs)
Returns a randomly selected database per the weights assigned from
        a list of databases. If any database is standby and its replication has
        more than acceptable delay, that db is dropped from selection

    Args:
        weighted_dbs: a list of tuple of db and the weight.
            [
                ("pgmain", 5),
                ("pgmainstandby", 5)
            ]
Returns a randomly selected database per the weights assigned from
        a list of databases. If any database is standby and its replication has
        more than acceptable delay, that db is dropped from selection
Returns a randomly selected database per the weights assigned from a list of databases . If any database is standby and its replication has more than acceptable delay that db is dropped from selection
def select_db_for_read(weighted_dbs):
    """
    Returns a randomly selected database per the weights assigned from
        a list of databases. If any database is standby and its replication has
        more than acceptable delay, that db is dropped from selection

    Args:
        weighted_dbs: a list of tuple of db and the weight.
            [
                ("pgmain", 5),
                ("pgmainstandby", 5)
            ]

    """
    if not weighted_dbs:
        return

    weights_by_db = {db_: weight for db_, weight in weighted_dbs}
    fresh_dbs = get_databases_for_read_query(set(weights_by_db))

    dbs = []
    weights = []
    for _db, weight in weights_by_db.items():
        if _db in fresh_dbs:
            dbs.append(_db)
            weights.append(weight)

    if dbs:
        return random.choices(dbs, weights=weights)[0]
def select_db_for_read ( weighted_dbs ) : if not weighted_dbs : return weights_by_db = { db_ : weight for db_ , weight in weighted_dbs } fresh_dbs = get_databases_for_read_query ( set ( weights_by_db ) ) dbs = [ ] weights = [ ] for _db , weight in weights_by_db . items ( ) : if _db in fresh_dbs : dbs . append ( _db ) weights . append ( weight ) if dbs : return random . choices ( dbs , weights = weights ) [ 0 ]