Find the plant by the name passed in as an argument

Assignment Help Python Programming
Reference no: EM132330154

Question : Parsing and Manipulating XML Data

XML is the basis for many interfaces and web services. Consequently, reading and manipulating XML data is a common task in software development.

Description

An online plant distributor has recently experience a shortage in its supply of Anemone plants such that the price has increased by 20%. Their plant catalog is maintained in an XML file and they need a Python utility to find the plant by name, read the current price, change it by the specified percentage, and update the file. Writing this utility is your assignment.

Using Python's ElementTree XML API, create a Python program to perform the following tasks below. Note that your program's execution syntax must be as follows:

python xmlparse.py plant_catalog.xml plantName percentChange

1. Using ElementTree, read in this assignments XML file plant_catalog.xml specified by a command line parameter as shown above.

2. Find the plant by the name passed in as an argument on the command line (plantName above).

3. Once found, read the current price and adjust it by the command line argument percentChange. Note that this value could be anything in the range of -90 < percentChange < 100.

For example, if you run your script as follows:

python plant_catalog.xml "Greek Valerian" -20

with the original XML containing:

<PLANT>

<COMMON>Greek Valerian</COMMON>

<BOTANICAL>Polemonium caeruleum</BOTANICAL>

<ZONE>Annual</ZONE>

<LIGHT>Shade</LIGHT>

<PRICE>4.36</PRICE>

<AVAILABILITY>071499</AVAILABILITY>

</PLANT>

The resulting file should contain:

<PLANT>

<COMMON>Greek Valerian</COMMON>

<BOTANICAL>Polemonium caeruleum</BOTANICAL>

<ZONE>Annual</ZONE>

<LIGHT>Shade</LIGHT>

<PRICE>3.48</PRICE>

<AVAILABILITY>071499</AVAILABILITY>

</PLANT>

Note: You may reduce the precision of the calculation if you wish but it isn't required.

Hints

Since XML is just a text file, you could write the code to read all the data and the decode the XML information. However, I certainly don't recommend this approach. Instead, let Python do it for you! Using Python's ElementTree module, parse the file into an "in-memory" representation of the XML data. Once parsed, the root (or starting place) is as simple as requesting it from the tree.

Once you have the root, you can call methods to find what you are looking for and modify them appropriately. You'll want to "findall" the plants and, "for" each plant "in" the result, you'll want to compare the name with the name passed on the command line. If you find a match you'll apply the percentage change, save the result back to the tree.

When you are done with the search you will "write" the tree back to a file. I suggest using a different file name or you will be having to re-download the original with each run.

One note of caution, be sure to read about XML in the Distributed Systems text. From doing so and reviewing the data file you will not that there are no attributes in the XML file. Consequently, you do not need to use attribute methods when you attempt this assignment.

The following code snippet will give you a good starting point:

# Calling arguments: plant_catalog.xml plantName percentChange

import xml.etree.ElementTree as ET

import sys

# input parameters

searchName = sys.argv[2]

percent = float(sys.argv[3])

# parse XML data file

tree = ET.parse(sys.argv[1])

root = tree.getroot()

Reference no: EM132330154

Questions Cloud

Write an overview of the corrupt practices of the police : Provide a 1-page executive summary or overview of the corrupt practices of the police and correctional systems. The best time to write an executive summary.
Find the sample program illustrating the split method : Plan program carefully on paper before beginning code. You may find the sample program illustrating the 'split' method to be a useful reference.
Describe the contemporary theoretical frameworks : OGBH201 Organisational Behaviour - Group Oral Presentation and a Group Report Assignment, Kent Institute Australia. Describe contemporary theoretical frameworks
Explain what the phrase means to you personally : Watch the 10-minute video: Understanding the Declaration of Independence - 9 Key Concepts Everyone Should Know (By cmseibert) and discussing in more detail.
Find the plant by the name passed in as an argument : An online plant distributor has recently experience a shortage in its supply of Anemone plants such that the price has increased by 20%.
Under what circumstances might each be employed : According to the text, what four broad categories of criminal defenses does our legal system recognize? Under what circumstances might each be employed?
Identifying and evaluating the constitutional safeguards : Write a 1,400- to 1,750-word paper identifying and evaluating the constitutional safeguards provided by the 4th, 5th, and 6th Amendments to the United States.
Is it ethical for prisons to focus on profit : Is it ethical for prisons to focus on profit? Why or why not? Do you support the phasing out of corporate-run prisons by the Justice Department? Why or why not?
How should we balance the need to collaborate : How should we balance the need to collaborate and cooperate for the benefit of all with the need to maintain maximum advantage (if however slight) over.

Reviews

Write a Review

Python Programming Questions & Answers

  Write a python program to implement the diff command

Without using the system() function to call any bash commands, write a python program that will implement a simple version of the diff command.

  Write a program for checking a circle

Write a program for checking a circle program must either print "is a circle: YES" or "is a circle: NO", appropriately.

  Prepare a python program

Prepare a Python program which evaluates how many stuck numbers there are in a range of integers. The range will be input as two command-line arguments.

  Python atm program to enter account number

Write a simple Python ATM program. Ask user to enter their account number, and print their initail balance. (Just make one up). Ask them if they wish to make deposit or withdrawal.

  Python function to calculate two roots

Write a Python function main() to calculate two roots. You must input a,b and c from keyboard, and then print two roots. Suppose the discriminant D= b2-4ac is positive.

  Design program that asks user to enter amount in python

IN Python Design a program that asks the user to enter the amount that he or she has budget in a month. A loop should then prompt the user to enter his or her expenses for the month.

  Write python program which imports three dictionaries

Write a Python program called hours.py which imports three dictionaries, and uses the data in them to calculate how many hours each person has spent in the lab.

  Write python program to create factors of numbers

Write down a python program which takes two numbers and creates the factors of both numbers and displays the greatest common factor.

  Email spam filter

Analyze the emails and predict whether the mail is a spam or not a spam - Create a training file and copy the text of several mails and spams in to it And create a test set identical to the training set but with different examples.

  Improve the readability and structural design of the code

Improve the readability and structural design of the code by improving the function names, variables, and loops, as well as whitespace. Move functions close to related functions or blocks of code related to your organised code.

  Create a simple and responsive gui

Please use primarily PHP or Python to solve the exercise and create a simple and responsive GUI, using HTML, CSS and JavaScript.Do not use a database.

  The program is to print the time

The program is to print the time in seconds that the iterative version takes, the time in seconds that the recursive version takes, and the difference between the times.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd