Python Programming Homework Help
Python Programming Homework Help
PYTHON PROGRAMMING
Introduction
You will use object-oriented programming (classes and inheritance) to
build a program to monitor news feeds over the Internet. Your
program will filter the news, alerting the user when it notices a news
story that matches that user's interests (for example, the user may be
interested in a notification whenever a story related to the Red Sox is
posted).
RSS stands for " Really Simple Syndication." An RSS feed consists
of (periodically changing) data stored in an XML-format file
residing on a web-server. For this problem set, the details are
unimportant. You don't need to know what XML is, nor do you
need to know how to access these files over the network. We
have taken care of retrieving and parsing the XML file for you.
First, let's talk about one specific RSS feed: Google News. The URL
for the Google News feed is: http://news.google.com/?output=rss
If you try to load this URL in your browser, you'll probably see
your browser's interpretation of the XML code generated by the
feed. You can view the XML source with your browser's "View
Page Source" function, though it probably will not make much
sense to you. Abstractly, whenever you connect to the Google
News RSS feed, you receive a list of items . Each entry in this list
represents a single news item . In a Google News feed, every
entry has the following fields:
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
●guid : A globally unique identifier for this news story.
●title : The news story's headline.
●description : A paragraph or so summarizing the news story.
●link : A link to a website with the entire story.
●pubDate : Date the news was published
●category : News category, such as “Top Stories”
Problem 1
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
●globally unique identifier (GUID) - a string
●title - a string
●description - a string
●link to more content - a string
●pubdate - a datetime
●get_guid(self)
●get_title(self)
●get_description(self)
●get_link(self)
●get_pubdate(self)
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Parsing the Feed
Parsing is the process of turning a data stream into a structured
format that is more convenient to work with. We have provided
you with code that will retrieve and parse the Google and Yahoo
news feeds.
Triggers
Given a set of news stories, your program will generate alerts for
a subset of those stories. Stories with alerts will be displayed to
the user, and the other stories will be silently discarded. We will
represent alerting rules as triggers . A trigger is a rule that is
evaluated over a single news story and may fire to generate an
alert. For example, a simple trigger could fire for every news story
whose title contained the phrase "Microsoft Office". Another
trigger may be set up to fire for all news stories where the
description contained the phrase "Boston". Finally, a more specific
trigger could be set up to fire only when a news story contained
both the phrases "Microsoft Office" and "Boston" in the
description.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Trigger Interface
Each trigger class you define should implement the following
interface, either directly or transitively. It must implement the
evaluate method that takes a news item ( NewsStory object)as an
input and returns True if an alert should be generated for that
item. We will not directly use the implementation of the Trigger
class, which is why it raises an exception should anyone attempt
to use it.
The class below implements the Trigger interface (you will not
modify this). Any subclass that inherits from it will have an
evaluate method. By default, they will use the evaluate method
in Trigger, the superclass, unless they define their own evaluate
function, which would then beused instead. If some subclass
neglects to define its own evaluate() method, calls to it willgo to
Trigger.evaluate(), which fails (albeit cleanly) with the
NotImplementedError:
class Trigger(object):
def evaluate(self, story):
""“
Returns True if an alert should be generated
for the given news item, or False otherwise.
""“
raise NotImplementedError
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
We will define a number of classes that inherit from Trigger. In the
figure below,
Trigger is asuperclass, from which all other classes inherit. The
arrow from PhraseTrigger to Triggermeans that PhraseTrigger
inherits from Trigger - a PhraseTriggeris a Trigger .Note that other
classes inherit from PhraseTrigger .
Phrase Triggers
Having a trigger that always fires isn't interesting; let's write
some that are interesting! A user may want to be alerted about
news items that contain specific phrases. For instance, a simple
trigger could fire for every news item whose title contains the
phrase "Microsoft Office". In the following problems, you will
create a phrase trigger abstract class and implement two classes
that implement this phrase trigger.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
A phrase is one or more words separated by a single space
between the words.
You may assume that a phrase does not contain any punctuation.
Here are some examples of valid phrases:
●'purple cow‘
●'PURPLE COW‘
●'mOoOoOoO‘
●'this is a phrase'
Given some text, the trigger should fire only when each word in
the phrase is present in its entirety and appears consecutively in
the text, separated only by spaces or punctuation. The trigger
should not be case sensitive. For example, a phrase trigger with
the phrase "purple cow" should fire on the following text
snippets:
●'PURPLE COW‘
●'The purple cow is soft and cuddly.‘
●'The farmer owns a really PURPLE cow.‘
●'Purple!!! Cow!!!‘
●'purple@#$%cow‘
●'Did you see a purple cow?'
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
But it should not fire on these text snippets:
●'Purple cows are cool!‘
●'The purple blob over there is a cow.‘
●'How now brown cow.‘
●'Cow!!! Purple!!!‘
●'purplecowpurplecowpurplecow'
Play around with this a bit to get comfortable with what it is. The
split
,methods of strings will almost certainly be helpful as you tackle
this part.
You may also find the string methods lower and/or upper useful
for this problem., replace , join
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Problem 2
Implement a phrase trigger abstract class, PhraseTrigger . It
should take in a string phraseas an argument to the class's
constructor. This trigger should not be case-sensitive (it should
treat "Intel" and "intel" as being equal).
Problem 3
If you find that you're not passing the unit tests, keep in mind
that FAIL means your code runs but produces the wrong answer,
whereas ERROR means that your code crashes due to some error.
Problem 4
Time Triggers
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Please check the earlier diagram ifyou’re confused about the
inheritance structure
of the Triggers in this problem set.
Problem 5
Problem 6
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Composite Triggers
So the triggers above are mildly interesting, but we want to do
better: we want to 'compose' the earlier triggers to set up more
powerful alert rules. For instance, we may want to raise an alert
only when both "google glass" and "stock" were present in the
news item (an idea wecan't express with just phrase triggers).
Note that these triggers are not phrase triggers and should not
be subclasses of PhraseTrigger . Again, please refer back to the
earlier diagram if you’re confused about theinheritance structure
of Trigger.
Problem 7
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Problem 8
Problem 9
Filtering
At this point, you can run ps5.py, and it will fetch and display
Google and Yahoo news items for in a pop-up window. How
many news items? All of them.
Right now, the code we've given you in ps5.py gets the news
from both feeds every minute and displays the result. This is
nice, but, remember, the goal here was to filter out only the the
stories we wanted.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Problem 10
After completing Problem 10, you can try running ps5.py, and
various RSS news items should pop up, filtered by some hard-
coded triggers defined for you in code near the bottom. You may
need to change these triggers to reflect what is currently in the
news. The code runs an infinite loop, checking the RSS feeds for
new stories every 120 seconds.
User-Specified Triggers
Right now, your triggers are specified in your Python code, and to
change them, you have to edit your program. This is very user-
unfriendly. (Imagine if you had to edit the source code of your web
browser every time you wanted to add a bookmark!)
Instead, we want you to read your trigger configuration from a
triggers.txt file every timeyour application starts and use the
triggers specified there.
Consider the following example configuration file:
// description trigger named t1t
1,DESCRIPTION,Presidential Election
// title trigger named t2
t2,TITLE,Hillary Clinton
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
// description trigger named t3
t3,DESCRIPTION,Donald Trump
The example file specifies that four triggers should be created, and
that two of those triggers should be added to the trigger list:
●A trigger that fires when the description contains the phrase
'PresidentialElection' ( t1).
●A trigger that fires when the title contains the description Donald
Trump and the titlecontains the phrase Hillary Clinton ( t4 ).
The two other triggers ( t2 and t3 ) are created but not added to the
trigger list directly. They areused as arguments for the composite
AND trigger's definition ( t4 ).
Each line in this file does one of the following:
●is blank
●is a comment (begins with // with no spaces preceding the // )
●defines a named trigger
●adds triggers to the trigger list
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Each type of line is described below.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Trigger addition: A trigger definition should create a
trigger and associate it with a name but should NOT
automatically add that trigger to the trigger list. One or
more ADD lines in the trigger configuration file will specify which
triggers should be in the trigger list. An ADD line begins with the
ADD keyword. The elements following ADD are the names of one
or more previously defined triggers. The elements in these lines
are also separated by commas. These triggers will be added to the
the trigger list.
Problem 11
Finish implementing read_trigger_config(filename) . We've
written code to open thefile and throw away all blank lines and
comments. Your job is to finish the implementation.
read_trigger_config should return a list of triggers specified by the
configuration file.
Hint: Feel free to define a helper function if you wish! Using a
helper function is not required though.
Hint: You will probably find it helpful to use a dictionary where
the keys are trigger names.
Once that's done, modify the code within the function
main_thread to use the trigger list specified in your configuration
file, instead of the one we hard-coded for you:
# TODO: Problem 11
# After implementing read_trigger_config, uncomment this line:
# triggerlist = read_trigger_config('triggers.txt')
After completing Problem 11, you can try running ps5.py , and
depending on yourtriggers.txt file, various RSS news items should
pop up. The code runs an infinite loop,checking the RSS feed for
new stories every 120 seconds.
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com
Hint: If no stories are popping up, open up triggers.txt and change
the triggers to
onesthat reflect current events (if you don't keep up with the
news, just pick some triggers that would fire on the current
Google News stories).
Problem 12
For any Homework related queries, Call us at : - +1 678 648 4277
You can mail us at :- [email protected] or
reach us at :- www.pythonhomeworkhelp.com