Saturday, September 8, 2012

pylint and pep8

I've spend some time analyzing Django code. Maybe because I return to python with a huge experience in C#, but I find a Django code really messy. Classes, methods are big, many examples of violation of SOLID rules, and so on. I decided to test it against typical code analysis tools for python - pylint and pep8. Below are some results. By the way there is also django-lint - unfortunately not available via pip.
pylint db\models\query.py
...

Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |85     |NC       |NC         |
+-----------+-------+---------+-----------+
|refactor   |26     |NC       |NC         |
+-----------+-------+---------+-----------+
|warning    |98     |NC       |NC         |
+-----------+-------+---------+-----------+
|error      |22     |NC       |NC         |
+-----------+-------+---------+-----------+

...
Global evaluation
-----------------
Your code has been rated at 6.52/10
pep8 db\models\query.py
db\models\query.py:12:5: E128 continuation line under-indented for visual indent
db\models\query.py:28:1: E302 expected 2 blank lines, found 1
db\models\query.py:53:14: E231 missing whitespace after ','
db\models\query.py:54:29: E231 missing whitespace after ','
db\models\query.py:174:17: E127 continuation line over-indented for visual indent
db\models\query.py:269:80: E501 line too long (83 > 79 characters)
db\models\query.py:290:80: E501 line too long (81 > 79 characters)
db\models\query.py:315:50: E225 missing whitespace around operator
db\models\query.py:328:80: E501 line too long (88 > 79 characters)
db\models\query.py:336:17: E128 continuation line under-indented for visual indent
db\models\query.py:366:21: E128 continuation line under-indented for visual indent
db\models\query.py:367:80: E501 line too long (127 > 79 characters)
db\models\query.py:368:17: E128 continuation line under-indented for visual indent
db\models\query.py:408:80: E501 line too long (90 > 79 characters)
db\models\query.py:409:17: E125 continuation line does not distinguish itself from next logical line
db\models\query.py:410:80: E501 line too long (84 > 79 characters)
db\models\query.py:412:80: E501 line too long (87 > 79 characters)
db\models\query.py:414:80: E501 line too long (96 > 79 characters)
db\models\query.py:416:80: E501 line too long (144 > 79 characters)
db\models\query.py:434:17: E127 continuation line over-indented for visual indent
db\models\query.py:445:80: E501 line too long (83 > 79 characters)
db\models\query.py:467:80: E501 line too long (113 > 79 characters)
db\models\query.py:469:17: E127 continuation line over-indented for visual indent
db\models\query.py:482:17: E127 continuation line over-indented for visual indent
db\models\query.py:495:17: E127 continuation line over-indented for visual indent
db\models\query.py:523:17: E127 continuation line over-indented for visual indent
db\models\query.py:553:17: E127 continuation line over-indented for visual indent
db\models\query.py:567:80: E501 line too long (84 > 79 characters)
db\models\query.py:581:21: E128 continuation line under-indented for visual indent
db\models\query.py:583:80: E501 line too long (103 > 79 characters)
db\models\query.py:585:17: E128 continuation line under-indented for visual indent
db\models\query.py:593:17: E127 continuation line over-indented for visual indent
db\models\query.py:595:17: E127 continuation line over-indented for visual indent
db\models\query.py:597:17: E128 continuation line under-indented for visual indent
db\models\query.py:633:21: E127 continuation line over-indented for visual indent
db\models\query.py:680:80: E501 line too long (80 > 79 characters)
db\models\query.py:681:21: E128 continuation line under-indented for visual indent
db\models\query.py:685:80: E501 line too long (90 > 79 characters)
db\models\query.py:699:80: E501 line too long (80 > 79 characters)
db\models\query.py:724:80: E501 line too long (80 > 79 characters)
db\models\query.py:734:80: E501 line too long (81 > 79 characters)
db\models\query.py:735:21: E128 continuation line under-indented for visual indent
db\models\query.py:744:17: E128 continuation line under-indented for visual indent
db\models\query.py:753:17: E127 continuation line over-indented for visual indent
db\models\query.py:764:17: E127 continuation line over-indented for visual indent
db\models\query.py:775:17: E127 continuation line over-indented for visual indent
db\models\query.py:777:80: E501 line too long (85 > 79 characters)
db\models\query.py:903:80: E501 line too long (85 > 79 characters)
db\models\query.py:920:80: E501 line too long (80 > 79 characters)
db\models\query.py:1013:80: E501 line too long (93 > 79 characters)
db\models\query.py:1014:21: E128 continuation line under-indented for visual indent
db\models\query.py:1018:80: E501 line too long (85 > 79 characters)
db\models\query.py:1039:21: E128 continuation line under-indented for visual indent
db\models\query.py:1043:80: E501 line too long (80 > 79 characters)
db\models\query.py:1054:21: E128 continuation line under-indented for visual indent
db\models\query.py:1079:80: E501 line too long (102 > 79 characters)
db\models\query.py:1201:17: E127 continuation line over-indented for visual indent
db\models\query.py:1240:1: E302 expected 2 blank lines, found 1
db\models\query.py:1245:80: E501 line too long (80 > 79 characters)
db\models\query.py:1303:80: E501 line too long (83 > 79 characters)
db\models\query.py:1304:80: E501 line too long (89 > 79 characters)
db\models\query.py:1305:80: E501 line too long (82 > 79 characters)
db\models\query.py:1307:80: E501 line too long (83 > 79 characters)
db\models\query.py:1323:80: E501 line too long (93 > 79 characters)
db\models\query.py:1323:91: E225 missing whitespace around operator
db\models\query.py:1330:80: E501 line too long (103 > 79 characters)
db\models\query.py:1332:80: E501 line too long (96 > 79 characters)
db\models\query.py:1333:80: E501 line too long (97 > 79 characters)
db\models\query.py:1332:94: E225 missing whitespace around operator
db\models\query.py:1336:80: E501 line too long (82 > 79 characters)
db\models\query.py:1359:80: E501 line too long (88 > 79 characters)
db\models\query.py:1361:29: E203 whitespace before ':'
db\models\query.py:1418:80: E501 line too long (82 > 79 characters)
db\models\query.py:1420:80: E501 line too long (92 > 79 characters)
db\models\query.py:1425:80: E501 line too long (85 > 79 characters)
db\models\query.py:1426:80: E501 line too long (88 > 79 characters)
db\models\query.py:1439:9: E128 continuation line under-indented for visual indent
db\models\query.py:1439:9: E125 continuation line does not distinguish itself from next logical line
db\models\query.py:1443:80: E501 line too long (87 > 79 characters)
db\models\query.py:1449:80: E501 line too long (80 > 79 characters)
db\models\query.py:1485:80: E501 line too long (81 > 79 characters)
db\models\query.py:1489:80: E501 line too long (82 > 79 characters)
db\models\query.py:1527:80: E501 line too long (84 > 79 characters)
db\models\query.py:1530:17: E128 continuation line under-indented for visual indent
db\models\query.py:1531:17: E128 continuation line under-indented for visual indent
db\models\query.py:1532:17: E128 continuation line under-indented for visual indent
db\models\query.py:1589:15: E261 at least two spaces before inline comment
db\models\query.py:1596:25: E261 at least two spaces before inline comment
db\models\query.py:1599:22: E261 at least two spaces before inline comment
db\models\query.py:1600:33: E261 at least two spaces before inline comment
db\models\query.py:1639:80: E501 line too long (83 > 79 characters)
db\models\query.py:1640:80: E501 line too long (83 > 79 characters)
db\models\query.py:1642:80: E501 line too long (92 > 79 characters)
db\models\query.py:1645:80: E501 line too long (89 > 79 characters)
db\models\query.py:1647:80: E501 line too long (82 > 79 characters)
db\models\query.py:1653:80: E501 line too long (81 > 79 characters)
db\models\query.py:1654:80: E501 line too long (81 > 79 characters)
db\models\query.py:1659:63: E225 missing whitespace around operator
db\models\query.py:1663:80: E501 line too long (93 > 79 characters)
db\models\query.py:1722:80: E501 line too long (80 > 79 characters)
db\models\query.py:1745:80: E501 line too long (83 > 79 characters)
db\models\query.py:1757:80: E501 line too long (81 > 79 characters)
db\models\query.py:1788:80: E501 line too long (82 > 79 characters)
Not so bad, after all we are investigating a file that has almost 2000 lines (many comments). Analysis of different classes in average gives more or less same results. The bigger the class the worst results.

