Skip to main content

Study, Day 6

Did a little bit of french and geography.

MITx, 6.00.1x, Introduction to Computer Science and Programming Using Python

Doing compsci but no notes today, because i am busy learning.
Am going to have to revisit oop sometime again.
will make notes then

But to summarise,

  • i learnt oop
  • i learnt about generators,
    • which to me sounds like a tap on a loop.
    • instead of a bucket of water, i get it by the mug.
    • here’s a mug. here’s another mug. here’s another mug.
    • enough? then i can turn the spigot off
    • no need to fill the whole bucket (run through a whole dictionary, or carry out a huge operation) when I can just take however much I need.
  • i learnt about o notation to figure out how long code takes to run, if want to later optimise stuff
  • sorting lists with sorting algorithms
    • bubble sort
    • selection sort
    • merge sort efficient
  • i learnt how to plot using python

Have finished all the videos and will now move on to the problem sets.
I had so much fun, I actually was sad to watch Prof Grimson’s last video.
The course has given me a really good grounding on the basics.
And should help me start my journey.
While I understood the what, MIT 6.00.1x gave me a good grounding on the how and the why.

Study, Day 5

Did some geography for 12th in the morn.
Slow, steady, iterative progress there.


MITx, 6.00.1x, Introduction to Computer Science and Programming Using Python

exceptions & assertions

Exceptions

  • What happens when program execution comes across something that it did not expect?
    • like you could go beyond the bounds of a list
    • i try to reference a variable that isn’t there
    • i mix data types. (i dun dis :P 'a'/9)
  • This is basically Python telling me, ‘I don’t get this! Whatcha talkin’ bout Willis?’
  • Python has a whole host of errors, the names of kinda give me clues to what it thinks the problem is. There are
    • SyntaxError (I boo booed typing somewhere, Python does not ‘get’ me)
    • NameError (Can’t find a variable. Did I put it in? Did I call a module correctly?)
    • AttributeError (Errors with classes. Am I calling them wrong?)
    • TypeError (Am I doing string operations on numbers? Or vice versa?)
    • ValueError (Something is wrong with my results. Is it in a form the program understands?)
    • IOError (something that happens when i make boo boos with files)
What do I do with exceptions?
  • Fail silently (like fart silently and then look about innocently)
    • bad idea, though, really bad idea :)
  • Return a “error” value
    • make sure that you have code that can check for and handle the values you choose
    • makes code a little clunkier
  • Just stop execution, and signal an error condition
    • in Python that’s called raising an exception like so,
      raise Exception("Good God! You’re dividing letters with numbers?!")
Dealing with Exceptions. How do I handle them?
  • Use the try & except keywords.
  • As in you’re telling Python, ‘try’ this code. If everything works well, great!
  • But if there’s an error, jump to the ‘except’ clause, and execute what is in there
  • And then carry on with the rest of the program
  • I can then get fancy. I can have multiple except clauses.
    • one to handle typos
    • one to see if my input is valid
    • one to handle division isssue
    • and a final one as a catch all, if all else fails.
  • There is also an else that i can pair with a try
    • the else will execute code if try block it is associated works, with out any exceptions.
    • like ok, try this. all worked? then go that else block and do that thing and then carry on with the rest of the program
    • to my mind it kind of is the main program, because this is the part that always executes.
    • so as I iterate, i keep removing things from try and move them down to the mandatory else clause
  • And then there is finally
    • executed after try, except and else
    • even if they raise errors or cause a break
    • useful for cleaning up after a mess :P (close a file for e.g.)
    • like ok, here’s a booboo, but I better put everything back in it's place and undo stuff if I can
    • like do no harm.
Exceptions as control flow
  • turn the idea of exceptions on its head
  • instead of waiting for errors and catching them and then raising exceptions
  • we could check ourselves for expected values and raise our own exceptions if results don’t match expectations :)
    • like if i am unable to produce a result that is consistent with the specifications of a function
  • to do that we use raise
  • we raise exceptions
    • like raise iAmDisappointedinYouError("You should have written this better")
    • or more likely you want to raise an existing known error like raise TypeError("You need to enter a number! For the love of God, enter a number!")

Assertions

