$24
Introduction
Lab 6 is our final Python programming project and a fun one called filemaker . Your filemaker script is to create a file containing a specified number of records based on a record layout. The record layout consists of constants and verbs that when read and executed by your Python script, will cause a file to be created that matches that format.
Requirements
Your Python script shall be named filemaker and be marked executable.
Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT
The INPUTCOMMANDFILE contains one command per line in the file. Your filemaker script shall support the following commands in the INPUTCOMMANDFILE:
HEADER "string"
writes "string" once at the top of the file
STRING "string"
writes the constant "string" to each record in the output
FILEWORD label "filename"
write a random word from file "filename"
NUMBER label min max
write a random number between min and max inclusive
REFER "label"
write the item referred to by LABEL (see notes below)
The label identifier you see above is a string that is unique in this file from all other labels and is a way to reference something created earlier. For example, consider the following sample command file we will call commandfile1 :
COMMAND
EXPLANATION
STRING '"'
Output a double quote (notice that it is a single quote
followed by a double quote followed by a single quote)
FILEWORD lastname "surnames.txt"
Output a random word from file "surnames.txt",
labeled lastname
STRING ", "
Output a comma followed by a space
FILEWORD firstname "firstnames.txt"
Output a random word from file "firstnames.txt",
labeled firstname
STRING '","'
Output a double quote, comma and a double qoute
REFER firstname
Output the same random word in this record as the
command labeled firstname
STRING "."
Output a period
REFER lastname
Output the same random word in this record as the
command labeled lastname
STRING '@mail.weber.edu"\n'
Output @mail.weber.edu followed by a double quote
and a newline
If you were to execute this command:
./filemaker commandfile1 outputfile.txt 3
Your script should produce output like this:
"Cowan, Ted","Ted.Cowan@mail.weber.edu"
"Mouse, Mickey","Mickey.Mouse@mail.weber.edu"
"Duck, Donald","Donald.Duck@mail.weber.edu"
Helpful Hints
Suggested logic:
Check for errors
Issue an Usage message and exit(1) if the user did not specify all three parameters
Issue an appropriate Error message and exit(1) if an error occurs opening any of the files.
Preprocess headers and files: Do this only once. For each line in the commandfile:
CS 3030 Cowan 01-07-2019 07:27 PM
2
Write any HEADER strings immediately to the output file
For FILEWORD commands, "slurp" the entire file as a single list into a dictionary called randomFiles using the name of the file as the key
Append all commands except HEADER to a list called commands for later execution
Do the following steps for each record to be created (the bolded steps are described in detail below):
Delete everything in dictionary randomData
Generate random data and populate dictionary randomData using random number generators and file data in dictionary randomFiles
Generate output from commands and dictionary randomData
Generate random data: For each line in the the commands list, generate all random data into dictionary randomData . For each command:
If the first word is REFER or STRING , skip it (we will deal with them in the next pass)
The first word is a label. Using the label as a key, look in the randomData dictionary for that label. If it exists already, it is a duplicate, which is an error: issue a message and exit(1)
Obtain the random data ( FILEWORD or NUMBER ) and store with the label as the key in dictionary randomData:
FILEWORD : use a random number as a subscript to the dictionary of words from the file. The name of the file is the key to the list of words in dictionary randomFiles . Use 0 as the min and len(randomFiles[label])-1 as the max
NUMBER : generate a random number using random.randint(min,max)
Generate output - for each command in the commands list:
STRING : write the string to the output file
RANDOM : write to the output file the random number stored in randomData using the label as the key
FILEWORD : write to the file the random word stored in randomData using the label as the key
REFER : write to the file the random data stored in randomData using the label as the key
Clone your private repo on github.com
In the assignment module in Canvas, find the link to the Canvas page entitled "Clone your Lab 6 Repo", click on it and follow the instructions.
Copy your private repo down to icarus
CS 3030 Cowan 01-07-2019 07:27 PM 3
BASH
git clone https://github.com/cowancs3030spring19/lab6-YOURGITHUBUSERNAME
cd lab6-YOURGITHUBUSERNAME
Write and test filemaker
Fire up your favorite text editor, and update the header:
#!/usr/bin/python
TEXT
(Your name)
Lab 6 - Filemaker
CS 3030 - Scripting Languages
(add your mind-blowingly kewl code here)
Test files provided for your convenience
When manually testing your script, the following files are available for you to use in testing in folder /var/classes/cs3030/lab6 :
firstnames.txt a file of first names, one per line
surnames.txt a file of surnames (last names), one per line
lab6cmds the command file used in the example above
lab6cmds2 the command file above plus HEADER and NUMBER
Run cucumber to check your progress
./cucumber -s
cucumber randomly generates testfiles so you will want to run cucumber many, many times to verify your script’s operation.
Submit your assignment code for grading
Remember, you must push your script in your private repo to github.com to receive any points, for all assignments in this course.
BASH
git add filemaker
git commit -m"COMMIT MESSAGE"
git push origin master
CS 3030 Cowan 01-07-2019 07:27 PM 4
For this lab you will have created the following executable files:
filemaker
Grading
Here is how you earn points for this assignment:
CS 3030 Cowan 01-07-2019 07:27 PM 5
FEATURES
POINTS
Must-Have Features
Script is named correctly and found in its proper place in your private repo
5
Script is executable
5
Required Features
Script prints a “Usage:” statement and exits rc=1 if any of the commandline parameters
10
are missing
Script prints an error message containing the word “Error” and exits rc=1 if the
10
INPUTCOMMANDFILE file cannot be opened
Script prints an error message containing the word “Error” and exits rc=1 if the
10
OUTPUTFILE file cannot be opened
Script prints an error message containing the word “Error” and exits rc=1 if the COUNT
10
is negative or not a number
Script exits rc=0 on successful completion
10
Script outputs the requested number of records
30
Script supports HEADER
30
Script supports STRING
40
Script supports FILEWORD
20
Script supports FILEWORD with true randomness
20
Script supports NUMBER
20
Script supports NUMBER with true randomness
20
Script supports REFER to a label on a FILEWORD command
30
Script supports REFER to a label on a NUMBER command
30
Grand Total
300