My soon-to-be mother-in-law, somewhat concerned it seems about the trouble my fiancee’s little sister might find herself in on an upcoming trip to Crete, has prepared this hilarious but touching crib sheet, printed and laminated once for each girl on the trip.
Whilst exploring the area around Banff on a day’s break from snowboarding up at Lake Louise my mate Stu and I followed a freight train as it slowed into Banff station. The driver picked us up as his train hit walking pace and we talked to him about the experiences he’s had over 20 years of driving trains through some of the world’s most spectacular scenery.
Ever since I started coding in Java I’ve been regularly obliged to participate in conversations debating whether Java is a pass-by-value or pass-by-reference language. I’ve never been particularly hazy on the matter; Java is strictly a pass-by-value language and the values which are passed are pointers (to a memory location representing the data you are passing).
Lord knows there are enough articles online providing concrete examples of this fact. The one which I would recommend reading is here. Read the references too.For a day or so now I’ve been battling with Django’s model inheritance, which has a particularly frustrating approach to dynamic polymorphism. The problem is that when one is implementing a group of related models using multi-table inheritance and you attempt to iterate over the bog standard QuerySet for the parent class, we see an unfortunate phenomenon: attributes and methods specific to a subclass will not be available as the list we are returned is a list of instances of the parent class, not the various children.
For example, I have a basic CMS which models a Page as having a series of Nuggets. A Nugget could be a simple TextNugget, a TwitterNugget, a BlogNugget etc. Each nugget has a title but beyond that each subclass must implement a method content() which returns the HTML content (including templatetags, in case you’re interested) which will be rendered by the browser in the appropriate place. For ease of use I wish for my users to be able to select from available Nuggets without worrying about their type.
class Nugget(models.Model): title_text = models.CharField(_('Title'), max_length=200) show_title = models.BooleanField(_('Show')) text = models.TextField(_('Text'), blank=True) def __unicode__(self): return self.title def title(self): return self.title_text def content(self): return self.text # extends Nugget class TwitterNugget(Nugget): username = models.CharField(_('username'), max_length=30) def __unicode__(self): return self.title def content(self): return "{% insert_tweets user" + self.username + "%}" def title(self): return self.title_text
In order to access, for example, the content method of a
Nugget
and be sure that we’re going to get the twittertemplatetag
, rather than the text which the inherited method would provide, we need to be sure that the iterator provided by theQuerySet
is going to give us instances of the child (TwitterNugget
) class, rather than instances ofNugget
. In order to achieve this, we should override theManager
for theNugget
class to return a new kind ofQuerySet
– one with overriden__getitem__
and__iter__
methods which cast our models to the correct child classes. In turn this requires us to implement this new type ofQuerySet
(thePolymorphicQuerySet
)Determining which type of object should be returned in the iterator is done using a
content_type
field in the model – we have to override thesave
method on theNugget
to make sure that the content_type is written when we persist the model.content_type
is aForeignKey
to theContentType
model provided in the djangoContentTypes
package.from django.contrib.contenttypes.models import ContentType from django.db.models.query import QuerySet class PolymorphicQuerySet(QuerySet): def __getitem__(self, k): result = super(PolymorphicQuerySet, self).__getitem__(k) if isinstance(result, models.Model) : return result.as_child_class() else : return result def __iter__(self): for item in super(PolymorphicQuerySet, self).__iter__(): yield item.as_child_class() class NuggetManager(models.Manager): def get_query_set(self): return PolymorphicQuerySet(self.model) class Nugget(models.Model): title_text = models.CharField(_('Title'), max_length=200) show_title = models.BooleanField(_('Show')) text = models.TextField(_('Text'), blank=True) content_type = models.ForeignKey(ContentType,editable=False,null=True) objects = NuggetManager() def save(self, *args, **kwargs): if(not self.content_type): self.content_type = ContentType.objects.get_for_model(self.__class__) super(Nugget, self).save(*args, **kwargs) def as_child_class(self): content_type = self.content_type model = content_type.model_class() if (model == Nugget): return self return model.objects.get(id=self.id) def __unicode__(self): return self.title def title(self): return self.title_text def content(self): return self.text # extends Nugget class TwitterNugget(Nugget): username = models.CharField(_('username'), max_length=30) objects = NuggetManager() def __unicode__(self): return self.title def content(self): return "{% insert_tweets user" + self.username + "%}" def title(self): return self.title_text
It is with great sadness that I inform you all that my Asus eeePC 701 has finally exhaled it’s last 7-inch breath, wheezing under the weight of the many superfluous processes running on it’s aging LinuxMint installation. Please join me in thanking it for many years of service – it really did punch well above it’s weight.
Of course we shouldn’t dwell – enter the Asus eeePC R101. One year on from the theft of the Gateway netbook I purchased in New York, this pocketable(-ish) wonder is riding on my back everywhere I go, allowing me to progress my pet projects in any downtime I might find. It is no deluxe model, in fact it is as basic as they come, but it’s now dual booting Ubuntu Netbook (with Unity interface) and Windows 7. I’ve been developing a client’s site (django) on it under the Ubuntu installation and it’s a joy to use. I guess one benefit of spending years with a 7-inch screen and underpowered Celeron processor is that the bar is set low for subsequent purchases…
This document was written as my submission for the computer science writing course I took in my third year of study at the University of York. It attempts to summarise the privacy issues associated with the development of new technologies in the information communications domain. Please don’t read with too much of a critical eye – it is old now and it was unfortunately a last minute effort, which I may one day improve.
Introduction
In order for one to consider the broad topic of ethics with regard to computer engineering, it is important to be sure that we understand precisely what “ethics” are. In her lecture notes, Susan Stepney defines ethics to be “right and wrong behaviour in a society” [1, p25], a definition expanded upon in Computer Ethics: “the code or set of principles by which people live” [2, p14]. These broad definitions suggest each of us have a good idea what is right and what is wrong, but a question central to the topic of this review is “can we judge the use of the Internet with existing ethical frameworks?” [1, p9]. (more…)