Skip to main content

Writing more readeable code... than in the previous blog!

As it happens, sometimes code works, but after a while you go back to your lines of code and you see how complicated is to get back to what you did, what that variable was there for, etc.
I tried to make more readeable the code I posted in the last post... and here it is.

The code takes the names from an external file. There is one name and surname for row, like these:

Agresta Lenza
Filomena Cesareo
Annarita Cortazzo
Pasqualina Amoresano
Tiziana Iuliano
Fortunata Mangia
Matteo Cavallo
Angela Imbriaco
Ortensia Russo
Marotta Sansone
Maurizio De Vita
Caputo Califano
Annarita Rambaldi
Sara Piciocchi
Marotta La Marca
Ida Franco

Giovanni Saturno


And now let's see the code to reverse surname with names.


I put the code to grab the names in the txt file... returning it as a list, of course. You must give an arg to the function: the name of the txt file, as we will see at the end.
Then I put the code to 'reverse' the name and surname in two functions. The first, up here, takes care of a 3 parts name that can have the case of 2 names or a surname made of "De" or "La" before the surname (those are the only type of names I have in my list of names in the txt file). Below we got the function that takes care of a 4 parts name.

I changed other parts in the code. I put the parts rearranged in a list called name. Then I appended to the "wholename" list a string that passes in the curly brackets the sigle values of the items with the format methods, a very clean method for combining strings and variables.
Ok, now this part is more readeable than before. The list is iterated and each name is divided in parts, if the parts are 3 it goes to the names3 function, if 4 to names4, otherwis it's the simpliest case of a 2 parts name that is handled in this function.
Then the wholename list is sorted (by surname this time) and the name are printed.


Finally... we use lista to memorize the list generated by grabNames function (passing the arg that is the name of the file with the names and surname of people in the list).
Then we create the wholename list (empty).
Then we call the main function surnameBeforeName passing as arg lista (the list of names) and the reordering will be done as we've seen just before.

This is the result:

0 ) Amoresano Pasqualina
1 ) Califano Caputo
2 ) Cavallo Matteo
3 ) Cesareo Filomena
4 ) Cortazzo Annarita
5 ) De Vita Maurizio
6 ) Franco Ida
7 ) Imbriaco Angela
8 ) Iuliano Tiziana
9 ) La Marca Marotta
10 ) Lenza Agresta
11 ) Mangia Fortunata
12 ) Piciocchi Sara
13 ) Rambaldi Annarita
14 ) Russo Ortensia
15 ) Sansone Marotta
16 ) Saturno Giovanni

Comments

Popular posts from this blog

Widgets for Jupyter Notebook: a text input widget

Widgets for Jupyter notebook ¶ Let's import the module ipywidgets into the Jupyter Notebook from ipywidgets import widgets from ipywidgets import * from traitlets import * Now we import the display function from IPython ¶ let's attach a function to the event on_submit After we run this cell, we can go up and write something in the text widget and after you submit the text you wrote will be printed after the cell from IPython.display import display text = widgets . Text () display ( text ) def handle_submit ( sender ): print ( "Thank you for entering this text:" , text . value ) text . on_submit ( handle_submit ) Thank you for entering this text: Ciao

Image in Jupyter and PIL step by step

Hi, """ Hi, we will see a step by step tutorial about PIL and IPython.core.display modules to create images from other images and diplaying them in Jupyter notebook """ # What we will do # Create a card # 1. Take a pic of a heart # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle # 4. show the card """ As first step wi will simply display an image on the notebook. I will show two way to display the image with 'display' from IPhyton a. Using the open method of PIL.Image (named Img) b. Using the Image method from the IPython.core.display module """ # 1. Take the pic of a heart from IPython.core.display import Image , display from PIL import Image as Img heart = 'img/heart.png' display ( Image ( heart )) display ( Img . open ( heart )) # 2. Create an image blanck the size of a card 90*130 # 3. Paste the heart in the middle #...

Let's draw a circle with PIL in Python

Let's continue making our coding around PIL. Let's start with some basic drawing: a circle from PIL import Image , ImageDraw img = Image . new ( "RGB" ,( 60 , 60 ), 'white' ) dr = ImageDraw . Draw ( img ) dr . ellipse (( 0 , 0 , 60 , 60 ), 'yellow' ) img . show () this is the image produced *If you use jupyter notebook, just write img at the end to see the output.