Friday, 30 December 2016

Testing Python Code


Testing your code is very important to know if your code is having the expected behavior. No one likes a software program which would give a weird result or a program which would break in the middle of a critical task and throw of ugly error codes. For this and many more reasons it’s very important to test you code before making it public, testing is not a “best practice” rather it’s a necessity to deliver clean and delicious code which will obey all the commands that the user provides.
There are many different python modules like unittest that help you to test units of your code and then there are some tools like py.test that helps you achieve the same goal, but I want to point out a simple & fast way to quickly test your piece of code without importing any separate module other than the one that is included in your project or which you have created for testing your code.
Testing code in isolation
In every type of code you must first design your code in modularised way to make debugging and maintenance easier . Let me show you one example -
Consider the following code -
  1. def func_example1 (a,b):
  2.   return a+b
  3. def func_example2 (a,b):
  4.   return a*b
  5. def func_main(func_example1(a,b), func_example2(a,b)):
  6.   result = func_example1 + func_example2
  7.   return result
  8. func_main(func_example1(1,2), func_example2(3,4))   
The above pseudo code is modularised meaning that a specific feature is divided into different functions like func_example1, func_example2 & func_main. Now since the code is divided into different functions so we can test each function separately and to do this it’s better if a separate “test module” is created where we can invoke a particular function under test with mock objects
to check that the function is really performing the intended action. Let’s consider the above pseudo code example, I will place the above code in a module named as “main_module” (you can name it as you like). I will create another module and name it as “test_module” & import the “main_module” into that -
  1. import main_module
  2. # todo - call function to be tested
We have our module ready now so we can test each function individually and for this we will pass mock objects to the function under test by invoking the function from the main_module into the test module & recording its output.
  1. import main_module
  2. output = main_module.func_example1 (1,2)
  3. print("the output of func_example1 is" + output)
I invoked only the func_example1 from the main_module and passed the objects ‘1’ & ‘2’ (which may be different from the actual data) & I could now record & test the behavior of this function in isolation which will help me to zero in on any possible bug or identify any improvement needed in that particular block of code.
Brute force method to Test the program end to end
  1. def func_example1 (a,b):
  2.   Print("1st function is now invoked")
  3.   return a+b
  4. def func_example2 (a,b):
  5.   Print("2nd function is now invoked")
  6.   return a*b
  7. def func_main(func_example1(a,b), func_example2(a,b)):
  8.   Print("main function is now invoked")
  9.   result = func_example1 + func_example2
  10.   return result
  11. func_main(func_example1(1,2), func_example2(3,4))
In the above pseudo code I have inserted simple print statements inside every function so that each time that function is invoked anywhere the statement inside the print () will be displayed on the screen along with the regular return value or action of that function which can help us test if the desired function/code block is invoked and if the control is traversing the code in the expected manner.
Using print statements like above may be a brute force way or may be a novice way to test the control flow but it’s simple & is very useful way to quickly devise a system so that you can track your program flow. I am not saying that this is the only way or it is the best way to do things but it’s one of the practical way to test your code in a short period of time and it’s more visual. If you hate the “print” statements then you can create another function which will write the output message into a log file which can be later analysed to check if your program flow is as expected or you can refer the log in case you have a break in your code. So, folks this was all for this week but more stuffs are coming your way next week, till then happy coding.
If you found this post helpful and you have any suggestion to improve my blog then do post your comments below. Additionally you can also bug me on Quora. You can also find me on Twitter or Facebook. So, why wait go ahead and start exploring.


If you wanna get started in learning Python then why not get started with the python basics chapter. Check out this link to access the Python Basic Chapter for free.


If you want to download a copy of the chapter then purchase the chapter by clicking on the following link.

Friday, 23 December 2016

Python Tutorials

If you want to download a copy of the chapter then purchase the chapter by clicking on the button below. This lesson is based on google docs so if you have any doubts on any of the topics in this lesson then you can just put a comment against that topic and I will answer that question there itself so that you don’t need to navigate to any other page or document to view the answer.


Additionally you can also bug me on Quora. You can also find me on Twitter or Facebook. So, why wait go ahead and start exploring.

Click on the button below to purchase the chapter




Saturday, 17 December 2016

How to learn Python in short time?


Many times people ask me this question How to learn Python in as short time as possible ? . I myself had this question when I was first starting out and at that time I searched and searched over internet to find a magical shortcut to learn Python, but I found none. The reality is that the “short span” of time that we want is actually relative as for some it may take a month and for some it may to 3 months or even more and that depends on individual learning capacity which is OK. It took me 2 months or so to learn the language 😄. But that’s me someone else may take more or lesser time than me. The key here is to learn by doing. You can check through the following to start learning -
  • First start with the basics Visit the post in this blog to know about where to start so that you can get to know at least the basic concepts that can get you started with this programming language.


  • Once you get going with the basic concepts you can pick a small project and started working on it. To get an idea of what your project should look like you can visit this post on our blog initially your progress will be slow but this is what will drive you to research the topic on which you want to build your project and as you research more and progress on building your project you will gain more insight and deeper understanding of the programming language which will further help you in learning by doing .


  • Once you get started with your project then make it a point to complete it because if you leave that in between thinking that it is difficult to learn that way or you may find an easier project to work on then you won’t face any challenges while you work on that project which in turn will limit your capability to solve a difficult problem and this will keep you away from gaining the experience of creating an indigenous solution to a difficult problem. It is your ability to find a solution to a coding challenge that will give teach you deeper concept of coding.

Follow the above steps to progress in learning Python by actually coding which will give you more knowledge than just reading books or going through online courses. Remember the more you practice the more you will learn.


Subscribe to my youtube channel to watch new python programming tips and tutorials.

If you found this post helpful and you have any suggestion to improve my blog then do post your comments below. Additionally you can also bug me on Quora. You can also find me on Twitter or Facebook. So, why wait go ahead and start exploring.


If you wanna get started in learning Python then why not get started with the python basics chapter. Check out this link to access the Python Basic Chapter for free.


If you want to download a copy of the chapter then purchase the chapter by clicking on the following link.





Saturday, 10 December 2016

How Python implements “Data Encapsulation”?


Data privacy or “data encapsulation” is provided by Python by default. In Python every variable or method is scoped within its parent code block. Look at the below function-
  1. def newcurrencynote():
  2. newnote = 2000
  3. print("This is new" + newnote + "currency note")
  4. print(newnote)
  5. print(example())

In the above function newcurrencynote() the variable newnote inside the function has “ local” scope within that function, in other words it is private to that functions only and is not accessible outside that function . So, the first print function gives an error stating that you should initialize the variable first and then print it, but since the second function is trying to access the function itself so it will print the value of the variable.


If you found this post helpful and you have any suggestion to improve my blog then do post your comments below. Additionally you can also bug me on Quora. You can also find me on Twitter or Facebook. So, why wait go ahead and start exploring.


If you wanna get started in learning Python then why not get started with the python basics chapter. Check out this link to access the Python Basic Chapter for free.


If you want to download a copy of the chapter then purchase the chapter by clicking on the following link.