Code Challenge Pilot - code review

Bob, Thu 05 January 2017, Challenges

assert, bestpractices, code review, codechallenges, git, git flow, Github, pythonic, refactoring, vim

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

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

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book