Δημοσιεύτηκε: 03 Ιουν 2009, 18:52
από dimosfire
Συνεχίζοντας τη μετάφραση υποβάλλω για έλεγχο το κεφάλαιο 14 και θα αναλάβω και το κεφάλαιο 15 (Εξαιρέσεις):

Κώδικας: Επιλογή όλων
Introduction

There will be situations where your program has to interact with the user. For example, you would want to take input from the user and then print some results back. We can achieve this using the input() and print() functions respectively.

For output, we can also use the various methods of the str (string) class. For example, you can use the rjust method to get a string which is right justified to a specified width. See help(str) for more details.

Another common type of input/output is dealing with files. The ability to create, read and write files is essential to many programs and we will explore this aspect in this chapter.
Input from user

#!/usr/bin/python
# user_input.py

def reverse(text):
return text[::-1]

def is_palindrome(text):
return text == reverse(text)

something = input('Enter text: ')
if (is_palindrome(something)):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")

Output:

$ python user_input.py
Enter text: sir
No, it is not a palindrome

$ python user_input.py
Enter text: madam
Yes, it is a palindrome

$ python user_input.py
Enter text: racecar
Yes, it is a palindrome

How It Works:

We use the slicing feature to reverse the text. We've already seen how we can make slices from sequences using the seq[a:b] code starting from position a to position b. We can also provide a third argument that determines the step by which the slicing is done. The default step is 1 because of which it returns a continuous part of the text. Giving a negative step, i.e., -1 will return the text in reverse.

The input() function takes a string as argument and displays it to the user. Then it waits for the user to type something and press the return key. Once the user has entered, the input() function will then return that text.

We take that text and reverse it. If the original text and reversed text are equal, then the text is a palindrome.

Homework exercise:

Checking whether a text is a palindrome should also ignore punctuation, spaces and case. For example, "Rise to vote, sir." is also a palindrome but our current program doesn't say it is. Can you improve the above program to recognize this palindrome?
Files

You can open and use files for reading or writing by creating an object of the file class and using its read, readline or write methods appropriately to read from or write to the file. The ability to read or write to the file depends on the mode you have specified for the file opening. Then finally, when you are finished with the file, you call the close method to tell Python that we are done using the file.

Example:

#!/usr/bin/python
# Filename: using_file.py

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''

