Welcome to the third edition of Python’s byte. Before we jump into this week’s short tip, I wanted to thank you all for the immensely positive response and support that you showed for this little effort. I hope to live up to the expectation. It is a difficult time at the moment. World, specifially India, is going through unprecedented hardship due to the second wave of COVID-19. I hope that you and your loved ones are doing well and pray that we come out of this tunnel sooner than later. Stay indoor as much as possible, use mask, maintain socail distancing. Stay safe!
This week we will try to see how can we use Python’s type hinting for something more than just static analysis. I know that some of you are thinking about Pydantic. Whereas that is a great library for doing such tasks, the particular type of validation we will be looking into, is still in an early stage in Pydantic.
Before we venture into the solution, let me state the problem. Imagine we have this following code -
def div_it(a: float, b: float):
return a / b
Very simple, right? But what if someone calls this function like the following
div_it(12.0, "my_number")
Although silly, these kind of mistakes are fairly common. Now, in a compiled and linked language, such as C, this would not compile and will throw an error (in most cases) but the same is not true for Python. Question is, how fast can we catch such an error and break (or otherwise log and ignore safely)?
Typeguard to the answer!
First install it -
pip install typeguard
And then, just add the following to the code
from typeguard import typechecked
@typechecked
def div_it(a: float, b: float):
return a / b
This magical decorator, @typechecked is going to safeguard you against all the type related errors. With this in place, a call like the following -
div_it(12.0, "my_number")
will throw an error like this -
TypeError: type of argument "b" must be either float or int; got str instead
It is simple, efficient, and easy to debug. And that is not all. You can “type-protect” your classes and even full modules at the import time using typeguard. Check out the documentation to know more.
Now please start using types more and more and use a library like Pydantic or Typeguard to break / ignore type errors as early as possible. Save the day!
From around the web
How often do people copy and paste from Stack Overflow? - https://stackoverflow.blog/2021/04/19/how-often-do-people-actually-copy-and-paste-from-stack-overflow-now-we-know/
Latest Neural Nets Solve World’s Hardest Equations Faster Than Ever Before - https://www.quantamagazine.org/new-neural-networks-solve-hardest-equations-faster-than-ever-20210419/
sheet2dict – simple Python XLSX/CSV reader/to dictionary converter - https://github.com/Pytlicek/sheet2dict
CuPy v9 is here - https://medium.com/cupy-team/cupy-v9-is-here-27e9cbfbf7e5
Gradient Descent Models Are Kernel Machines - https://infoproc.blogspot.com/2021/02/gradient-descent-models-are-kernel.html
If you feel that this newsletter is something you enjoy reading then please help me to spread the word.
See you next week :)