R1714 python ошибка

Back to top

Edit this page

Toggle table of contents sidebar

Message emitted:

Consider merging these comparisons with 'in' by using '%s %sin (%s)'. Use a set instead if elements are hashable.

Description:

To check if a variable is equal to one of many values, combine the values into a set or tuple and check if the variable is contained «in» it instead of checking for equality against each of the values. This is faster and less verbose.

Problematic code:

def fruit_is_round(fruit):
    # +1: [consider-using-in]
    return fruit == "apple" or fruit == "orange" or fruit == "melon"

Correct code:

def fruit_is_round(fruit):
    return fruit in {"apple", "orange", "melon"}

Created by the refactoring checker.

Back to top

Edit this page

Toggle table of contents sidebar

Message emitted:

Consider merging these comparisons with 'in' by using '%s %sin (%s)'. Use a set instead if elements are hashable.

Description:

To check if a variable is equal to one of many values, combine the values into a set or tuple and check if the variable is contained «in» it instead of checking for equality against each of the values. This is faster and less verbose.

Problematic code:

def fruit_is_round(fruit):
    return fruit == "apple" or fruit == "orange" or fruit == "melon"  # [consider-using-in]

Correct code:

def fruit_is_round(fruit):
    return fruit in {"apple", "orange", "melon"}

Created by the refactoring checker.

Search code, repositories, users, issues, pull requests…

Provide feedback

Saved searches

Use saved searches to filter your results more quickly

Sign up

My Code :

def modify_book():
        db = mysql.connector.connect(host='localhost',
                                     database='library',
                                     user='Aishwary_Pandey',
                                     password='aishwary')
        c = db.cursor()
        clear()
        print('M O D I F Y  --  B O O K  --  D E T A I L S')
        print('-'*120)
        print('\n1. Book Title')
        print('\n2. Book Author')
        print('\n3. Book Publisher')
        print('\n4. Book Pages')
        print('\n5. Book Price')
        print('\n6. Book Edition')
        print('\n7. Exit')
        print('\n\n')
        choice = int(input('Enter your choice : '))
        field = ''
        if choice == 1:
            field = 'title'
        if choice == 2:
            field = 'author'
        if choice == 3:
            field = 'publisher'
        if choice == 4:
            field = 'pages'
        if choice == 5:
            field = 'price'
        if choice == 6:
            field = 'edition'
    
        book_id = input('Enter ID of the Book : ')
        value = input('Enter the Value to be Updated : ')
        if field == 'pages' or field == 'price':
            sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
        else:
            sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'

error :
basically there is no error but my idle (spyder) suggested me to merge these comparisons with «in» to «field in (‘pages’, ‘price’)» which I am not able to understand how would I do that.

please help.

  • python-3.x
  • spyder
  • anaconda3

asked Mar 5, 2021 at 6:57

Aishwary's user avatar

1 Answer

This should «fix» it

if field in ('pages', 'price'):
    sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
else:
    sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'

answered Aug 25, 2021 at 13:17

Sean Sadoun's user avatar

Back to top

Edit this page

Toggle table of contents sidebar

Message emitted:

Consider merging these comparisons with 'in' by using '%s %sin (%s)'. Use a set instead if elements are hashable.

Description:

To check if a variable is equal to one of many values, combine the values into a set or tuple and check if the variable is contained «in» it instead of checking for equality against each of the values. This is faster and less verbose.

Problematic code:

def fruit_is_round(fruit):
    # +1: [consider-using-in]
    return fruit == "apple" or fruit == "orange" or fruit == "melon"

Correct code:

def fruit_is_round(fruit):
    return fruit in {"apple", "orange", "melon"}

Created by the refactoring checker.

Permalink

Cannot retrieve contributors at this time

R1714 (consider-using-in)

❌ Problematic code:

array = list(range(10))
n = input()

for elem in array:
    if int(n) == elem:
        pass

✔️ Correct code:

array = list(range(10))
n = input()

if int(n) in array:
    pass

Rationale:

To check if a variable is equal to one of many values,combine the values into
a tuple and check if the variable is contained in it instead of checking
for equality against each of the values. This is faster and less verbose.

Related resources:

  • Issue Tracker

My Code :

def modify_book():
        db = mysql.connector.connect(host='localhost',
                                     database='library',
                                     user='Aishwary_Pandey',
                                     password='aishwary')
        c = db.cursor()
        clear()
        print('M O D I F Y  --  B O O K  --  D E T A I L S')
        print('-'*120)
        print('n1. Book Title')
        print('n2. Book Author')
        print('n3. Book Publisher')
        print('n4. Book Pages')
        print('n5. Book Price')
        print('n6. Book Edition')
        print('n7. Exit')
        print('nn')
        choice = int(input('Enter your choice : '))
        field = ''
        if choice == 1:
            field = 'title'
        if choice == 2:
            field = 'author'
        if choice == 3:
            field = 'publisher'
        if choice == 4:
            field = 'pages'
        if choice == 5:
            field = 'price'
        if choice == 6:
            field = 'edition'
    
        book_id = input('Enter ID of the Book : ')
        value = input('Enter the Value to be Updated : ')
        if field == 'pages' or field == 'price':
            sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
        else:
            sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'

error :
basically there is no error but my idle (spyder) suggested me to merge these comparisons with «in» to «field in (‘pages’, ‘price’)» which I am not able to understand how would I do that.

please help.

asked Mar 5, 2021 at 6:57

Aishwary's user avatar

This should «fix» it

if field in ('pages', 'price'):
    sql = 'update book set ' + field + ' = '+value+' where id = '+book_id+';'
else:
    sql = 'update book set ' + field + ' = "'+value+'" where id = '+book_id+';'

answered Aug 25, 2021 at 13:17

Sean Sadoun's user avatar

Permalink

Cannot retrieve contributors at this time

consider-using-in (R1714)

Consider merging these comparisons with ‘in’ by using ‘%s %sin (%s)’.
Use a set instead if elements are hashable. To check if a variable is
equal to one of many values, combine the values into a set or tuple and
check if the variable is contained «in» it instead of checking for
equality against each of the values. This is faster and less verbose.

Skip to content

    • GitLab: the DevOps platform
    • Explore GitLab
    • Install GitLab
    • How GitLab compares
    • Get started
    • GitLab docs
    • GitLab Learn
  • Pricing
  • Talk to an expert

  • Help

    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next

    Projects
    Groups
    Topics
    Snippets


  • Register

  • Sign in

P

pylint-errors


Project ID: 19126888

Star
0


Topics:

pylint

pylint-errors

linter

+ 1 more

A curated list of pylint errors with explanation and examples

Find file

Download source code

zip
tar.gz
tar.bz2
tar


Clone

  • Clone with SSH

  • Clone with HTTPS

  • Open in your IDE

    Visual Studio Code (SSH)

    Visual Studio Code (HTTPS)

    IntelliJ IDEA (SSH)

    IntelliJ IDEA (HTTPS)

  • Copy SSH clone URLgit@gitlab.com:britonad/pylint-errors.git
  • Copy HTTPS clone URLhttps://gitlab.com/britonad/pylint-errors.git
  • README
  • MIT License

Понравилась статья? Поделить с друзьями:
  • Qnap ошибка fw00007
  • R1590 ошибка митсубиси
  • R1320 ошибка ниссан
  • R001 ошибка рено меган
  • R08 ошибка сигнализации