Thursday, October 24, 2013

Enums come to Python, finally!

In the newly released python 3.4, although no new syntax has been added, the enum library module is a new addition amongst other changes. this library brings the enum type as a first class construct (PEP 435). Although it was discussed in 2005, but was rejected back then. However, this year various discussions occurred on this topic and it was decided to let the enums be a subclass of int and act as integer placeholders.
Since the syntax of the language has not changed, the enum is not a special keyword per se, rather you need to add an import line declaring the use of enums in your script
from enum import Enum
Thereafter, any enumeration 'class' can be created. Note that it is preferable to call this as enumeration rather than a class (and enumeration members instead of objects) for the obvious reasons.

class Avengers(Enum):
  captain_america =1
  iron_man = 2
  hulk = 5
  thor = 3
If you prefer to go functional, then this is the format:
Avengers = Enum('Avengers', 'captain_america, iron_man, hulk, thor')

Whatever the case, you are free to subsitute these placeholders in place of integers like other languages that support enums.

The iteration is performed in the order of enum member definition, not their values.
for avenger in Avengers:

results in captain_america, iron_man, hulk and thor being printed out.
At times, you will not have an idea of the enum members during runtime. For this, the values can be used which allow access to those members (which is quite different from enums in other languages as per my experience)
heavy = Avengers(5)

Members and Attributes

As the enumerations are python classes in its core, they also support use of various custom members:

class Avengers(Enum):
  captain_america =1
  iron_man = 2
  hulk = 5
  thor = 3
  def inform(self):
    return '{0} coming in {1} second'.format(, self.value)
  def tagline():
    return 'Avengers unite!'



Having enums with same names is invalid, but two enum members can have same values (as the second member will be an alias to the first). While iterating, the alias will be skipped.


The equality and identity comparisons are allowed:
>>> Avengers.hulk is not Avengers.thor
>>> Avengers.thor == Avengers.iron_man
The comparisons with values will evaluate to false. If you wish to do that, use IntEnum which can compare its members with integers and other IntEnum subclasses, but Not with enum.

Happy Enum-ing !

A true phonebloc - removing the evil software shackels

Recent efforts like phoneblocs have caught everyone's fancy (more than 1 million have signed up for them, including me). As the smartphones continue to improve at a breakneck pace, the marketing done by all phone companies(like the new feature their latest phone has is the greatest invention since sliced bread) leads to consumers buying phone (and tablet) devices even when there is not really a need to purchase them.
Such purchase and throwing off phones every year by enthusiasts is increasing the generated e-waste. It only benefits the mobile vendors, who can proudly proclaim their sales figures.
While phones can be broken and be easily re-assembled according to user needs, as explained in the phoneblocs video, there is another need to free the phone boot-loader/bios away from evil grips of software. Currently iOS or Android is created which only runs on a particular system on chip(SOC), or hardware. Projects like idroidproject exist, but are lacking driver support and community.
Hopefully with the advent of web based mobile os like mozilla, the dream of dual and multiboot mobile os can be made a reality - which could be a lifesaver for old phones with limited hardware constraints, but can support new UI altogether.
Ubuntu touch has demonstrated their OS on many android devices and is moving into the right direction while building a community. I am hopeful that other providers like google and microsoft take note and provide this interchangeability, which in turn will allow phoneblocs to be truly vendor neutral.

Wednesday, October 16, 2013

Learning just got cheaper - Packt Publication announces all products at half price

For the next 1 day (Till 17th October, 2013), you can purchase any book at half its listed price from Packt Publication. This is greater than offers present earlier as a flat 50% discount is being offered.
As this is being done to celebrate Columbus Day, the code COL50 is to be applied at checkout.

Due to my diverse reading habits, I always have a list of possible ebooks that I might purchase, and this presents a worthwhile opportunity.
Hope you avail this offer too!

(Update) Friday, October 18:  This promo has now extended till Monday, 21st!!
All the more reasons to smile :)