Skip to content

Commit

Permalink
Merge pull request #33 from ashishkg0022/url_gen
Browse files Browse the repository at this point in the history
Added feature for urls to be used as a source for generator
  • Loading branch information
Matthew Egan committed Oct 13, 2017
2 parents f73d0fd + 7d9f496 commit c15fbcd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rancat/Handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"""

from io import TextIOWrapper
from urllib.parse import urlparse
import requests

class Handler(object):
"""
Expand All @@ -26,7 +28,15 @@ def __init__(self, obj):

self.raw_obj = obj

if isinstance(obj, str):
if isinstance(obj, str) and urlparse(obj).scheme :
self.obj_type = list
self.page = requests.get(obj)
self.processed_obj = [line.decode('utf8') for line in self.page]
self.current_lines = [line.decode('utf8') for line in self.page]
self.opened = True
self.cursor = 0

elif isinstance(obj, str):
self.obj_type = TextIOWrapper
self.processed_obj = open(obj, 'r')
self.current_lines = []
Expand Down
7 changes: 7 additions & 0 deletions tests/test_rancat.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def test_load(self):
r.load(datafile)
assert len(r.files) == 1

def test_load_url(self):
data_url = 'http://www.sample-videos.com/text/Sample-text-file-10kb.txt'
seed_value = 123
r = RanCat(seed=seed_value)
r.load(data_url)
assert len(r.files) == 1

def test_next(self):
datafile = 'examples/data/colors.txt'
seed_value = 123
Expand Down

0 comments on commit c15fbcd

Please sign in to comment.