Skip to content
This repository has been archived by the owner on Dec 28, 2020. It is now read-only.

Commit

Permalink
Created a new ArchivedURLSet class for lists of ArchivedURL objects. …
Browse files Browse the repository at this point in the history
…Right now it's nothing more than a standard list that prevents duplicates and only allows ArchivedURL objects as members
  • Loading branch information
palewire committed Jul 14, 2014
1 parent aa17f89 commit 38c4e0c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 18 additions & 0 deletions storytracker/analysis.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import gzip
import copy
import storytracker
from bs4 import BeautifulSoup

Expand Down Expand Up @@ -44,6 +45,23 @@ def hyperlinks(self):
return link_list


class ArchivedURLSet(list):
"""
A list of archived URLs sorted by their timestamp
"""
def append(self, obj):
# Verify that the user is trying to add an ArchivedURL object
if not isinstance(obj, ArchivedURL):
raise TypeError("Only ArchivedURL objects can be added")

# Check if the object is already in the list
if obj in [o for o in list(self.__iter__())]:
raise ValueError("This object is already in the list")

# If it's all true, append it.
super(ArchivedURLSet, self).append(copy.copy(obj))


class Hyperlink(object):
"""
A hyperlink extracted from an archived URL with tools for analysis
Expand Down
18 changes: 17 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import storytracker
from datetime import datetime
from bs4 import BeautifulSoup
from storytracker.analysis import ArchivedURL, Hyperlink
from storytracker.analysis import ArchivedURL
from storytracker.analysis import ArchivedURLSet
from storytracker.analysis import Hyperlink


class NullDevice():
Expand Down Expand Up @@ -100,6 +102,20 @@ def test_url_hyperlinks(self):
self.assertTrue(isinstance(obj.hyperlinks, list))
[self.assertTrue(isinstance(a, Hyperlink)) for a in obj.hyperlinks]

def test_urlset_creation(self):
obj = ArchivedURL(self.url, datetime.now(), "foobar")
obj2 = ArchivedURL(self.url, datetime.now(), "foobar")
obj3 = ArchivedURL(self.url, datetime.now(), "foobar")
urlset = ArchivedURLSet([obj, obj2])
self.assertEqual(len(urlset), 2)
with self.assertRaises(TypeError):
urlset.append(1)
urlset.append([1,2,3])
with self.assertRaises(ValueError):
urlset.append(obj)
urlset.append(obj3)
self.assertEqual(len(urlset), 3)


if __name__ == '__main__':
if six.PY3:
Expand Down

0 comments on commit 38c4e0c

Please sign in to comment.