Mar-09-2019, 12:53 AM
ichabod801 - ok, I understand what you are saying. But, I am still trying to figure out if the discover is supposed to be as def discover_path(self), then how do I get it to recognize the self._src as path? The code is supposed to be written to accept any website, file, or text listed below. But I found if I used an if/else it gave me an error. Thank you for your help! It is greatly appreciated!
Here is the snipit of the code where this starts:
Here is the snipit of the code where this starts:
import requests, re from bs4 import BeautifulSoup from collections import Counter import statistics as stats import string import operator import matplotlib.pyplot as plt plt.rcdefaults() class TextAnalyzer(): "A Text Analyzer" def __init__(self, src, src_type='discover'): """Creates a object for analyzing text Keyword arguments: src (str) -- text, path to file, or url src_type (str) -- The type of input (text, path, url, discover)""" if isinstance(src, str) == False or len(src) <= 0: raise exception('Source must be a valid string, filepath or a valid URL') self._src = src self._src_type = src_type self._content = None self._orig_content = None def discover_url(self): self._src.startswith('http') self._src_type = 'url' url = 'https://www.webucator.com/how-to/address-by-bill-clinton-1997.cfm' r = requests.get(self._src) res = r.content self._orig_content = r.text self._content = res def discover_path(self): self._src.endswith('.txt') src_type = 'path' with open('pride-and-prejudice.txt') as f: self._content = self._orig_content def discover_text(self): src_type = 'text' text = ("The outlook wasn't brilliant for the Mudville Nine that day;the score stood four to two, with but one inning more to play. And then when Cooney died at first, and Barrows did the same, a sickly silence fell upon the patrons of the game.") self._orig_content = self._src self._content = self._srcHere is the test code for this part:
import unittest url = 'https://www.webucator.com/how-to/address-by-bill-clinton-1997.cfm' path = 'pride-and-prejudice.txt' text = '''The outlook wasn't brilliant for the Mudville Nine that day; the score stood four to two, with but one inning more to play. And then when Cooney died at first, and Barrows did the same, a sickly silence fell upon the patrons of the game.''' class TestTextAnalyzer(unittest.TestCase): def test_discover_url(self): ta = TextAnalyzer(url) self.assertEqual(ta._src_type, 'url') def test_discover_path(self): ta = TextAnalyzer(path) self.assertEqual(ta._src_type, 'path') def test_discover_text(self): ta = TextAnalyzer(text) self.assertEqual(ta._src_type, 'text')