Skip to main content

Posts

Showing posts from June, 2020

docstrings in python programming

What is docstrings? To quote PEP 257 from python's own documentation, " A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object. " To ease this definition out, basically, a docstring is a piece of string or a small note which is generally provided in a class, function or a method definition for informing as well as documenting important information about the class, function or the method. There are two basic types of docstrings, which are oneliner docstrings and multiline docstrings. How to write docstrings? docstrings are generally literal strings and therefore you can write them within """ triple double quotes""". But there are certain rules in case of unicode or backslashes. If your docstring information is supposed to have backslash character then you have to use r"""raw triple double quot

what is polymorphism?

Introduction: polymorphism is one of the many concepts in object oriented programming. I have previously discussed in details about different parts of object oriented programming in this OOPs post . In this post, we will discuss about polymorphism. Definition: In object oriented programming using a function for many purposes is termed as polymorphism. Polymorphism is a way to use the same function name, while it operates differently based on different parameters and classes it occurs in. I will explain it with a small example now. Example: Look at the following code: class examplesome:    def __init__(self,a,b,c):       self.a = a       self.b = b       self.c = c    def funct(self):       return self.a+self.b+self.c class examplesum:    def __init__(self,a,b):       self.a = a       self.b = b    def funct(self):       return self.a+self.b Observe here that examplesome and examplesum contains functions with same name.  But application wise, they are abruptly differen

Python unit testing frameworks

Introduction: In this post, we are going to describe the different frameworks used to run unit test for python codes. This is not a comprehensive article on unit testing in general, but the aim is to explore several frameworks for python code unit testing.  This post is intended for beginner and amateur level python developers who are interested to explore python unit testing frameworks. First lets explore the different testing scenarios one encounters. Different testing scenarios: There are mainly two types of testing in software, functional and non-functional. We have taken these common test types from softwaretesting   site. Functional Testing types include: Unit Testing Integration Testing System Testing Sanity Testing Smoke Testing Interface Testing Regression Testing Beta/Acceptance Testing Non-functional Testing types include: Performance Testing Load Testing Stress Testing Volume Testing Security Testing Compatibility Testing Install Testing Recovery Testing Reliability Testi

TypeError: list indices must be integers or slices, not str

Introduction: This error generally occur for very simple issues. The error normally translates to python saying that, you are trying to access a string index of a list; which is impossible as lists are indexed by integers and not by strings. Recreation of the issue: So this can be caused by something as simple as food = list['abc'] . But the problem is that we often write complex codes and therefore we don't easily find out this mistake. This sort of mistake if you are seeing in your code, please check that most probably you are passing a list in a place where there should be a dataframe or at least a dictionary. I have several times encountered this error in real coding scenarios. One of them have been where I wrote a condition statement inside a dataframe and this issue came. Later on, I found out that the variable I thought was a dataframe was in fact misspelled and actually was referring to a list I created one line above. Conclusion: So, it is important to examine th

how to check if a value is equal to nan in python

Introduction: Normally we check a lot of times whether some value is equal to something or not. Now, you may need to check the same with a nan, NaN, NAN value too. But the problem arises when you observe that a simple check nan == nan fails. Why does the check fails: The reason nan == nan sort of checks fail is because nan does not equate to anything, even itself. That's why, equating it ends up giving false. Solution: So, after going through stackoverflow, I found that the trustable solution which works for me as well, is np.isnan() function. There is a pd.isna() too for this same purpose. But for this example, I have used np.isnan() function only. Please observe the use of isnan below: Observe how all the forms such as nan, NaN, NAN are getting detected by np.isnan().