Showing posts with label virtualenvwrapper. Show all posts
Showing posts with label virtualenvwrapper. Show all posts

Tuesday, April 8, 2014

Getting Started with pylint


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.

jbisbee@beni:~$ mkvirtualenv test
New python executable in test/bin/python
Installing
Setuptools.............................................................................................done.
Installing
Pip....................................................................................................................................done.

Now pip install pylint

(test)jbisbee@beni:~$ pip install pylint
Downloading/unpacking pylint
...
Successfully installed pylint logilab-common astroid
Cleaning up...

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.

jbisbee@beni$ pylint sample.py
No config file found, using default configuration
...

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.

Tuesday, July 23, 2013

Introducing virtualenvwrapper-django

I recently took one of my previous posts entitled "Never Have to Type "python manage.py" with Django Again", and combined it with another bash function to auto set DJANGO_MODULE_SETTINGS by interrogating a Django project's directory. I then rolled everything together into github as


Its a little rough I admit, and I welcome any feedback for improvements.  As a result, I'm now able to run commands like the following anywhere once I have my virtual environment loaded.

(spock)jbisbee@tacquito:~/src/spock$ manage dbshell
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 95
Server version: 5.5.31-0ubuntu0.12.04.2 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Edit: I've gotten some feedback and I realize I didn't sell the real reason behind dynamically setting the DJANGO_SETTINGS_MODULE variable. I use Django at work and we have moved from the generic one file settings.py to a settings directory which looks something like this

  settings/
    base.py
    dev.py           # imports from base.py
    test.py          # imports from base.py
    production.py    # imports from base.py
    jbisbee.py       # imports from dev.py

This way you can have cascading Django settings with overrides at multiple levels. This bash function I wrote finds the settings directory and will set DJANGO_SETTINGS_MODULE to the user's specific dev settings (or dev if the user can't be found)

DJANGO_SETTINGS_MODULE=projectname.settings.jbisbee

This way in jbisbee.py I can override my database, turn on crazy debugging, or do anything else I want and be able to commit those settings without affecting others.

Disclaimer: If you manage your Django project's settings differently let me know. I'd love to make the determine_django_module_settings more flexible and patches are more than welcome!

Wednesday, July 3, 2013

Never Have to Type "python manage.py" with Django Again

I got very quickly got tired making sure I was in the right directory and typing python manage.py. I found django-shortcuts but didn't like the fact that it only mapped a handful of the commands and I wanted access to everything manage.py could do.

I then briefly considered creating a bash function that would determine manage.py's location on every invocation but figured that was silly and decided I might as leverage virtualenv's virtual environment and as Ron Popiel says, "You set it and forget it!"

It only took a minute of poking to find the postactivate hook file within my ~/.virtualenvs directory. Below is the fruit of my labor.

#!/bin/bash
# ~/.virtualenvs/postactive
# This hook is run after every virtualenv is activated.
VIRTUAL_ENV_NAME=$(basename $VIRTUAL_ENV)
SRC_DIR="$HOME/src"
if [ -d "$SRC_DIR/$VIRTUAL_ENV_NAME" ]
then
    MANAGE_PY=$(find "$SRC_DIR/$VIRTUAL_ENV_NAME" -name "manage.py" -type f)
    if [ -e "$MANAGE_PY" ]
    then
        alias django="python $MANAGE_PY"
    else
        unalias django
    fi
else
    unalias django
fi

The end result is that I have a new django alias that will work anywhere and act as though I typed python manage.py

jbisbee@beni:~$ workon django-tutorial
(django-tutorial)jbisbee@beni:~$ alias | grep django
alias django='python /Users/jbisbee/src/django-tutorial/mysite/manage.py'
(django-tutorial)jbisbee@beni:~$

Disclaimer: I'm making a big assumption here that the virtualenv name you're using is identical to the name of the project you're working on. It's a pretty big assumption so I apologize if it does't work right out of the box for you.

Tuesday, July 2, 2013

Proper Way to Start Programming Python

So after quite a few missteps I've fallen into the correct pattern for programming python. There are two main tools for installing python modules: pip and setuptools' easy_install script. On a virgin system you'll want to the following packages from the OS distribution. I've repeated this process on my macbook air, windows machine under cgywin, and my Ubuntu virtual machine.

1. Install python and python-setuptools

You'll need to install python and python-setuptools before you begin. Macs come with these packages pre-installed so you can skip this step if you'd like.

python
python-setuptools

2. easy_install virtualenvwrapper

Then you'll want to install virtualenvwrapper via setuptool's easy_install. It should be the only package you should ever install with easy_install.

jbisbee@benny:~$ easy_install virtualenvwrapper
Searching for virtualenvwrapper
Reading http://pypi.python.org/simple/virtualenvwrapper/
Best match: virtualenvwrapper 4.0
...
Finished processing dependencies for virtualenvwrapper

3. Find locaiton of virtualenvwrapper.sh

Once you have virtualenvwrapper installed you'll want to see where it was installed in your path. Please note that it is a shell script and does end with the .sh file extension.

jbisbee@benny:~$ which virtualenvwrapper.sh
/usr/bin/virtualenvwrapper.sh

4. Add virtualenvwrapper.sh to Your Shell Startup

# your ~/.bashrc or ~/.profile file
export WORKON_HOME=~/.virtualenvs
# virtualenvwrapper.sh may be installed in 
#     /usr/local/bin on your system
source /usr/bin/virtualenvwrapper.sh  

5. Reload Your Shell

jbisbee@benny:~$ source ~/.bashrc
# source .profile

6. Create a New virtualenv Environment

Now you're in business to start creating virtual workspaces and installing libraries within them with pip. Using django as an example :)

jbisbee@benny:~$ mkvirtualenv tutorial
New python executable in tutorial/bin/python2.7
Also creating executable in tutorial/bin/python
Installing setuptools............done.
Installing pip...............done.
(tutorial)jbisbee@benny:~$

7. Install Python Modules with pip

Now install python modules with pip within your virtualenv sandbox:

(tutorial)jbisbee@benny:~$ pip install django
Downloading/unpacking django
  Downloading Django-1.5.1.tar.gz (8.0MB): 8.0MB downloaded
  Running setup.py egg_info for package django
...
Successfully installed django
Cleaning up...
(tutorial)jbisbee@benny:~$

Have fun and let me know if you run into any issues as I wrote this post pretty quickly and may have left something out.

Edit: This blog post has been translated to Spanish by +Apokalyptica Painkiller. Thank you!