October 2009 Archives

Stupefying Django error

| | No TrackBacks

Wow. I spent a lot of time on this this afternoon. I had a Django application with an admin interface. Every time I tried to create an object with no foreign keys, I got this error on the save:

Traceback (most recent call last):

  File "C:\Python26\lib\site-packages\django\core\servers\basehttp.py", line 279, in run
    self.result = application(self.environ, self.start_response)

  File "C:\Python26\lib\site-packages\django\core\servers\basehttp.py", line 651, in __call__
    return self.application(environ, start_response)

  File "C:\Python26\lib\site-packages\django\core\handlers\wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "C:\Python26\lib\site-packages\django\core\handlers\base.py", line 134, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "C:\Python26\lib\site-packages\django\core\handlers\base.py", line 154, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 40, in technical_500_response
    html = reporter.get_traceback_html()

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 86, in get_traceback_html
    frames = self.get_traceback_frames()

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 205, in get_traceback_frames
    pre_context_lineno, pre_context, context_line, post_context = self._get_lines_from_file(filename, lineno, 7, loader, module_name)

  File "C:\Python26\lib\site-packages\django\views\debug.py", line 186, in _get_lines_from_file
    context_line = source[lineno].strip('\n')

IndexError: list index out of range

Totally mysterious: there is not a line of my code in the trace stack and I have no idea what file it is trying to index into. I really made no progress on this until I tried the shell:

>>> from django_league.league.models import *
>>> Sport.objects.create(sport_name='Hockey')
__unicode__
Traceback (most recent call last):
  File "lgt;console>", line 1, in lgt;module>
  File "C:\Python26\lib\site-packages\django\db\models\base.py", line 328, in __
repr__
    u = unicode(self)
  File "C:\Users\hughdbrown\Documents\django\django_league\..\django_league\leag
ue\models.py", line 17, in __unicode__
    return u"%s" % (sport_name, )
NameError: global name 'sport_name' is not defined

Now we're getting somewhere, I thought. I have a function of mine in scope. The problem was that I had defined my __unicode__ method without using self in the code:

class Sport(models.Model):    
    sport_name = models.CharField(max_length=20)
    def __unicode__(self):
        return u"%s" % (self.sport_name, )

    @models.permalink
    def get_absolute_url(self):
        return ('sport', None, {'object_id' : self.id})

    class Meta:
        ordering = ['sport_name']

So fixing it was pretty easy once I knew that.

DataVision sucks

| | No TrackBacks

So the Windows 7 upgrade that I reported yesterday gets better. DataVision, the store in New York I bought the upgrade from, is totally unwilling to provide a refund or to take an exchange on the product because I opened the package. I don't know of any way to determine that an operating system will not install on your machine without actually, you know, running the installation. Their recommendation? Take it up with Microsoft. Microsoft, understandably, says, "Waaaah? We didn't sell you this. Talk to DataVision."

The stance of the manager, Joey (212 689 1111 x1746), is three-fold:

  • You always have to reinstall your programs when you upgrade the operating system
    Patently untrue. Not true with any previous Microsoft OS I've used and not true for any Linux OS I've tried. Some people will say anything not to have to refund money.
  • We don't refund money
  • We don't take back opened software
    I have no idea how I would have known the installation would fail (or require reinstalling all programs) without opening the package.

So I have a suggestion of my own: don't do business with DataVision.




Update 2009-10-23: Maybe I should have known better. If you look for ratings of DataVision on the web, it's pretty consistently rated one of the worst places to get computer service in New York City.

"They truly have the worst customer service in the world" ... "Located in midtown Manhattan, this computer store leaves much to be desired" ... "ABSOLUTELY HORRIBLE CUSTOMER SERVICE!" ... "The prices are pretty good" ... "Awful. I wouldnt shop there again" ... "Very helpful"



Update 2009-11-05: Wahoo! I am number one on google for the phrase 'DataVision sucks'! Now there is some talented branding!

Introducing Windows 7. Your PC, simplified.

| | No TrackBacks

I tried out the Windows 7 Beta earlier this year. I had that sinking feeling in my gut when I realized that I would have to reinstall all my programs in order to move from Windows XP sp2 to WIndows 7 Beta. Apparently, the directory structures were not the same so the programs would not run. Or something. I sucked it up and installed a raft of development tools so that I could have the new OS. At least I was only going to have to do this once.

So when I ran the upgrade installation program for Windows 7 Professional, I was surprised to get this message:

Windows 7 Ultimate cannot be upgraded to Windows 7 Professional. You can choose to install a new copy of Windows 7 Professional instead, but this is different from an upgrade, and does not keep your files, settings, and programs. You'll need to reinstall any programs using the original installation discs or files. To save your files before installing Windows, back them up to an external location such as a CD, DVD, or external hard drive. To install a new copy of Windows 7 Professional, click the Back button in the upper left-hand corner, and select "Custom (advanced)".[EMphasis added]

So what do you think: should moving from Windows 7 Ultimate (beta) to Windows 7 Professional (release) require reinstallation of every non-operating system program on my machine? Would you go ahead or just return the upgrade package and wait until Microsoft gets its act together?

MoreLinq

| | No TrackBacks

Recently, I wanted a way to iterate over two sequences in LINQ the same way you do in functional languages like python. It's most commonly called zip() and it works like this:

seq = [fn(a1, b1) for a1, b1 in zip(a, b)]

This applies a function fn() to each pair of variables pulled from a and b together. There is no direct way to do this in LINQ 3.5, but I came across MoreLinq which has an implementation of zip() and many other useful functions.

VB.NET code to implement Excel Rank()

| | No TrackBacks

Recently, I had to implement the Excel function Rank() for a project. I came up with this templatized LINQ code:

Public Class ReversedComparer(Of T As IComparable)
    Implements IComparer(Of T)
    Public Function Compare(ByVal x As T, ByVal y As T) As Integer _
    Implements System.Collections.Generic.IComparer(Of T).Compare
       Return -x.CompareTo(y)
    End Function
End Class

Private Shared Function BinarySearch(Of T As IComparable)(ByRef x() As T, _
                                     ByVal val As T, _
                                     ByVal comparer As IComparer(Of T)) As Integer
    Dim lo As Integer = 0, hi As Integer = x.Count - 1
    While lo <= hi
        Dim mid As Integer = (hi + lo) \ 2
        If comparer.Compare(val, x(mid)) <> 1 Then
            hi = mid - 1
        Else
            lo = mid + 1
        End If
    End While
    Return CInt(If(comparer.Compare(x(lo), val) = 0, lo, -1))
End Function

Public Shared Function Rank(ByRef X() As Double) As Integer()
    Dim sorted() As Double = (From xx In X Order By xx Descending Select xx).ToArray()
     Dim comparer As IComparer(Of Double) = New ReversedComparer(Of Double)
     Return X.Select(Function(val) 1 + BinarySearch(Of Double)(sorted, val, comparer)).ToArray()
End Function

And it's pretty good as far as it goes. It takes a copy of the data in reverse-sorted order and then does a binary search to find the 0-based position in the array for each element in the original array. And that's fine, but I wanted to implement something more like my python code:

import collections
def rank(arr):
	"""
	>>> a = list(range(10))
	>>> print rank(a)
	[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
	>>> b = list(range(10))
	>>> b.reverse()
	>>> print rank(b)
	[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	>>> c = [5] * 10
	>>> print rank(c)
	[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
	>>> d = ([5] * 5) + ([1] * 5)
	>>> d
	[5, 5, 5, 5, 5, 1, 1, 1, 1, 1]
	>>> print rank(d)
	[1, 1, 1, 1, 1, 6, 6, 6, 6, 6]
	"""
	d = collections.defaultdict(list)
	for i, v in enumerate(arr):
		d[v].append(i)
	result = [0] * len(arr)
	i = 1
	for k in sorted(d, reverse = True):
		for j in d[k]:
			result[j] = i
		i += len(d[k])
	return result

if __name__ == '__main__':
	import doctest
	doctest.testmod()
 

So I worked through the LINQ issues using the LinqPad interpreter and came up with this:

Function Rank(Of T)(Byref x() as T) as Integer()
    Dim original_pos = x.Select(Function(xx, index) New With {.Val = xx, .Index = index}) _
             .ToLookup(Function(xxx) xxx.Val)
    Dim keys = original_pos.OrderByDescending(Function(yy) yy.Key)    
    Dim result(x.Count-1) as Integer
    Dim i as Integer = 1
    For Each item in keys
        For Each v in item
            result(v.Index) = i
        Next
        i = i + item.Count
    Next
    Return result
End Function

And I think that's pretty good code: templatized LINQ code that uses lambda functions and implements the algorithm as quickly as I know how.

Pages

OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Archive

This page is an archive of entries from October 2009 listed from newest to oldest.

August 2009 is the previous archive.

November 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.