Skip to main content

Posts about python (old posts, page 2)

Learning Python

Did I need to read a fifteen hundred page book to learn Python?
At the end of fourteen hundred pages, I can safely assure you, I did not.

If you want to just solve your pressing issues or scratch your itch, or just plain get started with programming (and programming in Python specifically), I’d recommend starting with a simple, fast paced book, like Python for you and me, and then doing tons of practice.1

Mark Lutz, as he closes the book, himself laments that Python has gotten too big to hold in your head. And by doing so, has lost some of the simplicity and the joy and fun and the magic, Python held for the early adopters of the language.

And yet, having said all this, boy, am I glad, I read the book.

This is a master class from a master.

I may not have understood everything. I may have skimmed a chapter or two (Lutz assures me, it’s ok :P), but what this book has done to my mind, the furrows it has ploughed, will be with me forever.

I have been trying to get into the book, multiple times since I bought it.
It took me a long time, before, as Mortimer Adler puts it, I could come to terms with the author.
The only reason I kept coming back, was because, Mark’s earnest teaching voice shines through, and I loved it, even if I did not quite get what he was saying in the beginning.
And the reason I could get through it (and enjoy it) this time, because I decided to follow his advice and follow along on the computer.
To actually type in the code, and see what happens.

Yes, the book is big, yes, the concepts are repeated a couple of times, but as I progressed, I could feel him sweating the small stuff over and over, just so that I could understand things, so that I would not get scared away.

Time and again, the book reassured me, that what was said, was not as complicated or hard as it read on the page.
And that turned out to be true as I kept trying the examples out.

While I still have a long way to go, before I can remotely be called fluent, I know this book will have a been a big reason, I will be.2

This book was last updated, oh, some six years ago, and yet unless Python decides to change radically, I dare say, the principles in here will stand the test of time.

This was a great read and will serve as an awesome reference on my Python journey.
If you are slightly kooky like me, and you want to know, why things are the way they are as you learn to program in Python, get this book.


  1. Which is actually, what I am doing. 

  2. Besides the practice, that is. 

Generating Markdown from HTML

2019-08-21

Started with the problem,
Need to take in an md file and then generate an html file.

Hint given, use a package from PyPI.

Decided to use the Markdown package from PyPI.
Looks good to me.

Was advised to work off a branch while developing.
Thank God for friends who teach you good habits.
Looking up how to do that in git now.

Watched Git videos for about an hour.
Learnt lots about branches. Giving up for today. it’s 8 in the eve and i am tired.

2019-08-22

Captain’s Log, Stardate something, something :P
Let’s see what the day holds.

  • Figured out how to read in a file.
  • Figured out how to convert it.
  • Figured out how to write to a file safely.
  • Now gotta figure out how to write the same file name as the one i feed it.
  • 11:44:24, went on a little rabbit hunt for how to type times in here :)
  • 11:45:21, first time ever, that I’ve hit a flow state, doing Python
  • 12:18:03, got it working, yea baby!
  • 12:40:55, Kushal suggested corrections and improvements. getting to work on those later.

2018-08-23

Captain’s Log, stardate, weekend on terra begins XD

  • 09:04, beginning with Kushal’s suggestions
  • 09:38, beginning now. :) had a power cut
    End day. Nothing done.

Au Revoir DTW

Decided to pull the plug on the Do the Work blog.
While I wanted to use it for my tiny, crazy, work in progress thoughts, I find that it was increasingly being subsumed by my new shiny Mastodon.

And as the volume of things I write now scales up, I do not want another place to maintain.
I’ve migrated all my posts here.

Will use this blog for the long and medium stuff going forward. And the short, ooh shiny stuff is on my Mastodon account.
Read the kooky struggle there, and if you want a social account that does not do evil things to you, get an account yourself.

Thank you for coming along :)


First Python Program

Ok, really thrilled today.
Patting myself on the back.

I finally managed to write a program all on my ownsome. Kushal gave me a toy problem and I went around, scratched my head, did a lot of searching, a lot more headbanging, even more mistakes and then finally managed to write this.

Am happy because this is how I imagined myself learning in the first place.
Figuring out a problem someone has and then figuring out how to help them.

Also happy, because this makes me more confident.
Having gotten over the hump of my first program, I now am looking forward to doing more, instead of being apprenhensive about how to do it.

Onwards.

P.S. The problem was to process a summer training log file and print out, how many words, each participant spoke.
The code’s here, if you want to see it.


Study, Day 9

French

The app! the cursed app!
Just did the bare minimum today.

Programming Practice

  • Didn’t waste tooooo much time today :)
  • Manged to figure out what was actually asked for in Python terms and wrote it down as instructions in the file.
  • working at slowly converting them to code now.
  • All the fussing I did over fonts and setting things up yesterday paid off. The editor has receded to tho background and I can just work.
  • I made progress with the program today.
  • only a tiny bit left.

Python Jumpstart by Building 10 Applications

  • The fourth app kicked my butt.
  • got it working, but it was really hard going.
  • done for this week. will attempt next week

Study, Day 8

French

The app is now buggy, since the last update and crashes on me all the time.
Am at it though.

Python Practice

This is stupid.
I spent an hour just setting up my environment all over again.
This should teach me to do at least a little bit every day.

  • 1 hour of setup
  • 1 hour of tremendous headscratching
  • No practical reasults.
  • Tomorrow hopefully is a better day.
  • The only positive thing is I feel, my mind has begun to crank again.

Python Jumpstart by Building 10 Applications

  • Started watching the videos.
  • Built three tiny apps in rapid succession.
  • Getting my python mojo back.
  • Run out of steam after 3 hours.
  • Ending on a high note, though. will pick up the pieces tomorrow.

Study, Day 7

Was on a roll.
Went and finished the whole geography textbook.
My brains are smoking now.

Next step, spaced repetition exercises of all the chapters for two months.

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

Did quite a bit of the course’s exercises. I realise now that I should have just downloaded the course from the Open Courseware site and worked on it at my own pace.
The MOOC is excellent, but not for me.
I keep missing all sorts of deadlines.
My life and the MOOC sadly do not get along.

Or maybe, I just do the MOOCs at my pace, knowing that there will be things I will miss, but as long as I get the learning (and I do the practice), I should be fine.

Did up to Week 4 of exercises.
Will tackle the rest next week.


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.)