FileTransform.py
in which you will write two
functions. You can test these functions before testing the pig-latin
and rot13 functions you will write in Transforms.py
that
are described below. You do not need to edit any of the other given Python files.
You can run the program and it will print the contents of a file to the Eclipse Console window. You will need to transform the data read from a file and print to a file rather than to the Console. Then you will need to write new transforms for pig-latin and rot13. These are described after the Getting Started section below.
Please note when editing the files we provide that you do not remove
the comments we provide. In particular the GUI menu looks for a comment right
after the def statement
in the functions in Transforms.py
and if you remove that
comment
the program will not run.
It is ok to remove a comment that is beside code you need to replace.
FileTransform.py
which makes
calls to code in Transforms.py
as described
below.
The main function transform_file
will do three tasks by
calling functions: you will write code that helps the second
and third of these tasks.
Each task is described below:
Use this dialog box to choose a file to transform. The data
directory
that comes with this assignment has the files shown above in it, although you can
choose to transform any text file you have.
You will need to create and add a simple.txt datafile that has just a few lines of code. You may find that it is the most helpful in debugging your program.
You can also create additional files to test
your program. You should not run it only on large files! Create a file
to put in the data folder, by clicking on the data folder to highlight it,
then select New>Other>General>Untitled Text
File
.
The way we have setup this program, when your program reads the file it
creates a list of the lines in the file. Each line is represented by a list
of words on that line. This list of lists of strings is returned by the
function get_words
which is completely written for you. The
code then prompts the user for a transform by displaying a menu like the
one below:
The chosen transform should be applied to each word in the file being
transformed. This means you must write code in the function
transform
to create a new list of lists, with each word in the parameter
words
being transformed by applying func
. For example, if
the parameter words
is the two lines from a file
represented by
the two lists below:
[ ["This", "is", "the", "story"], ["of", "a", "streetcar", "named", "Desire"]]
Then if the parameter func
represents pig-latin, you'll
write code to return this list:
[ ["is-Thay", "is-way", "e-thay", "ory-stay"], ["of-way", "a-way", "eetcar-stray", "amed-nay", "esire-Day"]]
The code you're given in transform
returns a copy of the
list of words with just the first word transformed as an example. You should modify it to return a copy in which every word is transformed.
After the data from the file has been transformed, a new transformed file should be written. The file dialog below asks the user for a filename to save the transformed words in. Note, by default, it chooses to save files in the same data folder as the original files so use different names (like shown below) when saving your transforms to avoid overwriting the original data files.
The program then calls write_words
with a file open for
writing
and a list of transformed data/words. You must complete
write_words
so that it writes the transformed data to a file.
Whenever you print something to the console, you should also write it to
the file open for output. You do that with the write
method for file
objects which takes a string as a parameter,
two uses shown below:
outfile.write(word) # to output any string outfile.write("\n") # to create a line, write the newline character
To make sure the output file is completely written, the last line of your code must close the file as below:
outfile.close()
This will ensure that all writing to the file happens, that the file is flushed and closed properly.
You will need to test both
the code in FileTransform.py
that reads and writes a
file and the code in Transforms.py
that does the
transforms.
Once you write a file, you may have to right click on the data folder and refresh in eclipse to see the new file.
There is more detail on writing to files at the bottom of this page.
Modify the Python module named
Transforms.py
. In this module, start by writing the functions transform_pigify
and
transform_unpigify
that each have a single string parameter
and return a string that is either the pig-latin equivalent
or that is reversed from pig-latin to normal text, respectively. You are given two transform functions, transform_identity
and transform_uppercase
, that serve as simple examples.
Word | pig-latin Equivalent |
---|---|
anchor | anchor-way |
elegant | elegant-way |
oasis | oasis-way |
isthmus | isthmus-way |
only | only-way |
Word | pig-latin Equivalent |
---|---|
computer | omputer-cay |
slander | ander-slay |
spa | a-spay |
pray | ay-pray |
yesterday | esterday-yay |
strident | ident-stray |
rhythm | ythm-rhay |
Word | pig-latin Equivalent |
---|---|
quiet | iet-quay |
queue | eue-quay |
quay | ay-quay |
A few words will not conform to these rules, but the rules should always be used. If a word contains no vowels it should be treated as though it starts with a vowel. For example "zzz" will be translated to "zzz-way".
It is possible that different words will be transformed to the same
pig-latin form. For example, "it" is "it-way", but "wit" is also
"it-way" using the rules above.
ROT13
Write a function named transform_rot13
to use a
ROT13 cipher to
encode/decode a string, and then use this to encode
every word in a file. The function transform_rot13
returns a rotated form
of its string parameter:
To convert a letter character to its ROT13 equivalent we suggest using
these strings, the find
method for String
that returns an
index, and the string indexing operator.
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" b = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"For example, the letter 'S' in the first string (labeled
a
above) is found at index 18. Note that
a.find("S")
will evaluate to 18. Then
b[18]
represents the encoding for 'S', namely 'F'. You
can reverse the roles of the strings a
and
b
since the ROT13 cipher is symmetric.
There are other ways to do the ROT13 cipher using the functions
chr
, ord
and the % operator, but the
approach suggested by using indexing and the strings above
is much easier to get working.
You can identify non-letters by using the return value of the string
find
method which will be -1 for non-letters. Alternatively
you can use the Python
string
module to identify letters. For example, the
code below generates the output that follows it.
1 2 3 ! , #
Note that you must import the string
module to use its
functions and constants,
see the Python string docs for full information on the module.
write_words
writes to the
console only, but it takes a file
parameter that you'll
use when you modify the function. This is the code you're given:
words
,
which is one line of the transformed file, has been written
completely to the console.
You must write the words to a file as well as to the console.
To write to a file, you use the file
.write
method which takes a string as a parameter,
two uses are shown below for a variable named outfile
:
This means that completing write_words
requires mirroring
the print
calls to also write to a file using
file.write
as follows:
file.write(word+" ")
or by using one call to write
to write the word to a file
and another call to write the space.
file.write("\n")
so that a newline is started in
the file just as it is on the console.
file.close
when everything has been written.