Code Challenge Pilot – code review

By on 5 January 2017

Intro

First of all we have to praise Julian for learning by challenge, submitting his code to github for review, starting out as a coder this takes tremendous courage. Quick round of applause please …

The flip-side though is that he will learn super fast his way, he is on his way to become a Python ninja 🙂

As described in the previous article our first challenge was to:

Parse a copy of the loggedin practical JS course page calculating the total course time. We focussed on the html parsing, not scraping for now (we will use BeautifulSoup in a future post for sure!)

The submitted code is here.

Refactoring 1.0 – making it more Pythonic

Enter the code review.

Github’s history link lets you browse all the commits. This is very convenient and it shows the benefit (best practice) of making git commits as small as possible. This actually made it very easy to show all refactorings in chronological order, which I will do in a bit.

Before we dive in …

  • Note that starting 5a34e5a, I ran the script before each commit to see if my assert would work (regression).

    For convenience I use this shortcut in my .vimrc:

    nmap ,p :w:!python3.6 %
    

    I love this shortcut. This way I can just press comma+p and it saves the script and runs it, this saves a lot of cycles as you do this over and over (run tests -> refactor -> run tests).

    Ah and yes, you probably want to try 3.6 by now 😉

  • Secondly below could be picky (sorry), but I just want to point out as many things as possible to get the most out of this exercise.

  • Again see the history link or clone our blog_code repo and go through it with:

    $ git log --oneline --reverse 66fb7c7fe..9876f968b
    $ git show 
    

Commit by commit

Conclusion

Julian, very well done on the challenge man, you are making good progress.

I hope this inspires you and the readers to think about making code as Pythonic and clean as possible, because the extra time upfront saves a lot of time later on.

Any feedback or questions use the comments below, or if code specific: use the comment box Github has for each commit.

These refactorings are suggestions, I am learning too, so any improvements are welcome …

About Code challenges

As Julian explained:

Bob and I thought it’d be interesting to do some code challenges. That is, Bob specifies the challenge and I complete it. Bob then goes through my code and makes any necessary edits/improvements to make it more Pythonic.

We plan to do a code challenge here once a week. Stay tuned.

If you like this subscribe below of follow us on Twitter. Thanks for reading.

Update 4th of Feb 2018

6.841944444444445 is not the best way to show a timestamp. We updated the script to:
– have the assert expect 6:50:31,
– use divmod in our calc_duration (OK, we also sneaked an f-string, sorry we could not resist the temptation),
– added calc_duration_improved to use datetime and timedelta to calculate the total duration of the course.

See the new revision here.


Keep Calm and Code in Python!

— Bob

Want a career as a Python Developer but not sure where to start?