Tuesday, July 31, 2012

Python and a GIL

I run recently into a very nice article that describes in an excellent way of what a GIL is and what it is not. So if you, as me run in to many GIL battles in your life, it is a good read. One thing to add, traditionally threads were introduced to do a parallel IO not a parallel computation, keep that in mind.

Friday, July 27, 2012

UTF8 support under Windows in SublimeText2

SublimeText2 has a great problem with guessing encoding under Windows system. It tries to use CP1252 instead of CP1250 (for my locals:)) by default. I strongly suggest installing a package manager for SublimeText2. And then installing a encoding helper plugin. I also recommend (If you are a Central-European nation) changing a fallback_encoding property in: Preferences->Settings- Default to a Central European (Windows 1250).
A list of possible values:
st_encodings_list = [
   "UTF-8",
   "UTF-8 with BOM",
   "UTF-16 LE",
   "UTF-16 LE with BOM",
   "UTF-16 BE",
   "UTF-16 BE with BOM",
   "Western (Windows 1252)",
   "Western (ISO 8859-1)",
   "Western (ISO 8859-3)",
   "Western (ISO 8859-15)",
   "Western (Mac Roman)",
   "DOS (CP 437)",
   "Arabic (Windows 1256)",
   "Arabic (ISO 8859-6)",
   "Baltic (Windows 1257)",
   "Baltic (ISO 8859-4)",
   "Celtic (ISO 8859-14)",
   "Central European (Windows 1250)",
   "Central European (ISO 8859-2)",
   "Cyrillic (Windows 1251)",
   "Cyrillic (Windows 866)",
   "Cyrillic (ISO 8859-5)",
   "Cyrillic (KOI8-R)",
   "Cyrillic (KOI8-U)",
   "Estonian (ISO 8859-13)",
   "Greek (Windows 1253)",
   "Greek (ISO 8859-7)",
   "Hebrew (Windows 1255)",
   "Hebrew (ISO 8859-8)",
   "Nordic (ISO 8859-10)",
   "Romanian (ISO 8859-16)",
   "Turkish (Windows 1254)",
   "Turkish (ISO 8859-9)",
   "Vietnamese (Windows 1258)",
   "Hexadecimal"
]

Wednesday, July 25, 2012

A good approximation of pi

Ah, yes, a really nice article about a Pi approximation.

Monday, July 16, 2012

PowerShell sending requests

From time to time, I have to generate a request to some web service for each element from a database. From a db I tend to generate a PowerShell commands to send a request:
$page = (New-Object System.Net.WebClient).DownloadString("http://localhost/")
And if one needs to know what was returned
Write-Host "$page"

Friday, July 13, 2012

Using PowerShell

I still keep using Unix tools underneath my GNU Emacs on windows, tools, like grep, sort, find. But, they need a configuration, and on Windows, they face specific problems :) On SO, there is a nice list of PoSh cmdlets that are an excellent replacement for my typical tasks.

Tuesday, July 10, 2012

The next JavaScript issue

This site becomes my diary of JavaScript problems that I fall into, or am aware of. The newest one is the infinity. In other words what is a result of:
parseInt(1 / 0, 19)
And why it is 18.