Skip to main content

Posts

Showing posts from October, 2016

Searching for words in a text file

So... in this example, we will see how we can search in a txt file the information we want based on a keyword. There is a list of workers that do their job once a week. We want to know who worked on each day of the week as the list is not ordered in this way. Here is the list in the txt file (called workday.txt). Save this list as a txt file with the name indicated. Then save the code in a file called searchingdays.py (or anything you like). The list Amoresano Pasqualina friday Califano Caputo monday Cavallo Matteo thursday Cesareo Filomena wednesday Cortazzo Annarita monday De Vita Maurizio monday Franco Ida friday Imbriaco Angela monday Iuliano Tiziana monday Lenza Agresta wednesday Mangia Fortunata friday Marotta La Marca monday Piciocchi Sara saturday Rambaldi Annarita friday Russo Ortensia monday Sansone Marotta tuesday Saturno Giovanni thursday Starace Guglielmo saturday The code def goSearch(day): with open("workday.txt",encoding="

Transform a text in Mp3...

I made this video some time ago. I could do other posts to explain how to mix different languages in one mp3 file easily.

Virtualenv for another version of Python

To exit from a virtual environment, we write in the console deactivate Now we can create a virtual environment for python 2.7 that I have on my pc and that is not the default version that appears when I open the prompt line command or when I use Sublime Text... I just have to write this: cd.. cd.. to go in my env directory (where I choose to put all my environments) and write: and the new environment will be placed in the env2 folder (I can choose whatever name I want). My python 2.7 is in the folder python27, but your could be in another version... do your adjustments. Now write cd env2 cd scripts activate As you can see ... the (env2) shows that we are under the env2 environment. If we write the command python, you will see this: As you can see, the version of python is 2.7, even if in my computer the default version is 3.4. If write print "Hello" it will not show errors, because in this version it is allowed to avoid the pare

Virtualenv installation

There are two version of python. The 2.7 and the 3.5 (or more if you are reding this in the future). You could start your projects with different versions and different modules (of different versions). How can we work without having to think to the different versions of this stuffs..? With virtualenv. First of all install it with pip. Go in the command line and write (see the picture): pip install virtualenv Now, we will see how to use different versions of python with no problems. Now create a folder "venv" and right click on it, click on open command line window from here and the prompt of the command line will appear. Let's say we use python 3.4 as the default version write this Thes the pc will make something My version of python is in the python34_32bit folder, your will be in a different folder, and a different version. The program will install some basic stuff and it's done. Now write this 3 commands: cd env34 cd scri

Computer: speak!

Ok, so we want to make our Windows Pc speak, right? 1. We import our module win32com 2. We create our istance of the class Dispatch passing the SAPI.spVoice string as arguments (that's the voice used by the pc) 3. Then we are ready to make the pc talk... easy and fast! >>> import win32com.client as wincl >>> speak = wincl.Dispatch (" SAPI.SpVoice ") >>> speak.Speak("You will break that stuff, dude") But first you have to install the win32com module.

Working with BIG DATA (not so big, indeed)

# We got some data in form of a string like this # the data goes from 0 to 4 and I want to # count how many 0,1,2,3 and 4 are there # PS: I've taken them from a google module # for statistic purpose data = "2 3 0 0 4 3 2 3 3 0 0 4 2 1 0 2 0 0 1 0 2 2 3 2 0 3 2 0 1 1 3 2 2 2 1 0 0 3 1 2 2 4 2 1 0 0 0 0 2 3 1 0 1 0 2 1 3 3 1 2 2 2 2 1 3 1 2 0 3 1 3 0 3 1 3 0" # Let's transform the string in a list with the split() method of the string objects listnum = data.split() # Now, let's use a list comprehension to populate a list with # numbers instead of strings """ other way to change strings in a list into integers  for lit in listnum: listnum[listnum.index(lit)]=int(lit) """ # We will use this way listnum2 = [int(lit) for lit in listnum] # The converted in int list print(listnum2) # Now we create a dictonary with # keys = "0" "1" "2" "3" or "4" # and the value

Counting words in a text with python

"""Let's count words in a text. We got a text like this:""" message = " How it happened that Mastro Cherry, carpenter, found a piece of wood that wept and laughed like a child. Centuries ago there lived-- \"A king!\" my little readers will say immediately. No, children, you are mistaken. Once upon a time there was a piece of wood. It was not an expensive piece of wood. Far from it. Just a common block of firewood, one of those thick, solid logs that are put on the fire in winter to make cold rooms cozy and warm. I do not know how this really happened, yet the fact remains that one fine day this piece of wood found itself in the shop of an old carpenter. His real name was Mastro Antonio, but everyone called him Mastro Cherry, for the tip of his nose was so round and red and shiny that it looked like a ripe cherry. As soon as he saw that piece of wood, Mastro Cherry was filled with joy. Rubbing his hands together happily, h

How to get an Image from the net and save it

Ok, today an easy task: get an image from the net and save it! For the record, this image here: Here is the code: from urllib.request import urlopen site = urlopen(' http://placekitten.com/250/350 ') data = site.read() kitten = open (' kitten.jpg ',' wb ') kitten.write(data) kitten.close() Just six lines of code and you're done. Here is the code in Sublime Text 3: At the end in the folder where you have placed the file (.py) with this code (after you run it, of course) you will see this cute little kitten: Let's look at the code for some details: We import this method from the module urllib.request (this code is for python 3, for python 2 the library has another name). Here we take the data from the site... where the pic is. Here we create a file for writing data in it ("w" is for write, if we put "r" or nothing it would just open an existing file). After we created the file, we write the data (the va

Ordering lists of names: still another version

Finally... another version without that format thing... it is a little longer. It is a little longer, yes, but a little more vertical... a little more concise. It's a matter of taste. Let's compare it with the other one:

When you write too many lines of code... Ordering lists again

Did I use too many lines of code in the last post? Yes, something like 56 lines of code were too much... I could have done it in 20 lines of code (or even less... we will see it). This is the ElencoCorsisti3.txt content Agresta Lenza Guglielmo Starace 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 This is the new code... The result is the same (I have omitted the numbers): Lenza Agresta Starace Guglielmo Cesareo Filomena Cortazzo Annarita Amoresano Pasqualina Iuliano Tiziana Mangia Fortunata Cavallo Matteo Imbriaco Angela Russo Ortensia Sansone Marotta De Vita Maurizio Califano Caputo Rambaldi Annarita Piciocchi Sara Marca Marotta La Franco Ida Saturno Giovanni There could be other "tricks" to use even less code

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.

Ordering lists... stored in a txt file

Ok, this time we want to take the list of names from a file. Let's say we have this names written in a txt file called Agresta Lenza Guglielmo Starace 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 Rosalba Boselli Giuseppe Rossi Lucia Pirone Flavio Rosciano Maria Mangia Guglielmo De Vita Guglielmo Balzano Paola Russo Flavia Turriziani Pasqualina Renato Rosalba Starace Ida Piccione Gaetana Cortazzo Anna Starace Olga Russo Giovanni Cortazzo Annapina Rubano Caruccio De Angelis Olga La Marca Mariagrazia La Regina Anella Piccione Annarita Passaro Angelamaria Rosaria Cozza Agresta Orco Grazia Turriziani Flavio De Luca Annapina Russo Annarita Rattazzi Dario Luongo Michele De Angelis Rosalba Cortazzo Pasqualina Rossi Now, lik

Ordering lists, splitting strings and other stuffs...

Let's sort a list of names... in many ways Hi, let's say we have a list of names like the following: listofnames = [ 'Caputo Mariangela', 'Filomena Cortazzo', 'Tommaso Peduto', 'Filomena Rossi', 'Antonietta Russo', 'Caputo Iuliano', 'Rosalba Rubano', 'Marotta Rubano', 'Daniela Cortazzo', 'Anna Gallo', 'Nunzio Buccino', 'Maurizio Amoresano', 'Rosalba Concetta', 'Maria Isabella Luongo', 'Iuliano Mariangela', 'Derna Cimmino', 'Anna Rossi', 'Flavia Damato', 'Derna La Marca', 'Guglielmo Mariangela', 'Caruccio Mariangela', 'Regina Madaio', 'Vincenza Balzano', 'Maurizio Giorno', 'Marco Lenza', 'Domenico Tancredi', 'Ortensia Russo', 'Carlo Amoresano', 'Guglielmo De Luca', 'Annapina Califano', 'Gaetana Margiotta', 'Tommaso La Regina

List comprehension through an example: cards game

Ok, I like doing examples instead of explaining theory. That will come later. We want to generate a list with list comprehension, right? So let's do it with this example: I want to generate a list of cards like this: 1 hearts, 1 diamonds.... etc. This is what the code will look like: ------------- code ------------------ cards = [str(i) + x for i in range(2,11) for x in ['♦','♣','♠','♥']] cards += [i + x for i in ["J ","Q ","K ","A "] for x in ['♦','♣','♠','♥']] print(cards) -------------------------------------- and this is the result: ['2 ♦ ','2♣','2♠','2 ♥ ','3 ♦ ','3♣','3♠','3 ♥ ','4 ♦ ','4♣','4♠','4 ♥ ','5 ♦ ','5♣','5♠','5 ♥ ','6 ♦ ','6♣','6♠','6 ♥ ','7 ♦ ','7♣','7♠','7 ♥

sys.argv or ... how to get an input straight from the console

Hi, let's say we want to make a program to find the square of a number. Let's say we don't want to get the input from the user with the input() function in the usual process, but we want to input the number that we want to process by the program when we call the program in the console in this way: C:\examples > python square.py 3 The square of 3 is equal to 9 So, it's a nice way to do things in a quick and easy way.. nothing to run... just what you saw. The code to do that is this: ---------------------- code ---------------- import sys # sys is the module that we need for this square = int (sys.argv[1])**2 print ("The square of {} is {} ". format (sys.argv[1],square)) ---------------------------------------------- Now... save the file as "square.py" go check for the folder where you put the file (in my example in "examples") and when you see the icon of the folder press shift and the right button of the mouse: f

List comprehension (a sort of an intro)

Hello World! We have a list like >>> people = ["John","Jack","Jim"] Let's say we want to add a number next to each one of this name. we could use this way ------------------------------- code ------------------------ people = [ "John" , "Jack" , "Jim" ] x = 0 for names in people : people [ people . index ( names )] = people [ people . index ( names )] + " " + str ( x ) x += 1 print ( people ) ------------------------------------------------------------- otuput: ['John 0', 'Jack 1', 'Jim 2'] -------------------------------------------------------------- Let's explain it a little bit. with for names in people : we iterate the list "people", right? We go through "John","Jack" and "Jim"... to add the number ...we used this system: to find the index of each name in people an

How to delete an item from a list

So... we  made a list of names called, by the way, names  (this is the name of the list, the label, the object is the list, while names is just the label, or name, of that object). names = [ "John","Carl","Mary","Sean","Sally"] What if I want to delete Carl? If I know that Carl index is 1, as it is, I can write this code: >>> del names[1] But, what if I don't know the index of Carl? Well, we can find the index of it and then delete it. Let's make that Python does all the work. >>> del names[names.index("Carl")] If you want you could have done, taking it easier, this way >>> indexOfCarl = names.index("Carl") >>> del names[indexOfCarl] It's a bit too long, but the result is the same. In fact: >>> names [ "John","Mary","Sean","Sally"] There in not the name of Carl in the list any more. Let's po