Question

In: Computer Science

Consider the list of animals and their attributes given below cell (CELL_Q3_INPUT). The objective is to...

Consider the list of animals and their attributes given below cell (CELL_Q3_INPUT). The objective is to obtain the print outs in the subsequent cell (CELL_Q3_OUTPUT). Note that the attributes of each animal are 'name', 'species', 'color', and 'age'. You must use collections.namedtuple to add readable attribute references, which then will be used to generate the desired output in CELL_Q3_OUTPUT.

# CELL_Q3_INPUT

lassie = ('Lassie', 'dog', 'black', 12)

buddy = ('Buddy', 'pupper', 'red', 0.5)  

astro = ('Astro', 'doggo', 'grey', 15)

mrpb = ('Mr. Peanutbutter', 'dog', 'golden', 35)

bojack = ('BoJack Horseman', 'horse', 'brown', 52)

pc = ('Princess Carolyn', 'cat', 'pink', 34)

tinkles = ('Mr. Tinkles', 'cat', 'white', 7)

pupper = ('Bella', 'pupper', 'brown', 0.5)

doggo = ('Max', 'doggo', 'brown', 5)

seuss = ('The Cat in the Hat', 'cat', 'stripey', 27)

pluto = ('Pluto (Disney)', 'dog', 'orange', 3)

plu2o = ('Pluto (space)', 'planet', 'brownish', 4500000000)

yertle = ('Yertle', 'turtle', 'green', 130)

horton = ('Horton', 'elephant', 'blue', 79)

# CELL_Q3_OUTPUT

# Lassie is an old black dog who is 12 years old.

# Buddy is a young red pupper who is 0.5 years old.

# Astro is an old grey doggo who is 15 years old.

# Mr. Peanutbutter is an old golden dog who is 35 years old.

# BoJack Horseman is a 52-year-old non-canine brown horse.

# Princess Carolyn is a 34-year-old non-canine pink cat.

# Mr. Tinkles is a 7-year-old non-canine white cat.

# Bella is a young brown pupper who is 0.5 years old.

# Max is a young brown doggo who is 5 years old.

# The Cat in the Hat is a 27-year-old non-canine stripey cat.

# Pluto (Disney) is a young orange dog who is 3 years old.

# Pluto (space) is a 4500000000-year-old non-canine brownish planet.

# Yertle is a 130-year-old non-canine green turtle.

# Horton is a 79-year-old non-canine blue elephant.

Solutions

Expert Solution

Pre requisites:

install the required packages

python -m pip install xlwt

python -m pip install xlrd

python -m pip install collections

Explanation : The program will read an excel file and then process all cells under a column. The values in the cells are evaluated directly in the python environment thereby autmatically creating variables like lassie in the python running env. The exec command is used for this. Now we make the necessary changes and write to the sheet. In order to preserve the content of input values we need to copy the content of input cell values and paste in the new sheet. Then we will overwrite the existing sheet.

import xlwt 
from xlwt import Workbook 
import xlrd
from collections import namedtuple
# Create a named tuple called Animal 
Animal = namedtuple('Animal',['name','species','color','age'])
# Open the work book for reading
workbook = xlrd.open_workbook("input.xlsx")
readSheet = workbook.sheet_by_index(0)
rows = []
# Execute each line in the excel file which will create variables in python env
for i in range(readSheet.nrows):
        exec(readSheet.cell(i, 0).value);
# Now we have all the variables having the tuples. Convert the tuple to Animal tuple.
lassie = Animal(*lassie)
buddy = Animal(*buddy)
astro = Animal(*astro)
mrpb = Animal(*mrpb)
bojack = Animal(*bojack)
pc = Animal(*pc)
tinkles = Animal(*tinkles)
pupper = Animal(*pupper)
doggo = Animal(*doggo)
seuss = Animal(*seuss)
pluto = Animal(*pluto)
plu2o = Animal(*plu2o) 
yertle = Animal(*yertle) 
horton = Animal(*horton) 

# Create the workbook for writing the result
wb = Workbook() 
  
