B
B
bfesiuk2020-08-02 12:54:33
Python
bfesiuk, 2020-08-02 12:54:33

Why am I getting add_advert() got multiple values ​​for argument 'connection'?

Good day to all!

I'm making a small script for myself with a record in the database. I want to make record in a DB in some flows. Now it works in one thread, but I get this error:

add_advert() got multiple values ​​for argument 'connection'


What could be the problem?

Source:
def db_connect(func):
    """ decorator to connect to the database
    """
    def inner(*args, **kwargs):
        with sqlite3.connect('adverts.db') as connection:
            kwargs['connection'] = connection
            response = func(*args, **kwargs)
        return response
    return inner


@db_connect
def add_advert(connection, source_link: str, user_since: str, user_name: str, user_link: str, advert_name: str,
                advert_text: str, address: str, phone_num: int, advert_price: float, advert_currency: str):
    """
    add data to db
    :param connection: connector to db
    :param source_link: link to advert
    :param user_since: user registration date
    :param user_name: user name
    :param user_link: link to user profile
    :param advert_name: name of advert
    :param advert_text: description of advert
    :param address: user address
    :param phone_num: user phone number
    :param advert_price: advert price
    :param advert_currency: advert currency
    """
    c = connection.cursor()
    c.execute('INSERT INTO adverts (source_link, user_since, user_name, user_link, advert_name, advert_text\
    address, phone_num, advert_price, advert_currency) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
              (source_link, user_since, user_name, user_link, advert_name, advert_text, address, phone_num,
               advert_price, advert_currency))

    # save editing
    connection.commit()

add_advert(source_link, user_since, user_name, user_link, advert_name, advert_text, address, phone_num,
                    advert_price, advert_currency)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-08-02
@bfesiuk

Do print(args, kwargs) and see that everything is in args, and adding connection to kwargs just makes multiple values ​​for this argument. Well, the idea itself is so implicitly transferred to the connection function, so-so.
PS you can do it like that response = func(connection, *args, **kwargs)but anyway, so-so idea

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question