Search code examples
pythonpylint

pylint compliance for following code


I have following code from wesley chun's core python application programming book

#!/usr/bin/env python
"""
tcp server
"""

from socket import AF_INET, SOCK_STREAM, socket
from time import ctime

HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)

tcp_server_socket = socket(AF_INET, SOCK_STREAM)
tcp_server_socket.bind(ADDR)
#maximum number of incoming connection before connection is refused
tcp_server_socket.listen(5)

while True:
    print "waiting for connection"
    tcpCliSock, addr = tcp_server_socket.accept()
    print "... connected from:" , addr

    while True:
        DATA = tcpCliSock.recv(BUFSIZE)
        if not DATA:
            break
        tcpCliSock.send('[%s] %s' % (ctime(), DATA))

    tcpCliSock.close()
tcp_server_socket.close()

I did some modifications to the original code however I am still confused how best to modify it to be more compliant

here are all the messages I am getting

C: 14,0: Invalid name "tcp_server_socket" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 21,4: Invalid name "tcpCliSock" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 21,16: Invalid name "addr" (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
E: 25,15: Instance of '_socketobject' has no 'recv' member
E: 28,8: Instance of '_socketobject' has no 'send' member

I suppose first three just want me to use all caps variable names, is that the standard practice for these type of scripts, I don't see the code becoming more readable by using this convention, on the contrary it will look less readable, what are the motivation behind such rule in pylint and how to make code more compliant, I hardly think writer of such stature would write code like this without reason, be it readablity, beginner friendliness or anything else.


Solution

  • The two errors about _socketobject you are seeing are a quirk of how the socket module works. This issue has come up on StackOverflow once before, and the linked question provides a couple of answers to help you get rid of those errors.

    The first three messages you are getting are convention warnings. They are complaining that the names tcp_server_socket, tcpCliSock and addr do not match the regular expression for constant members. Because your code is at 'top-level' (i.e. outside of any functions or classes), members are expected to be constant, and names of constants should match the regular expression given.

    Suppose your Python script was saved in a file tcp_server.py. If you then write import tcp_server either from the Python interpreter or from another Python script, your TCP server will start. This isn't typically what you would want to happen. If you import a module, it can define functions, classes and constants, but it shouldn't run any code.

    I'd recommend moving all of the code from the line tcp_server_socket = socket(....) downwards into a separate function, (let's call it start_server), and then adding to the bottom of your script the following lines:

    if __name__ == "__main__":
        start_server()
    

    These two lines will then start your server if you run the script directly, but not if you import tcp_server from somewhere else.

    Once you've done that, the warnings about variable names will go, but you will get some further convention warnings. Two of them will complain about DATA and tcpCliSock not matching the naming convention for variable names, and the other will nag you that your start_server function doesn't have a docstring.