# add_sheet is used to create sheet. 
writeSheet = wb.add_sheet('Sheet1',cell_overwrite_ok=True)
# Write the input cell values to the new sheet  
for i in range(readSheet.nrows):
        writeSheet.write(i, 0, readSheet.cell(i, 0).value)
# Write each output to the next column in the sheet
lassie=lassie.name +' is an old '+ lassie.color + ' ' +  lassie.species +  ' who is ' +  str(lassie.age) + ' years old.'
buddy=buddy.name + ' is a young ' + buddy.color + ' ' + buddy.species + ' who is '+ str(buddy.age) + ' years old.'
astro=astro.name + ' is an old ' + astro.color + ' ' + astro.species + ' who is ' + str(astro.age) + ' years old.'
mrpb=mrpb.name + ' is an old ' + mrpb.color + ' ' +  mrpb.species + 'who is' + str(mrpb.age) + 'years old.'
bojack=bojack.name + ' is a ' + str(bojack.age) + '-year-old non-canine' + bojack.color + ' ' + bojack.species + '.'
pc=pc.name + ' is a ' + str(pc.age) + '-year-old non-canine ' + pc.color + ' ' +  pc.species + '.'
tinkles=tinkles.name + ' is a ' + str(tinkles.age) + '-year-old non-canine ' + tinkles.color + ' ' + tinkles.species + '.'
pupper= pupper.name + ' is a young ' + pupper.color + ' ' + pupper.species + ' who is '+ str(pupper.age) + ' years old.'
doggo=doggo.name + ' is a young ' + doggo.color + ' ' + doggo.species + ' who is '+ str(doggo.age) + ' years old.'
seuss=seuss.name + ' is a ' + str(seuss.age) + '-year-old non-canine' + seuss.color + ' ' + seuss.species + '.'
pluto=pluto.name + ' is a young ' + pluto.color + ' ' + pluto.species + ' who is '+ str(pluto.age) + ' years old.'
plu2o=plu2o.name + ' is a ' + str(plu2o.age) + '-year-old non-canine' + plu2o.color + ' ' + plu2o.species + '.'
yertle=yertle.name + ' is a ' + str(yertle.age) + '-year-old non-canine ' + yertle.color + ' ' + yertle.species + '.'
horton=horton.name + ' is a ' + str(horton.age) + '-year-old non-canine ' + horton.color + ' ' + horton.species + '.'
writeSheet.write(0, 1, lassie) 
writeSheet.write(1, 1, buddy)
writeSheet.write(2, 1, astro)
writeSheet.write(3, 1, mrpb)
writeSheet.write(4, 1, bojack)
writeSheet.write(5, 1, pc)
writeSheet.write(6, 1, tinkles)
writeSheet.write(7, 1, pupper)
writeSheet.write(8, 1, doggo)
writeSheet.write(9, 1, seuss)
writeSheet.write(10, 1, pluto)
writeSheet.write(11, 1, plu2o)
writeSheet.write(12, 1, yertle)
writeSheet.write(13, 1, horton)

# Overwrite the input sheet
wb.save('input.xls') 

The input cell

lassie = ('Lassie', 'dog', 'black', 12)
buddy = ('Buddy', 'pupper', 'red', 0.5)
astro = ('Astro', 'doggo', 'grey', 15)
mrpb = ('Mr. Peanutbutter', 'dog', 'golden', 35)
bojack = ('BoJack Horseman', 'horse', 'brown', 52)
pc = ('Princess Carolyn', 'cat', 'pink', 34)
tinkles = ('Mr. Tinkles', 'cat', 'white', 7)
pupper = ('Bella', 'pupper', 'brown', 0.5)
doggo = ('Max', 'doggo', 'brown', 5)
seuss = ('The Cat in the Hat', 'cat', 'stripey', 27)
pluto = ('Pluto (Disney)', 'dog', 'orange', 3)
plu2o = ('Pluto (space)', 'planet', 'brownish', 4500000000)
yertle = ('Yertle', 'turtle', 'green', 130)
horton = ('Horton', 'elephant', 'blue', 79)

The output


Related Solutions

