Configuring PyDev in Eclipse to work with Django

The pre-requisite for working with Django is that it is installed in the Python interpreter you want to use.

Eclipse will add the Django options to the menu automatically. At this point, you can:

  1.  create a new Django project: Ctrl+N > PyDev Django Project
  2.  change an existing project to Django: right-click the project and choose
    PyDev -> Set as Django Project.
  3. (you can also change projects back in the same way).

In order to compile and run the web server, use the standard CTRL-F11.

  1. Don’t forget to add runserver to the Run Configuration
  2. Add –noreload as well so you can see the console output.

You can find a complete tutorial here.

Advertisements

Adding Numpydoc to Sphinx

This post is a continuation to the Sphinx Autodoc Tutorial for Dummies.

Sphinx has great documentation, but sometimes its documentation requirements can be somewhat awkward when looking at the code.

Here is a Sphinx function documentation example, which looks like this:

def public_fn_with_sphinxy_docstring(name, state=None):
    """This function does something.

    :param name: The name to use.
    :type name: str.
    :param state: Current state to be in.
    :type state: bool.
    :returns:  int -- the return code.
    :raises: AttributeError, KeyError

    """

Not that intuitive, IMHO.

Numpy is an excellent Python package for scientific computing, and they also offer their own Sphinx extension for documentation, numpydoc, that 1) looks good in html, and 2) looks clear in code.

Here is a Numpydoc function documentation example, which looks like this:

def foo(var1, var2, long_var_name='hi')
    """This function does something.

    Parameters
    ----------
    var1 : array_like
        This is a type.
    var2 : int
        This is another var.
    Long_variable_name : {'hi', 'ho'}, optional
        Choices in brackets, default first when optional.

    Returns
    -------
    describe : type
        Explanation
    """

I personally find the numpy version quite a bit more intuitive, and it looks great.

Installation

Super simple (easy_install reference).

easy_install numpydoc

And then add

extensions = ['numpydoc']

to your conf.py file. If you’re already using autodoc or other extensions, the line will look like this:

extensions = ['sphinx.ext.autodoc', 'numpydoc']

That’s it! Running

make html

should compile your documentation using numpydoc. If you don’t care for it – no problem – just remove the extension from conf.py.

The KVM Switch Hack: Force Resolution to Non-PnP Monitor in Windows

I recently installed a KVM switch so I can work conveniently with my PC and laptop. The laptop had no problems, but the PC was a different story: since my graphics card only supports DVI, I had to use a DVI/VGA converter for the KVM. This converter didn’t transfer the EDID monitor data to the computer properly; this meant that my computer couldn’t identify my monitor (displaying it only as “Generic Non-PnP Monitor”), and that it would not let me set my resolution to the proper screen values of 1680×1050. I spent quite a while hunting for an answer and finally found the perfect solution online: it worked seamlessly.

  1. Download Phoenix.zip and extract it to a working folder.
  2. Run it and click the “Extract Registry EDID” toolbar button.
  3. A list of monitors should appear.
  4. Try to find the monitor that matches the monitor that is giving you problems.
  5. Save the EDID to a .DAT file in your working folder.
  6. Download moninfo.exe and install it.
  7. Open the saved .DAT with MonInfo.
  8. Select “Create INF…” from the file menu and save it to your working folder.
  9. Open the Device Manager: click Start, type “devmgmt.msc”, and press Enter.
  10. Select the monitor you want to change (in my case, it was “Generic Non-PNP Monitor”).
  11. Right click and select Update Driver Software.
  12. Click “Browse my computer for driver software”.
  13. Click “Let me pick from a list of device drivers on my computer.
  14. Click Have Disk…
  15. Browse to the INF file in your working folder.
  16. Click next to override with your new INF.
  17. Restart your computer and enjoy your new resolution!

Selecting random rows with SQLite

A quick and easy way to select 100 rows randomly from a sqlite database:

SELECT * FROM UserRules 
ORDER BY Random() 
LIMIT 100

Copying lists in Python

In Python, assignment is in fact putting a name label (sticker) on an object. So if we want to copy a list, and we write the following code:

a = SomeList
b = a

a and b are actually two different labels for the object SomeList… and accordingly, changing the value of a will also change the value of b!

If we want to copy a list properly, we can use any of the following:

listB = listA[:]

or

listB = list(listA)

or

from copy import copy
listB = copy(listA)

If the list contains other lists as elements or other mutable objects, these objects are not copied but also shared so you might run into similar problems on these objects. In that case the following helps:

from copy import deepcopy
listB = deepcopy(listA)

Eclipse keeps throwing errors

Following a bad shutdown, Eclipse kept constantly throwing weird errors: it couldn’t save or open the workspace, had plugin issues, wouldn’t compile my project, etc.

Restart and reboot didn’t work, and I was at a loss until I tried this:

eclipse -clean

run from the eclipse directory. This cleared the plugins path, resolving the issue!