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?

 

Python and Ruby, siblings? 40

Posted by Peter Morris Tue, 29 Dec 2009 11:38:00 GMT

I have written code in many languages (Basic, dBase, C, Sculptor 4GL, Assembler(many flavours) Perl, VBA, Javascript, Ruby and finally, Python.

For much of my experience of Ruby, I viewed Python as a sibling language. I thought Ruby and Python where rough equals in capability and complexity.

It took me about 3 months of Python coding to realise that this is very far from the truth.

Ruby and Python are not compadres, they are not friends, they don't work at the same level.

Ruby is much simpler than Python, it has far less 'language' there. For a start, python is a compiled bytecode language (you do notice all those PYC files all over the place don't you?).

That and its explicit binding mechanism (all those import statements at the head of a python file just make my ruby sensibilities scream!) make for a much quicker language than Ruby.

When you add to that all this stuff about decorators, metaclasses etc, that takes some getting used to. The whole idea that methods associated with an instance are actually method instances that need calling (forgetting @property for the moment).

When I read about that, my mouth started watering, there must be some REALLY interesting uses for that! But then I let it stew for a while.

Really, all that does is force the coder to recognise that there are properties and then there are methods, and to require the user to know the difference.

With ruby, you don't need that. Well, really, with ruby, there are really ONLY ever methods, this forces encapsulation much more completely than Pythons default behaviour does.

Looking more deeply into decorators and metaclasses, I realise that they are trying to give the same sort of versatility to Python that Rubys blocks give to it.

Blocks affect EVERY aspect of Ruby, without blocks ruby is a rather dull, flat language. With them, it is a rich, expressive language that allows for some truely lovely DSL applications. Python tries to get the same functionality from metaclasses, decorators etc, but it just feels that much more forced. Having to use several different but interlocking constructs to ALMOST do what Rubys blocks do. This MIGHT be as a consequence of limitations enforced on the language by its bytecode underpinnings, I don't know enough yet to be able to say.

So, I suppose my final word on the issue is that Ruby is NOT a sibling of Python, its probably more of a teenager to Pythons twentysomething. There is a LOT more depth to Python than there is to Ruby, but thats not because the depth brings anything extra, it's more because Rubys simplicity is sufficient for the task. Python IS faster though, and probably always will be.

The later the binding, the slower the runtime performance.

All hail, Django models. 14

Posted by Peter Morris Tue, 29 Dec 2009 11:27:00 GMT

I have been spending a lot of time writing Django code (as you have probably noticed). In the past, I have been quite derisory about Django, there are many things that I don't like. Djangos ORM is NOT one of the problems. Well, fundamentally, it isn't. On the surface it stinks. So, lets get the nasty out of the way first. Some examples. In Rails, how do I find a particular instance if I know the ID? instance = Model.find(123) in Django instance = Model.objects.get(pk=123) Thats UGLY. This whole idea of managers, is a nice idea, but it is not entirely pretty. Now for the good thing. in Ruby if you do... object_list = Model.find(:all) you will get a big array filled with instances of all the objects in the database. Needless to say, this can SWALLOW your system. So, you have to be careful with AcriveRecord::Base, you get what you ask for, and if you ask for the moon, your system will fall to its knees and die. in Django if you do.... object_list = Model.objects.all() You will get an object that pretends to be an array, and only when you access chunks of it will it actually go over to the DB and pull back the required information. Thats NICE. BUT, it means you have to be a bit more careful, as it lulls you into a false sense of security. You have to remember that if you DO ask for the list of objects, you WILL get them. So, you have to remember. In rails, you know up front that you have to be careful, so its not quite as nice, but you aren't coddled. So, I do like Django models, it works in a more scalable fashion than Rails' ActiveRecord:Base. This leads me down the road of drawing comparisons between Ruby and Python. But thats the subject of a whole nother post.