Tuesday, April 8, 2014

Being somewhat new to Python, I figured I'd give pylint a try. In Perl we have perlcritic which serves a very similar function (although in Perl files are parsed in a DOM-like way because as some of us painfully know, the only thing that can truly parse Perl is Perl).

Setting Up

First things first, let's setup a test virtualenv so we don't clobber anything. If you want to install globally or already have a project you'd like to lint, skip this step.

So out of the box I get a warning message saying I don't have a default configuration. Before I start looking at the output, I figure I should probably figure out where the defualt rcfile is stored (~/.pylintrc) and what format its in.

Generate Your ~/.pylintrc File

Lucky for us, the authors have included the handy --generate-rcfile switch that will generate one for you. The rcfile generation option also takes into account all switches you pass into pylint and will include those in your resulting file. The rcfile is printed to standard out which you can just redirect to your .pylintrc file in your home directory.

(test)jbisbee@beni:~/src/test$ pylint --generate-rcfile > ~/.pylintrc

Explore and Tweak To Your Liking

The resulting file is pretty lengthy, but I'd like to highlight some of the various options and sections that I took interest in. I took some of the descriptions directly from the documentation.
disable=
Disable the message, report, category or checker with the given id(s). If you or your team ends up choosing not to enforce certain errors or warnings that pylint produces, just add the error number (or in later versions, the human readable string) to disable the messages from showing up.

output-format=colorize
Set the output format. Available formats are text, parseable, colorized, msvs (visual studio) and html. You can also give a reporter class, mypackage.mymodule.MyReporterClass. Going to default to colorize.

reports=no
Tells whether to display a full report or only the messages. No reports by default for me.

max-line-length=120
Maximum number of characters on a single line. I'm going to set this from 80 to 120. Purists out there, all I have to say is, "Haters gonna hate." :)

generated-members=objects
List of members which are set dynamically and missed by pylint inference system. Django ORM's 'objects' is only value I have presently. Feel free to suggest other common generated members.

Similarities Section
Similar lines in %s files Indicates that a set of similar lines has been detected among multiple file. This usually means that the code should be refactored to avoid this duplication. I love the idea that pylint does this and I'd like to get some feedback for folks who encountered this error and how accurate it was.

Design Section
This sections is my favorite. It points out possible design flaws in your code based on a set of rules you can tweak. I consider my OO design to be pretty darn good, but as I was quickly porting a simple all in one command line program into classes, I got flagged with 'max-attributes' error. The error states if that you have more than 7 attributes you're most likely doing something wrong. (I had 8) It pointed out 5 of those attributes belonged in a separate class of their own and made my design cleaner (which never really hurts)

IDE/Editor Support

One of the coolest things about pylint is the IDE/editor integration that can give you real time feedback as you're coding. Visit the IDE integration page on the pylint website for more information. Going attempt to setup pylint.vim soon. :)

Would Love Your Feedback

Hey guys, I'm writing these blog posts as an exercise to teach myself and hopefully have others who are in my position, stumble upon my blog posts and actually do them some good. If you have some valuable input, point out pitfalls, or just to say thank you, I would greatly appreciate it.

For those of you who don't use iTerm2, I highly recommend it. One of my favorite features is enabling semi-opaque transparency and blur so I can still code and not be distracted the background artifacts. The settings can be found in Preferences > Profiles > Window

Saturday, April 5, 2014

So I'm happy to be blogging again and I though I'd start by sharing a hack I ran across a couple of months ago. It involves adjusting the OSX Key Repeat setting to a lower value that what the GUI allows. This setting is great for developers like me who sometimes navigate through code using arrow keys or like to quickly delete something by holding down the delete key.

System Preferences > Keyboard

Now the GUI presently allows you to set Key Repeat to a minimum value of 2, which for many folks, doesn't seem to be fast enough. We can change the value to 1, but before we do, take note of your current setting with the following command.

jbisbee@beni$ defaults read NSGlobalDomain KeyRepeat
30

Now to set the value to 1, we can use the following command. Unfortunately, you will need to reboot to have the change take affect.

jbisbee@beni$ defaults write NSGlobalDomain KeyRepeat -int 1

In the event you don't like the result, simply run the command again with your initial value.