From the course: Level Up: Advanced Python

Create an argument checker - Python Tutorial

From the course: Level Up: Advanced Python

Create an argument checker

(upbeat video game music) - In this challenge, we're going to create an argument checker or arg checker decorator. Now, in Python, version 3.6 type hints were introduced. This allows you to specify what type an argument is. So is it an integer, a float, a boolean, and so on. Let me give you an example. So let's say we had a function called multiplier that has four arguments and returns the product of them. So multiplier 1, 2, 3, and 4 and I get the product of them as expected a value of 24. Now, if I entered three arguments instead of four I should raise a type error stating function multiplier takes four positional arguments, but three were given. Now let me just go ahead and clear my screen. Now, if I change one of the arguments to a type float, I should raise another type error saying the function multiplier expected positional arguments of type class int but got class float instead. And like all good decorator functions it should maintain the name and the doc string of the function that it decorates. This means that I can get the function name for multiplier. Let me go ahead and clear my screen. This means that I can get the function name for multiplier so, multiplier.name will give me the name and multiplier.done.doc will give me the doc string for the multiplier. And as you can see, the doc string for multipliers return the product of the argument. So here's the challenge. Create a decorator using the arg checker dot pie file that checks the number of arguments in a function and raises a type error. When there's a mismatch, it maintains the name and the doc stringing of the function that it decorates. You can test your solution using pytest. So go ahead and pause the video here and I'll show you the solution that I came up with. (upbeat music) We'll start off by importing wraps from funk tools. I'll talk more about this when we use it later in the code. We want our arg checker function to accept all of the positional arguments that are stored in arg types. We then go ahead and create a decorator function and then to ensure that we can preserve the name and the dot stringing of the function that we decorate. We use wrap from the funk tools module. In the wrap function, we want to make sure that the number of arguments in our decorator matches the number of arguments in the function that is decorated. If they don't match, you want to raise a type error and we can get the name of the decorated function by using func dot dnda name. The next check we want to do is to ensure that each of the positional arguments and the decorator is of the same type as in the function. We do this by zipping the decorator arguments and the function arguments together and then checking them one at a time. If the function argument isn't of the same type the decorator at that position, we raise another type error. And finally, we return each of the inner functions and we're done. So let's go ahead and check this passes all of our tests, so pytest, tests and test arg checker. And from the output here we can see that it's passed all the tests. Now my solution is just one way of solving this problem. Go ahead and share your solution in the Q and A section. Now, just so you know, if you just post your code in the Q and A section as plain text, it won't be formatted and so it'll be difficult to read. You might want to post the link to your code snippet using something like GitHub's "gist" instead. I'd love to see your answer to this code challenge.

Contents