Posts

Showing posts from July, 2016

Playing around with errors in Python - NameErrors

Let's start with NameErrors, which is one of the more common errors that a newcomer to Python will come across. It is reported when Python can't find a local or global variable in the code. One reason this might pop up is because a variable is being referred to outside of it's namespace . To give you an example a = 10 def test_f (): a = 20 print a test_f() print a Let's walk through the code. After defining the variable a and the function test_f, you would naively expect the test_f() function call to change the value of a to 20 and print 20. You expect the print statement after the function call to also print 20. Because you expect the function call to have changed the value of a. But, if you try running the code for yourself, you'll notice that the final print statement will print 10. This is where namespaces come into the picture. Now, let's try this instead def test_f (): b = 20 print b test_f() print b The

Pocket reading list - Week 4.1 of July.

The Ukrainian Hacker Who Became the FBI’s Best Weapon—And Worst Nightmare - What I find most amazing about this article is what the hacker says about his fellow mates, that all they want is a job and if they found one that paid well and was stable, they wouldn't have much need to hack and make money the illegal way. This is, in my opinion, in general true for a sizable human population that defaults to stealing and cheating to make their livelihood, because they didn't have the option to work towards a legal/stable livelihood and now they're having to make ends meet one way or another. Canada’s $6.9 Billion Wildfire Is the Size of Delaware—and Still Out of Control - Just another reminder that Nature is a force out of our control. After settling down in every remote corner or the Earth, moving to the top of any and every food chain, we humans might feel invincible. But events like this remind us that nature around us is very fragile and can be disturbed beyond the poin

Playing around with exceptions in Python - Up is Down and True is False

True , False = False , True The above is a valid statement in Python2 (but not in Python3) and after executing the above statement, Python will return False if you evaluate 1==1 and True if you evaluate 1==2 . That's funny. It's hilarious. It pretty much made my day. And in the same vein as the previous post , you can use it to screw around with people. Take a look at the following piece of code. from utils import * if True : print 1 You expect it to print 1 because the if statement always evaluates to True . But. But. If you placed the earlier True/False switch statement in a file called utils.py , importing from that file will mean that the if statement always evaluates to False and nothing will be printed. Hide the True/False switch in an import statement and watch the world burn! Let me now give you a little insight into what is happening behind the scenes. My name is Rahul. Calling me something else doesn't change who I am or what

Playing around with errors in Python - KeyboardInterrupt

It's really hard to write code that doesn't throw errors, no matter what language one programs in. I've gotten used to writing modular code that helps maintain a constant train of thought and reduce the chance of errors occurring but still, from time to time, only after running the code do i go "Ohh, No!". I've been reading up on advanced concepts in python for about a week now and one thing I came across was the Python Standard Library reference of the Built-in Exceptions . I've come across most of these errors, a few more commonly than the rest. I thought that it'd be an interesting exercise to write code that intentionally raises the errors listed in that reference. To start with, let's look at something that's not exactly an Error but is an interesting thing to play around with, KeyboardInterrupt . If a code is producing error messages or if it's not responsive or if you just wish to halt it, most of us press CTRL+C , which correspon

Diamonds and why they're not as precious as they're made out to be

Before I say anything, Priceonomics people are awesome and some of the things that they write about are just mind-blowing! Coming to the point, Diamonds. While I was familiar with the fact that diamonds aren't actually all that precious or all that rare, I didn't know the whole story. After reading the following two articles, one more recent than the other - Have You Ever Tried to Sell a Diamond? and Diamonds Are Bullshit , I truly believe in the power of advertising. That's all there is to it. Advertising. In fact, after reading these two articles, I am convinced that Inception is in fact possible. It's a long con. A very very long con, spanning half-a-decade advertising campaigns, which made people believe that 1. A diamond ring is a must when a man asks a woman's hand for marriage. An addendum to this is the fact that a month's salary must be spent on said diamond ring. - The size of diamonds used in an engagement ring and the amount of money spent b

Pocket reading list : Week 1.2 of July

Sexy beasts  : I don't know why I hadn't asked the questions that were being answered in this article earlier. There are male birds out there with bright and extremely beautiful feathers, feathers that are primarily used to attract their female counterparts. And I should've asked the question "How does the bright and beautiful feathers help the female understand which male is stronger/fitter/better to reproduce with?", which this article answers. And there are things involved that came as a surprise to me. How Seattle Gave Up on Busing and Allowed Its Public Schools to Become Alarmingly Resegregated  : Apparently Seattle schools in the 70s and 80s enforced racial uniformity in schools in the area, by providing bus service to students if they needed to travel to a school a little further away, in the hopes of achieving racial uniformity. But now, the current administration and ignorant/biased/elite/pretentious/dumb parents are apparently resegregating the school

Pocket reading list : Week 1.1 of July

On spaghetti sauce - Malcolm Gladwell  : There's a lot to learn from how products are priced and the is a lot of science behind why the product lineup is what it is. Apparently, when companies approached a consultant to help them revive their product, conduct customer surveys and help defeating their rivals. What the consultant suggested was revolutionary, for the time. This is a story of what the consultant understood from his understanding of the human condition. The Arctic Suicides: It's Not The Dark That Kills You  : Urbanisation, in many parts of the world, is on full swing, and it's moving at an especially fast and frighteningly pace in some parts of the world! This account tells us what can go wrong if this urbanisation, and it's effect on the native population, isn't handled with care. It's surprising, shocking and sad that the deaths of so many natives isn't receiving a broader public attention. Why the S.E.C. Didn’t Hit Goldman Sachs Harder  :