Skip to content

Commit

Permalink
Merge pull request petl-developers#358 from alimanfoo/issue_323
Browse files Browse the repository at this point in the history
resolves petl-developers#323 with documentation and extra test
  • Loading branch information
alimanfoo committed Dec 9, 2015
2 parents 27467ec + f034bfd commit eb7f81e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
25 changes: 25 additions & 0 deletions petl/io/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ def open(self, mode='r'):


class MemorySource(object):
"""Memory data source. E.g.::
>>> import petl as etl
>>> data = b'foo,bar\\na,1\\nb,2\\nc,2\\n'
>>> source = etl.MemorySource(data)
>>> tbl = etl.fromcsv(source)
>>> tbl
+-----+-----+
| foo | bar |
+=====+=====+
| 'a' | '1' |
+-----+-----+
| 'b' | '2' |
+-----+-----+
| 'c' | '2' |
+-----+-----+
>>> sink = etl.MemorySource()
>>> tbl.tojson(sink)
>>> sink.getvalue()
b'[{"foo": "a", "bar": "1"}, {"foo": "b", "bar": "2"}, {"foo": "c", "bar": "2"}]'
Also supports appending.
"""

def __init__(self, s=None):
self.s = s
Expand Down
22 changes: 18 additions & 4 deletions petl/test/io/test_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@

from petl.test.helpers import ieq, eq_
import petl as etl
from petl.io.sources import StringSource, PopenSource, ZipSource, \
from petl.io.sources import MemorySource, PopenSource, ZipSource, \
StdoutSource, GzipSource, BZ2Source


def test_stringsource():
def test_memorysource():
tbl1 = (('foo', 'bar'),
('a', '1'),
('b', '2'),
('c', '2'))

# test writing to a string buffer
ss = StringSource()
ss = MemorySource()
etl.tocsv(tbl1, ss)
expect = "foo,bar\r\na,1\r\nb,2\r\nc,2\r\n"
if not PY2:
Expand All @@ -31,7 +31,7 @@ def test_stringsource():
eq_(expect, actual)

# test reading from a string buffer
tbl2 = etl.fromcsv(StringSource(actual))
tbl2 = etl.fromcsv(MemorySource(actual))
ieq(tbl1, tbl2)
ieq(tbl1, tbl2)

Expand All @@ -44,6 +44,20 @@ def test_stringsource():
eq_(expect, actual)


def test_memorysource_2():

data = 'foo,bar\r\na,1\r\nb,2\r\nc,2\r\n'
if not PY2:
data = data.encode('ascii')
actual = etl.fromcsv(MemorySource(data))
expect = (('foo', 'bar'),
('a', '1'),
('b', '2'),
('c', '2'))
ieq(expect, actual)
ieq(expect, actual)


def test_popensource():

expect = (('foo', 'bar'),)
Expand Down

0 comments on commit eb7f81e

Please sign in to comment.