Thanks to pylint the OCD is kicking in

So in April I took advantage of the great offer from Pluralsight where they were fully open and free to all during April. One of the courses that I took on here was Python Best Practices for Code Quality.

In there it mentioned pylint. This is a great little utility you can run against your code to check your code is PEP8 compliant. PEP8 is a guideline, best practices for Python coding. I learnt Python years ago, I would say I was ok, I get by and I have always managed to scratch my own itches. However I have always sort of kept my code the same, just because that’s how I wrote. Now from this course I installed pylint and ran this over a script. Was shocked at all the things it recommended, but then from there is was a challenge and a game. I say this because when you make some changes your score goes up and the amount of errors reduces so then you want to get this down to the least number of lines and the highest score.

I had seen a few scripts being rejected when being added to my GitHub Repo which is another reason I wanted to look into this.

An example output of this is below,

Craig’s MacBook Air:python craigdba$ pylint hkc_senior_text.py 
No config file found, using default configuration
************* Module hkc_senior_text
C: 37, 0: Line too long (224/100) (line-too-long)
C: 46, 0: Line too long (135/100) (line-too-long)
W: 42, 4: Redefining built-in 'hash' (redefined-builtin)
C:  1, 0: Missing module docstring (missing-docstring)
C: 17, 0: Constant name "dropbox" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 18, 0: Constant name "scripts" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 19, 0: Constant name "dbfile" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 20, 0: Constant name "master_db" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 22, 0: Constant name "f" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 26, 0: Constant name "conn" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 27, 0: Constant name "cursor" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 28, 0: Constant name "loc_stmt" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 31, 4: Constant name "row" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 32, 7: Comparison to None should be 'expr is None' (singleton-comparison)
C: 37, 4: Constant name "message" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 39, 4: Constant name "username" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 40, 4: Constant name "sender" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 42, 4: Constant name "hash" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 44, 4: Constant name "numbers" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 47, 4: Constant name "test_flag" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 53, 4: Constant name "values" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 60, 4: Constant name "url" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 62, 4: Constant name "postdata" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 63, 4: Constant name "req" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 69, 8: Constant name "response" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 70, 8: Constant name "response_url" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 73,29: Variable name "e" doesn't conform to snake_case naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 3.41/10 (previous run: 2.68/10, +0.73)

So then you can make some changes are you can see your improvements.

No config file found, using default configuration
************* Module hkc_senior_text
C: 37, 0: Line too long (224/100) (line-too-long)
C: 46, 0: Line too long (135/100) (line-too-long)
W: 42, 4: Redefining built-in 'hash' (redefined-builtin)
C:  1, 0: Missing module docstring (missing-docstring)
C: 22, 0: Constant name "f" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 32, 7: Comparison to None should be 'expr is None' (singleton-comparison)
C: 42, 4: Constant name "hash" doesn't conform to UPPER_CASE naming style (invalid-name)
C: 53, 4: Constant name "values" doesn't conform to UPPER_CASE naming style (invalid-name)
E: 58,31: Undefined variable 'numbers' (undefined-variable)
E: 69,35: Undefined variable 'req' (undefined-variable)
C: 73,29: Variable name "e" doesn't conform to snake_case naming style (invalid-name)

------------------------------------------------------------------
Your code has been rated at 5.37/10 (previous run: 0.98/10, +4.39)

So now it has bugged me and I am in the process of changing all my own personal scripts to ensure that they are all PEP8 compliant. I have quite a few scripts but thankfully thanks to some command-line fu, and great tools like sed and some skills in vi this should be done in a few hours.

As explained, I learnt Python a good few years ago using the book below, and I do still recommend this book, its a great starting point. I will be putting up tips page very shortly explaining some more around Pylint. Please get in touch if you have any questions or want some more information, add a comment or send me an email.

List Python script to convert to PEP8

Huge outage for Virgin Media