We can take the idea of proactive exception raising, to another level.
We can defensively program so that we prevent situations that lead to unexpected results.
This idea is what we call an Assertion

  • Remember those docstrings we typed where we expected a certain of inputs and guaranteed outputs?
  • you can think of assertions as the bouncers who enforce them
    • i assert that this should be true. if not, raise an exception
  • you can use the assert statement that in turn will raise an AssertionError if conditions aren’t met
    • could be on inputs (typically, mostly used here)
    • on an intermediate stage of the program
    • if i want to be really careful, i can use it to check output too
  • having these checks makes it a lot easier to locate the source of a bug
    • because if an assert fails, the bug most probably should be just before we called this function
  • Where should you use assertions?
    • so spot bugs as soon as they are introduced and clarify where they happen
    • as a supplement to testing
    • filter and check input
      • check types of arguments or values
      • check that invariants on data structures are met
      • constraints on return values are met
      • violation of constraints on procedure (no duplicates in a list, for e.g.)

epictetus-books

“Don’t just say you have read books.
Show that through them you have learned to think better, to be a more discriminating and reflective person.
Books are the training weights of the mind.
They are very helpful, but it would be a bad mistake to suppose that one has made progress simply by having internalized their contents.”

— Epictetus, The Art of Living

NGINX Redirect .html to just slugs

I like Nikola’s pretty URLs feature and have been using it at the personal site.

And after a year of prettiness there and .html here, I decided to switch this site to use it too.
All my urls look nice now :)
Which also means all the old links are now broken.
And so, the last bit of work was to redirect all the links in the wild and on the blog to the new addresses

A quick web search led me to this stackoverflow answer.

#at the top of location /
if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }

#within \.php$
if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 302 /$1?$args;  }

All I needed was the top bit and all was well, with my world :)

Update: Gave up on the redirect idea, beacause it was breaking pagination on the blog. Well, I tried :)


French, Week 11

Didn’t listen to podcasts.
But kept up with the app.

I can now listen to the french sentences in the app and kind of get it.
No so yet, with stuff outside.

But I somehow think, I’m gaining the teensiest bit of fluency.

How to Say No, Better

Another James Clear pick today.

How do you make it easier on yourself to say no?
To stick to that diet?
To stop goofing off and buckle down and study or work?

Because,

The ability to overcome temptation and effectively say no is critical not only to your physical health, but also to maintaining a sense of well–being and control in your mental health.

We do this, by assertion rather than denying ourselves day in and day out.
Not that I can’t do this. Just that I don’t. I am a person who eats good food. So I don’t eat junk food.
I am a person who wants to be a programmer. So I don’t browse the web aimlessly all day.
I am a person who wants his mind in his control all the time. So I don’t drink.

When you decide ‘who’ you want to be, it becomes easier to decide, ‘what’ you don’t want to do.

Here’s an anecdote, James shares,

Group 1 was told that anytime they felt tempted to lapse on their goals they should “just say no.” This group was the control group because they were given no specific strategy.

Group 2 was told that anytime they felt tempted to lapse on their goals, they should implement the “can't” strategy. For example, “I can't miss my workout today.”

Group 3 was told that anytime they felt tempted to lapse on their goals, they should implement the “don't” strategy. For example, “I don't miss workouts.”

For the next 10 days, each woman received an email asking to report her progress. They were specifically told, “During the 10–day window you will receive emails to remind you to use the strategy and to report instances in which it worked or did not work. If the strategy is not working for you, just drop us a line and say so and you can stop responding to the emails.”

Here's what the results looked like 10 days later…

  • Group 1 (the “just say no” group) had 3 out of 10 members who persisted with their goals for the entire 10 days.
  • Group 2 (the “can't” group) had 1 out of 10 members who persisted with her goal for the entire 10 days.
  • Group 3 (the “don't” group) had an incredible 8 out of 10 members who persisted with their goals for the entire 10 days.

The words that you use not only help you to make better choices on an individual basis, but also make it easier to stay on track with your long–term goals.

So, say, I don’t, and you’ll say no more effectively :)

Why does this work better than I can’t?
How do I apply this to my life?
Read James’ article to find out.

P.S. And, go subscribe to the newsletter, if you are reading this on the web :)