‹ Kinyanjui Wangonya

Page 3


In this tutorial, we’ll create a simple todo api with Python 3 and Django 2.

After my first post on “Caring for your Health as a Developer”, I decided to do a bit of research on caffeine, specifically in coffee. I did learn quite a bit but what I found most interesting was that after oil, coffee is the 2nd most traded commodity in the world 🤯. People are really drinking a lot of coffee (2.25 billion cups of coffee on a daily basis). But while many may love and enjoy the momentary energy boost provided by caffeinated drinks, is it really the best way to go in the long term?

“When health is absent, wisdom cannot reveal itself, art cannot manifest, strength cannot fight, wealth becomes useless, and intelligence cannot be applied.” ― Herophilus

Let’s face it. As developers, it’s easy to let things get a bit out of hand health-wise. Many times we find ourselves in front of screens for hours on end. We sleep really late (or not at all) sometimes. Unless you’re disciplined enough to follow a schedule, you might find yourself not having time to eat proper meals so snacking becomes a habit. All these are usually sacrifices made to fix some bug or find the most efficient solution to some problem. Usually, our health suffers as a result. While this might seem to work out just fine for the moment, we don’t look at the bigger picture - how it will affect us in the future.

I think, if healthier practices were adopted, developers would be much more productive and would be able to accomplish some tasks in less time simply because the brain is much clearer. I might here quote surveys and researches conducted to prove this point but simple personal experience will suffice. It’s hard enough to get anything done when you’re down with a cold. There is an intimate connection between the mind and the body, and when one suffers, the other no doubt feels it. Below are some points to watch out for if you’re looking to keep fit physically and mentally.

In Javascript, code blocks are created using curly braces ({}). For example:

someFunction() {
    // some code here
}

anotherFunction() {
    // some more code here
}

someFunction and anotherFunction are two different code blocks. These two different code blocks could also be considered as two different scopes. What that means is that the variables declared in someFunction only affect that block of code, and those declared in anotherFunction only affect that block of code. They are “scoped” in that sense.

Given that one of the main reason computers were invented was to solve mathematical problems quickly, it is no wonder that all the modern programming languages are so rich in arithmetic-oriented methods. The earliest computers were basically just calculators. (Yes, I’m looking at you Abacus). If you dabble in Javascript (and a little math every now and then), you might find this useful. The very obvious operations like simple addition (+) and subtraction (-) have been omitted. So have more advanced operations.

Modules

Modules are single Python files that can be imported. Any python file can be a module. For example, if I have two Python files: module.py and hello.py in the same directory:

# module.py

def hello(name):
    print("Hello {}".format(name))

I can import that module in my hello.py:

#hello.py

import module

module.hello("World!") # Hello World!

I always have to search for how to do this or refer back to my previous code whenever I work with fetch. For a while, I’ve used the solution to this SO question. It’s a correct solution and it works great but to be honest, I couldn’t really explain very well what was going on if someone asked me to explain that piece of my code. So I thought of a simple way to do it. Here’s how:

Jan 31, 2019

Sometimes you might want to skip a particular test while executing others for some reason. Maybe the database guy isn’t done setting up and that particular test requires a database connection. Instead of having to wait, you can just write the test and instruct pytest to skip it, giving the appropriate reason so it doesn’t look like you just skipped a failing test to keep your test suite green.

There’s a couple of ways to do this.

You might need to capture print statements in your tests while debugging. This might be just to help you debug, or for some other reason. I mostly do this kind of thing when testing for database connections where, in addition to other tests, I want to make sure I see that “Database connection successful” message.

First time I had someone review my pull requests, she was pretty strict on tests. I couldn’t merge if the tests were failing, of course. But I also couldn’t merge if coverage had decreased. TDD was still new to me so maintaining coverage was a challenge since I was only testing the bare minimum I could. I had to find out how to make my tests more robust and ensure as much of my code was tested as possible. One area that I wasn’t really sure how to test was the custom exceptions I had written.

« Older posts Newer posts »