The other day I was trying to “print-debug” a piece of Python code. Well, I know that you are looking at me with a frown and telling me - “Use pdb or similar man! At least use logging.” I agree! I agree!! But, can you deny the fact the you have also plunged into the pool of guilty pleasure of simply throwing around some good’ol “print” here and there in your code, more often than you are ready to admit? :D
Long story short, I ended up having a lot of prints and things were not looking rosy. Then suddenly, a realization came, “Can I customize print?”.
It was a fun 15 minutes, working with that. And I am sharing it with you.
So the first version I came up with was looking something like the following
import builtins
def print(*args, **kwargs):
fillers = "*"
builtins.print(fillers * 10)
builtins.print(*args, **kwargs)
builtins.print(fillers * 10)
When used, it will do something like this
print("My name")
>>>
**********
My name
**********
That is nice and good, but can we change those stars to any other characters that we like? such as “=” or “+”. I was not comfortable to pass on custom kwargs to the built-in print, so it was obvious that I will need to delete that before. My solution looked like this
import builtins
def print(*args, **kwargs):
fillers = "*"
if kwargs.get("fillers"):
fillers = kwargs.get("fillers")
del(kwargs['fillers'])
builtins.print(fillers * 10)
builtins.print(*args, **kwargs)
builtins.print(fillers * 10)
And this I can use like the following
print("My name", fillers="=")
>>>
==========
My name
==========
Pretty neat, right? and fun also.
We will look more inside “builtins” soon again. Until then, Au Revoir
From Around the Web
Generate synthetic training data in Blender with zpy (Effective, Efficient, Scalable) - https://github.com/ZumoLabs/zpy
Weaviate is an open-source search engine powered by Machine Learning, vectors, graphs, and GraphQL - https://github.com/semi-technologies/weaviate
View, edit and execute Jupyter Notebooks in the terminal - https://github.com/davidbrochart/nbterm
Mathematician Disproves 80-Year-Old Algebra Conjecture - https://www.quantamagazine.org/mathematician-disproves-group-algebra-unit-conjecture-20210412/