If your Python project has a very short list of required packages (in requirements, pipfile, etc), it's easy to see all packages you have. But on large projects, the dependencies can run pretty long, not to mention the dependencies for the required packages. And what about learning more about those dependencies? pip list Will list … Continue reading TIL: installed packages in Python – list, and show
Category: python
A caller id for your python function
Most of you might be too young to know this but there was time that the phone in your house - not in your pocket! - would ring and gasp! you had no idea who was calling! You had to ANSWER the phone to find out. š¤¦āāļø Now we have caller ID and know exactly … Continue reading A caller id for your python function
Getting started with gRPC – part II: the code
In a previous post I wrote a summary of the things I learned about gRPC. Here I will talk about the prototype app I wrote to test gRPC and gRPC-Web. About my prototype As I mentioned before, I wrote a prototype app that tests if the string that the user typed in the browser is … Continue reading Getting started with gRPC – part II: the code
Getting started with gRPC – part I: the what
I recently spent some time researching gRPC and this post is a summary of what I learned. I also wrote a prototype application to test gRPC and gRPC-Web with Python and JS. The JS client takes a string from user input and test if it's a palindrome. The code can be found on GitHub and … Continue reading Getting started with gRPC – part I: the what
Difference among list, tuple and set in Python and when to use each one
Python has three similar data structures that can hold an unordered collection of elements: list, tuple and set. Here's how they differ: initialization list: using the built-in list() or the square brackets ([]): list1 = list() # creates an empty list list2 = [] # also creates an empty list list3 = ['a', 'b', 'c', … Continue reading Difference among list, tuple and set in Python and when to use each one
Debugging Python applications (plus free cheat sheet)
My primary debugging tool is to add print statements to my programs</confession_time>. Print statements are very easy to use and they work well for any simple scripts. But that's the catch: if you're debugging an application and/or a test file, print statements won't be enough or will just not work (in the case if tests … Continue reading Debugging Python applications (plus free cheat sheet)
Add a filename in the command line when running a Python script
OR: Run a Python script with arguments You can add arguments to the command line when running python scripts by importing the sys module from Python's standard library and using its argv function. Some examples: A simple script that displays the user name: def main(): name = 'Frodo' print(f'Hello, {name}') if __name__ == "__main__": main() … Continue reading Add a filename in the command line when running a Python script
Display nested dictionary content sorted by key in Python
Given a nested dictionary like this: dog_breeds = { 'Labrador Retriever': {'life_span': 14, 'male_weight': '36 Kg', 'female_weight': '32 Kg'}, 'Beagle': {'life_span': 15, 'male_weight': '11 Kg', 'female_weight': '10 Kg'}, 'German Shepherd': {'life_span': 13, 'male_weight': '40 Kg', 'female_weight': '32 Kg'}, 'Jack Russell Terrier': {'life_span': 16, 'male_weight': '8 Kg', 'female_weight': '8 Kg'}, 'Rottweiler': {'life_span': 10, 'male_weight': '60 Kg', … Continue reading Display nested dictionary content sorted by key in Python
Profiling: check how long it takes to run a Python script
From Python's official documentation: "AĀ profileĀ is a set of statistics that describes how often and for how long various parts of the program executed." Python's standard library provides two implementations of the same profiling interface: cProfile and profile but I always go with cProfile for short scripts because you can invoke it at run time like … Continue reading Profiling: check how long it takes to run a Python script
Map a string in Python with enumerate
Problem: create a map of the letters and indicesĀ in a string. My first approach was to loop over the string using range(len(s)), and checking if the letter exists in the map before adding it: s = 'mozzarella' mapped_s = {} for i in range(len(s)): if s[i] in mapped_s: mapped_s[s[i]].append(i) else: mapped_s[s[i]] = [i] print(f' mapped: … Continue reading Map a string in Python with enumerate