Compare and contrast the extracellular matrix of animals and the cell wall of plants. Consider their...
Compare and contrast the extracellular matrix of animals and the cell wall of plants. Consider their functions and the types of molecules that make up these structures. What properties of these molecules help to fulfill these functions?
Consider the table given below for a fixed memory allocation scheme: Given the jobs list information...
Consider the table given below for a fixed memory allocation scheme: Given the jobs list information and memory information in the table below, answer the questions that follow: Job Number Memory Requested                         Memory Block Memory Block Size Job 1 600 Block 1 1200 Job 2 920 Block 2 300 Job 3 1100 Block 3 930 Job 4 700 Block 4 950             (Assume all jobs are in a waiting queue in the order given, also assume that the memory...
Given the following list of classes, attributes and methods, - identify which items are classes, which...
Given the following list of classes, attributes and methods, - identify which items are classes, which items are attributes and which items are methods; - identify which class each attribute and method belongs to; and - suggest a class hierarchy given your list of classes. *Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes. LandOnStatue, NumberOfLegs, Height, ShoeSize, Eat, Animal, Speak, WingSpan, Age, Peck, Sleep, Horse, LengthOfMane, Move, BeakLength, LengthOfTail,...
Consider the electrochemical cell described below: What is the cell potential (Ecell) for the voltaic cell...
Consider the electrochemical cell described below: What is the cell potential (Ecell) for the voltaic cell described below if the. Ag(s) çAg2CrO4 (Sat. Aq. Soln) çç Ag+ (0.125 M) ç Ag(s) a. What is the concentration of [Ag+] in the anode of the cell described above, if the Ksp of Silver (I) chromate is 1.1 x 10-12 ? b. What is the Ecell? c. Draw an overall cell diagram for this cell. Remember to label the cathode, anode, and electron...
Consider a relation R with five attributes ABCDE. You are given the following dependencies: A à...
Consider a relation R with five attributes ABCDE. You are given the following dependencies: A à B, BC à E, and ED à A. (1) List all candidate keys for R. Please show your steps. (4 points) (2) Is R in 3NF? Please explain your answer. (3 Points) (3) Is R in BCNF? Please explain your answer. (3 Points)
Given below is a list of costs: Direct labor                                   &
Given below is a list of costs: Direct labor                                                                 35,500 Accounting Dept. salaries                                          70,000 Indirect materials                                                        32,700 Depreciation, factory                                                  15,000 Direct materials                                                          66,000 Sales commission                                                       36,000 Indirect labor                                                              20,200 Advertising                                                                 5,800 Water and electricity of the office                              12,000 Required, From the above costs calculate the following costs (show your calculation): Total product costs Total period costs Total prime costs Total conversion costs
What is pricing? List the objective of pricing and when setting prices, the company must consider...
What is pricing? List the objective of pricing and when setting prices, the company must consider its external environment. Describe four parts of the external environment and how they affect businesses.
Make a list of the attributes or desirable features (such as the relevance, for example) that...
Make a list of the attributes or desirable features (such as the relevance, for example) that must have financial information to make it useful. Explain each of them.
Objective: Practice common UNIX commands. Procedure: The following list of Unix commands are given for self-learning....
Objective: Practice common UNIX commands. Procedure: The following list of Unix commands are given for self-learning. Use 'whatis' or 'man' command to find out about each command. Your document should include the description or screen shots of the output from each of the command. Commands: df du gzip file history wget Changing access rights: chmod u+x Dir1.0             adds execute permission for the owner chmod go-w file1               removes write permission for the group and others chmod ugo=rw testfile      sets...
1. The two structures for cell-cell attachment in animals are: plasmodesmata, desmosomes plasmodesmata, gap junctions tight...
1. The two structures for cell-cell attachment in animals are: plasmodesmata, desmosomes plasmodesmata, gap junctions tight junctions, desmosomes gap junctions, desmosomes 2. The correct order of the 4 consecutive steps of ALL intercellular signaling pathways is Step 1) reception, Step 2) response, Step 3) transduction, Step 4) deactivation Step 1) reception, Step 2) processing, Step 3) response, Step 4) deactivation Step 1) reception, Step 2) processing, Step 3) response, Step 4) deactivation Step 1) reception, Step 2) response, Step 3)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT