Django/Python Epix Phail #5 - don't be evil. 1

Posted by Peter Morris Fri, 29 Jan 2010 13:15:00 GMT

Every language has its rough spots.


Ruby for instance does not handle array slices very nicely.

>> a = [1, 2, 3]
=> [1, 2, 3]
>> a[0]
=> 1
>> a[0..1]
=> [1, 2]
>> a[2, 3]
=> [3]
>> a[3, 4]
=> []
>> a[4, 5]
=> nil

 

Now, thats evil. Why should a[3, 4] give you an empty array (which is expected) but a[4, 5] give you nil!

But, Ruby gets some things right.....

>> a='1,2,3'
=> "1,2,3"
>> a.split(',')
=> ["1", "2", "3"]
>> a = '1'
=> "1"
>> a.split(',')
=> ["1"]
>> a = ''
=> ""
>> a.split(',')
=> []
>>
 
See that? an empty string gets you an empty array.

Theoretically, it SHOULD give you an array with a single empty text element, but it is nicer than that.

Not Evil.

Python on the other hand....

>>> a = '1,2,3'
>>> a.split(',')
['1', '2', '3']
>>> a = '1'
>>> a.split(',')
['1']
>>> a = ''
>>> a.split(',')
['']

Evil! pure and simple.

And lets look at slicing....

>>> a=(1,2,3,)
>>> a[0:1]
(1,)
>>> a[0:2]
(1, 2)
>>> a[0:3]
(1, 2, 3)
>>> a[0:4]
(1, 2, 3)
>>> a[1:4]
(2, 3)
>>> a[2:4]
(3,)
>>> a[3:4]
()
>>> a[4:5]
()
>>> a[6:7]
()
>>> a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
 

In some ways that is better, slicing works nicely, but indexing a single element raises an index error.

Surely, if it is happy to slice the array outside of its limits and return a valid result, why not handle

accessing an element outside of the limits too?

 

Out with the old, in with the new. 11

Posted by Peter Morris Sat, 02 Jan 2010 11:49:00 GMT

Every year I do a bit of a clear out of my bookshelves.


I have two shelves above my desk in my office where I hold my books, the lower shelf, is my 'working set' the books I reach for all the time. The upper shelf are the 'good to haves' but are books I don't need every day. Every year, I review the books in the upper shelf, and move a selection to the boxes in the loft, so that I can push books from my 'working set' shelf up and to free space on my working set for books I need as I move speciality.


So, into the loft this year goes...

  • PIC 16cXX Assembler data book
  • DC01 Television Tuners
  • Zilog Z8 microcontroller handbook
  • Microchip Data book
  • Keil 8051 Assembler/C51 manuals
  • Java in a nutshell
  • Programmers Guide to the Netbios
  • Palm Programming
  • iAPX88 Book

The assembler data books probably should have gone a couple of years ago, but
 I was a bit sentimental, I loved doing assembler coding.

From my working set to the 'upper shelf' (sort of like being demoted from the cabinet to the back benches I suppose

  • Lisp (Winston & Horn)
  • K&R The C Programming Language
  • Managing Projects with Make

And new in the working set is a section on Django and Python.

Django, epix phail #4 - The road to hell. 11

Posted by Peter Morris Fri, 01 Jan 2010 13:04:00 GMT

The road to hell is paved with good intentions, Djangos MTI gives you the tools to pave yourself right into the abyss.