Flight of the Conchords : The humans are dead

Tags

, , , , ,

Advertisements

Python’s equivalents to the Ternary operator cond?then:else

Tags

, , , ,

Recently a friend of mine asked me if there was a ternary operator in Python and it got me thinking.

Languagues such as C even have a syntax like [[ condition ? ifblock : thenblock ]]

The first thought that came to mind was using Dictionaries and lambda functions. A dictionary can also use the Boolean type for true so:-

{‘True’: lambda x : ifblockfunction(), ‘False’: lambda x : elseblockfunction()}[condition]()

So basically its a dictionary lookup agaist the condition that returns the apprpriate lambda function. Since lambda’s are functions, the () at the end is that of a function call.

Okay you guys still with me, cos I have more to tell you.

I learned python over a year ago back in the days of 2.4 and a notation that I hadnt come across that exists from 2.5 onwards is the [[ ifblock if cond else elseblock ]].

>>>5 if False else 7

Should return 7.

Pretty nifty I’d say, resembles what its parse tree would look like.

If you’re using python 2.4 or below, you must be quite disappointed by now but theres more, however you’ll have to bear with me on this one.

In python, datatypes inside an if statement evaluate to True or False. So if(3) should give True[Note not actual language syntax]. Similarly if([1,3,4]), if ((1,3,4)), etc all return True.

So what returns false. None, False, empty lists, empty tuples, etc return false

This is a functionality that Python could have inherited LISP since in LISP everystatement evaluates to something.

1234 and True

The above line should evaluate to True which it does. But consider this

>>> True and 1234
1234
>>> 1234 and True
True

True and 1234 evaluates to 1234, so its returning the second True Element and it doesnt make a differece because essentially both True and 1234 are True

So how can we use this to our liking. Consider the following.

“T” and condition or “F”

This would return “T” for True and “F” for false by shortcircuit evaluation.

However func1() and True or func2() wont work.

Function calls return None to the language and func1() would be executed anyways during the inorder execution so it would be of no use to us.

Therefore in order to make this work we can once again resort to lambda functions as follows:-

>>>(lambda : mojo()) and False or (lambda : len(“sdf”))()
3

This is the geekiest solution by far and it has the same inorder tree placement as its syntax in 2.5

P.S. I love language related information. If anyone found this useful and has anything to share, gems left in the comments section for me to munch on shall highly be appreciated.

Sicko, A must-watch if you’re from, in or interested in the US

Tags

, , ,

I recently watched Sicko, a documentary directed by Michael Moore about the American health care system. The problem with the US is that since its such a highly service oriented industry, they dont try to give him a maximum return on his money if there is no competition, rather the parts of your body are divided by function. An example that which deeply effected me from the movie was that of a carpenter who lost both his index and his ring fingers in a carpentry accident. The cost of reattaching the fingers[for the carpenter] varied with the functionality of the fingers rather than the price it cost the hospital to reattach them. Could there be a system more evil than one who’s only purpose is to maximize profits. In a country such as the US, health care should be the responsibility of the government and should not be provided by private companies.

The film is full of heart warming anecdotes, facts & comparisons that were the result of a through study of the american health care system. I wouldnt want to leak too many details, but this is a must watch.

I also watched the Corporation which is also an interesting film. According to some experts FOSS costs the software industry 70 billion US dollars ever year in missed profits and sales oppurtunity which means that eventually large companies would have to change the way they operate. And these developments are good for the little guys out there because a free trade system with a lot of small companies and individuals offering services is a much better option in my opinion.

Good Bye Vista

Tags

, , , ,

You sha’nt be missed. If you guys were wondering what Vista was doing on my system in the first place, I shall confess to err is human. But I have learnt from my mistakes and I shall follow the right path now.

I shall start this post by listing issues I faced with Windows Vista:-

  1. Blue Screens of Death. Lots of em. Reminded me of the days of Win 95.
  2. Weird Graphical Glitches. Sometimes my screen looks like two screens shifted by a couple of pixels and overlapping each other.
  3. Games running too slow. Well I’ll give you the example of HellGate london. I can run it on full specs 1920×1200 on my new XP installation, however on vista I had to run it on 1080×1024 on my laptop [[2GB Ram Nvidia 7950 GTX Core2Duo Santa Rosa Architecture]]
  4. Too much clicking. In attempts to make everything look better, they’ve included in a lot of clicks. In XP you could right click Network Icon -> Status -> Properties and change your network config. In Vista however you have you open Network Center.
  5. Ctrl+Alt+Delete < This is quite significant. Rather than bringing up the Task Manager directly in vista you have to see a fancy splash screen first.
  6. Games not running. Yep its true. There are major incompatibility issues to be faced here.

And the best one which actually caused me to get rid of vista. They burn some keys into the MBR so that it isnt visible if someone installs another MBR. It has been known to wreak havoc on dual booter systems. I discovered this while installing FreeBSD on my system. Thank you for your Guidance Richard Stallman.

In case you have been hit by this problem and are seeking remedial, you can move over to http://www.clearchain.com/wiki/FreeBSD_&_Windows_Vista  for a solution.  If microsoft paired CD keys for Vista with XP, I’m sure there’d still be 75% peopleof this population still using XP.