f = open('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file

f = open('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print(line, end='')
f.close() # close the file

Output:

$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!

How It Works:

First, open a file by using the built-in open function and specifying the name of the file and the mode in which we want to open the file. The mode can be a read mode ('r'), write mode ('w') or append mode ('a'). We can also by dealing with a text file ('t') or a binary file ('b'). There are actually many more modes available and help(open) will give you more details about them. By default, open() considers the file to be a 't'ext file and opens it in 'r'ead mode.

In our example, we first open the file in write text mode and use the write method of the file object to write to the file and then we finally close the file.

Next, we open the same file again for reading. We don't need to specify a mode because 'read text file' is the default mode. We read in each line of the file using the readline method in a loop. This method returns a complete line including the newline character at the end of the line. When an empty string is returned, it means that we have reached the end of the file and we 'break' out of the loop.

By deafult, the print() function prints the text as well as an automatic newline to the screen. We are suppressing the newline by specifying end='' because the line that is read from the file already ends with a newline character. Then, we finally close the file.

Now, check the contents of the poem.txt file to confirm that the program has indeed written and read from that file.
Pickle

Python provides a standard module called pickle using which you can store any Python object in a file and then get it back later. This is called storing the object persistently.

Example:

#!/usr/bin/python
# Filename: pickling.py

import pickle

# the name of the file where we will store the object
shoplistfile = 'shoplist.data'
# the list of things to buy
shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = open(shoplistfile, 'wb')
pickle.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # destroy the shoplist variable

# Read back from the storage
f = open(shoplistfile, 'rb')
storedlist = pickle.load(f) # load the object from the file
print(storedlist)

Output:

$ python pickling.py
['apple', 'mango', 'carrot']

How It Works:

To store an object in a file, we have to first open the file in 'w'rite 'b'inary mode and then call the dump function of the pickle module. This process is called pickling.

Next, we retrieve the object using the load function of the pickle module which returns the object. This process is called unpickling.
Summary

We have discussed various types of input/output and also file handling and using the pickle module.

Next, we will explore the concept of exceptions.

και η μετάφρασή του:
Κώδικας: Επιλογή όλων
ΕΙΣΟΔΟΣ-ΕΞΟΔΟΣ (Input-output)

Εισαγωγή
Θα υπάρξουν καταστάσεις όπου το πρόγραμμά σας πρέπει να αλλελεπιδράσει με το χρήστη. Για παράδειγμα, θα θέλετε να πάρετε είσοδο από το χρήστη και μετά να τυπώσετε πίσω μερικά αποτελέσματα. Μπορούμε να το επιτύχουμε χρησιμοποιώντας αντίστοιχα τις συναρτήσεις input() και print(). Για έξοδο μπορούμε να χρησιμοποιήσουμε τις διάφορες μεθόδους της κλάσης str (string). Για παράδειγμα μπορείτε να χρησιμοποιήσετε τη μέθοδο rjust για να πάρετε μια συμβολοσειρά, η οποία είναι δεξιά στοιχισμένη (right justified) σε ένα καθορισμένο εύρος. Κοιτάξτε τη help(str) για περισσότερες λεπτομέρειες. Ένας ακόμα συνηθισμένος τύπος εισόδου/εξόδου ασχολείται με τα αρχεία (files). Η ικανότητα να δημιουργείτε, διαβάζετε και να γράφετε αρχεία είναι βασική σε πολλά προγράμματα και θα εξερευνήσουμε αυτή την πτυχή σε αυτό το κεφάλαιο.

Είσοδος από το χρήστη
#!/usr/bin/python
# user_input.py

def reverse(text):
return text[::-1]

def is_palindrome(text):
return text == reverse(text)

something = input('Enter text: ')
if (is_palindrome(something)):
print("Yes, it is a palindrome")
else:
print("No, it is not a palindrome")

Έξοδος:
$ python user_input.py
Enter text: sir
No, it is not a palindrome

$ python user_input.py
Enter text: madam
Yes, it is a palindrome

$ python user_input.py
Enter text: racecar
Yes, it is a palindrome

Πως δουλεύει:
Χρησιμοποιούμε τον τεμαχισμό (κομμάτιασμα) για να αναστρέψουμε το κείμενο. Έχουμε ήδη δει πως μπορούμε να κάνουμε κομμάτια από ακολουθίες, χρησιμοποιώντας τον κώδικα seq[a:b], αρχίζοντας από τη θέση a μέχρι τη θέση b. Μπορούμε επίσης να δώσουμε ένα τρίτο όρισμα το οποίο προσδιορίζει το βήμα με το οποίο έγινε το κομμάτιασμα. Το προκαθορισμένο βήμα είναι το 1 εξαιτίας του οποίου επιστρέφει ένα συνεχές τμήμα του κειμένου. Δίνοντας ένα αρνητικό βήμα δηλ. -1, θα επιστρέψει το κείμενο ανάστροφα.
Η συνάρτηση input() παίρνει μια συμβολοσειρά σαν όρισμα και το παρουσιάζει στον χρήστη.Τότε περιμένει το χρήστη να τυπώσει κάτι και πιέζει το κλειδί επιστροφής (return key). Άπαξ και ο χρήστης έχει εισέλθει, η συνάρτηση input() τότε θα επιστρέψει αυτό το κείμενο. Παίρνουμε αυτό το κείμενο και το αναστρέφουμε. Εάν το αυθεντικό κείμενο και το ανεστραμμένο είναι ίσα, τότε το κείμενο είναι παλίνδρομο (palindrome).

Εργασία για το σπίτι:
Ελέγχοντας εάν ένα κείμενο είναι παλίνδρομο θα έπρεπε επίσης να αγνοεί τη στίξη, τα διαστήματα και τα κεφαλαία. Για παράδειγμα, το κείμενο "Rise to vote, sir" είναι παλίνδρομο, αλλά το τρέχον πρόγραμμά μας δεν το λέει. Μπορείτε να αναπτύξετε το ανωτέρο πρόγραμμα για να το αναγνωρίσει σαν παλίνδρομο;


Αρχεία (Files)
Μπορείτε να ανοίξετε και να χρησιμοποιήσετε αρχεία για διάβασμα ή γράψιμο, δημιουργώντας ένα αντικείμενο της τάξης file και χρησιμοποιώντας τις μεθόδους της, read, readline ή write κατάλληλα για να διαβάσει από ή να γράψει στο αρχείο. Η ικανότητα να διαβάζει ή να γράφει στο αρχείο εξαρτάται από τον τρόπο (mode) που έχετε καθορίσει για το άνοιγμα του αρχείου. Τότε τελικά, όταν τελειώσετε με το αρχείο, καλείτε τη μέθοδο close, να πει στην Python ότι είμαστε έτοιμοι με τη χρήση του αρχείου.

Παράδειγμα:
#!/usr/bin/python
# Filename: using_file.py

poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''

f = open('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file

f = open('poem.txt') # if no mode is specified, 'r'ead mode is assumed by default
while True:
line = f.readline()
if len(line) == 0: # Zero length indicates EOF
break
print(line, end='')
f.close() # close the file

Έξοδος:
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!

Πως δουλεύει:
Αρχικά ανοίγετε ένα αρχείο χρησιμοποιώντας την ενσωματωμένη συνάρτηση open καθορίζοντας την ονομασία του αρχείου και τον τρόπο με τον οποίο θέλουμε να ανοίγει το αρχείο. Ο τύπος μπορεί να είναι με διάβασμα ('r') (read mode), με γράψιμο ('w') (write mode) ή με προσάρτηση ('a') (append mode). Μπορούμε επίσης συσχετίζοντας ένα αρχείο κειμένου ('t') (text file) ή ένα δυαδικό αρχείο ('b') (binary file). Στην πραγματικότητα υπάρχουν πάρα πολλοί τύποι διαθέσιμοι και η help(open) θα σας δώσει περισσότερες λεπτομέρειες γι αυτούς. Προκαθορισμένα η open() θεωρεί το αρχείο ως αρχείο κειμένου ('t'ext file) και το ανοίγει με τον τύπο 'r'ead.
Στο δικό μας παράδειγμα, αρχικά ανοίγουμε το αρχείο σε 'write text mode' και χρησιμοποιούμε τη μέθοδο write του αντικειμένου του αρχείου, για να γράψουμε στο αρχείο και τότε τελικά κλείνουμε (close) το αρχείο. Κατόπιν ανοίγουμε το ίδιο αρχείο πάλι για διάβασμα (reading). Δεν χρειάζεται να καθορίσουμε έναν τύπο, γιατί ο 'read text file' είναι ο προκαθορισμένος τύπος. Διαβάζουμε σε κάθε γραμμή του αρχείου χρησιμοποιώντας τη μέθοδο readline σε βρόχο. Αυτή η μέθοδος επιστρέφει μια ολοκληρωμένη γραμμή περιλαμβάνοντας το χαρακτήρα νέας γραμμής (newline character) στο τέλος της γραμμής. Όταν μια άδεια συμβολοσειρά επιστρέφεται, σημαίνει ότι έχουμε φθάσει στο τέλος του αρχείου και ξεφεύγουμε (break) από το βρόχο. Προκαθορισμένα η συνάρτηση print() τυπώνει το κείμενο καθώς και μια αυτόματη νέα γραμμή (newline) στην οθόνη. Καταστέλλουμε τη νέα γραμμή καθορίζοντας end='', διότι η γραμμή που διαβάζεται από το αρχείο ήδη τελειώνει με ένα χαρακτήρα νέας γραμμής. Τότε, τελικά κλείνουμε (close) το αρχείο.
Τώρα, ελέγξτε τα περιεχόμενα του poem.txt αρχείου, για να επιβεβαιώσετε ότι το πρόγραμμα έχει πραγματικά γραφτεί και διαβαστεί από αυτό το αρχείο.

Pickle
Η Python παρέχει ένα κύριο άρθρθωμα που ονομάζεται pickle και χρησιμοποιώντας το μπορείτε να αποθηκεύετε κάθε αντικείμενο της Python σε ένα αρχείο και αργότερα να το παίρνετε πίσω. Αυτό ονομάζεται αποθήκευση του αντικειμένου επίμονα (persistently).

Παράδειγμα:
#!/usr/bin/python
# Filename: pickling.py

import pickle

# the name of the file where we will store the object
shoplistfile = 'shoplist.data'
# the list of things to buy
shoplist = ['apple', 'mango', 'carrot']

# Write to the file
f = open(shoplistfile, 'wb')
pickle.dump(shoplist, f) # dump the object to a file
f.close()

del shoplist # destroy the shoplist variable

# Read back from the storage
f = open(shoplistfile, 'rb')
storedlist = pickle.load(f) # load the object from the file
print(storedlist)

Έξοδος:
$ python pickling.py
['apple', 'mango', 'carrot']

Πως δουλεύει:
Για να αποθηκεύσουμε ένα αντικείμενο σε ένα αρχείο, πρέπει αρχικά να ανοίξουμε (open) το αρχείο σε 'w'rite 'b'inary mode και μετά καλούμε τη συνάρτηση dump του αρθρώματος pickle. Αυτή η διαδικασία ονομάζεται pickling. Κατόπιν, ανακτούμε το αντικείμενο χρησιμοποιώντας τη συνάρτηση load του αρθρώματος pickle, η οποία επιστρέφει το αντικείμενο. Αυτή η διαδικασία ονομάζεται unpickling.

Σύνοψη
Έχουμε συζητήσει διάφορους τύπους εισόδου/εξόδου, καθώς επίσης διαχείριση αρχείου και χρήση του αρθρώματος pickle. Κατόπιν θα εξερευνήσουμε την έννοια των εξαιρέσεων (exceptions).