Tuesday 2 September 2014

Use of "l" as long integer identifier :: Pylint :: W0332


Error:

lowercase-l-suffix, Use of "l" as long integer identifier

Description

Raised by pylint, Use of “l” as long integer identifier Used when a lower case “l” is used to mark a long integer. You should use a upper case “L” since the letter “l” looks too much like the digit “1”

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. We have calculated division in case of inequality while constant 1 as long integer for equality case.

def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        if a != b:
            result = a/b
        else:
            result = 1l
    except ZeroDivisionError:
        result = 0
    return result



Solution(s)

This could be resolved using Capital "L" to represent long Integer.

Use "L"


def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        if a != b:
            result = a/b
        else:
            result = 1L
    except ZeroDivisionError:
        result = 0
    return result

old-ne-operator :: Pylint :: W0331


Error:

old-ne-operator, Use of the <> operator

Description

Raised by pylint, when old inequality operator <> is used. This error is not fatal but considered a bad practice and could result in unexpected behavior.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. We have calculated division in case of inequality while constant 1 for equality case.

def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        if a <> b:
            result = a/b
        else:
            result = 1
    except ZeroDivisionError:
        result = 0
    return result



Solution(s)

This could be resolved using standard != for testing not equal.

Use !=


def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        if a != b:
            result = a/b
        else:
            result = 1
    except ZeroDivisionError:
        result = 0
    return result



Monday 1 September 2014

mixed-indentation :: Pylint :: W0312


Error:

Used when there are some mixed tabs and spaces in a module.

Found indentation with tabs instead of spaces
Found indentation with spaces instead of tabs

Description

Raised by pylint, when a mix of both spaces and tabs is used for indentation purpose. Precisely, when an indent is detected that is not consistent with the indent-string option. By default, indent-string is set to four spaces, the style of indentation recommended in PEP-8. This error is not fatal but considered a bad practice and could result in unexpected behavior.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. We have used spaces and tab both to represent  as single indentation

def divide(a, b):
        """
        function to take two arguments to divide first by second
        """
        try:
            result = a/b
        except ZeroDivisionError:
            result = 0
        return result


Solution(s)

PEP recommend four spaces to be used as indentation, So this could be resolved using standard four spaces for indentation.

Use Four spaces
Use four spaces for single indentation.

    def divide(a, b):
        """
        function to take two arguments to divide first by second
        """
        try:
            result = a/b
        except ZeroDivisionError:
            result = 0
        return result


Bad indentation :: Pylint :: W0311


Error:

Bad indentation

Description

Raised by Pylint, when an  statements use indentations that are not standard four spaces. PEP recommend 4 spaces to be used as single indent. This warning is shown when other then four spaces are used as indentation. This error is not fatal but considered a bad practice.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. We have used eight spaces as single indentation

def divide(a, b):
        """
        function to take two arguments to divide first by second
        """
        try:
                result = a/b
        except ZeroDivisionError:
                result = 0
        return result


Solution(s)

PEP recommend four spaces to be used as indentation, So this could be resolved using standard four spaces for indentation.

Use Four spaces
Use four spaces for single indentation.

    def divide(a, b):
        """
        function to take two arguments to divide first by second
        """
        try:
            result = a/b
        except ZeroDivisionError:
            result = 0
        return result


Unnecessary semicolon :: Pylint :: W0301


Error:

Unnecessary semicolon

Description

Raised by Pylint, when an  statement ends with semicolon. semicolon is not required in python to end statement. This error is not fatal but considered a bad practice.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. We have putted semicolon to end simple division syntax, which is a C or perl style to end statement. Although their existence does not create problem and will execute smoothly.

    def divide(a, b):
        """
        function to take two arguments to divide first by second
        """
        try:
            result = a/b;
        except ZeroDivisionError:
            result = 0
        return result

Solution(s)

Semicolons are not needed in Python unless using multiple statements in single line, This idea is too considered poor practice. This style is used in old languages like c or perl, where semicolons are always required after each statement.

Remove Semicolon
Remove semicolon from then end of line, in-case of multiple statements Use more specific exceptions like  ZeroDivisionError, TypeError.

    def divide(a, b):
        """
        function to take two arguments to divide first by second
        """
        try:
            result = a/b
        except ZeroDivisionError:
            result = 0
        return result


Catching too general exception :: Pylint :: W0703


Error:

Catching too general exception

Description

Raised by Pylint, when an except clause doesn't specify exceptions type to catch and use general Exception. This error is not fatal but considered a bad practice, good practice require to catch specific exception like ZeroDivisionError.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. Although their exist potential exception like ZeroDivisionError, we have used general exception to catch errors.

def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        result = a/b
    except Exception as ex:
        result = 0
    return result

Solution(s)

Good programming practice is to catch very specific range of types. Using general Exception without specifying type of exception will result in catching not just the errors you planned for, but other errors too, this may mask bugs in your code that would be quicker to diagnose if they weren't caught at all, or possibly would be better dealt with by a single very high level exception handler.

Use  Specific Exceptions
Use more specific exceptions like  ZeroDivisionError, TypeError.

def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        result = a/b
    except ZeroDivisionError:
        result = 0
    except TypeError:
        result = 0
    return result

Friday 29 August 2014

No exception type(s) specified :: Pylint :: W0702

Error:

No exception type(s) specified

Description

Raised by Pylint, when an except clause doesn't specify exceptions to catch. This error is not fatal but considered a bad practice, good practice require to catch specific exception like ZeroDivisionError.

Example(s)

Given sample of code describe basic divide function, it takes two arguments a and b. We have simple divided first argument with second and returned result. Although their exist potential exception like ZeroDivisionError, we have used general exception to catch errors.

def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        result = a/b
    except:
        result = 0
    return result

Solution(s)

Good programming practice is to catch only a very specific range of types. Since every error in Python raises an exception, using except: can make many programming errors look like runtime problems, which hinders the debugging process. This may mask bugs in your code that would be quicker to diagnose if they weren't caught at all, or possibly would be better dealt with by a single very high level exception handler. Because except: catches all exceptions, including SystemExit, KeyboardInterrupt, and GeneratorExit (which is not an error and should not normally be caught by user code), using a bare except: is almost never a good idea. In situations where you need to catch all “normal” errors, such as in a framework that runs callbacks, you can catch the base class for all normal exceptions, Exception. Unfortunately in Python 2.x it is possible for third-party code to raise exceptions that do not inherit from Exception, so in Python 2.x there are some cases where you may have to use a bare except: and manually re-raise the exceptions you don’t want to catch.

Use  Specific Exceptions
Use more specific exceptions like  ZeroDivisionError, TypeError.
def divide(a, b):
    """
    function to take two arguments to divide first by second
    """
    try:
        result = a/b
    except ZeroDivisionError:
        result = 0
    except TypeError:
        result = 0
    return result