mirror of
https://github.com/kennethreitz/tablib.git
synced 2026-06-05 15:00:19 +00:00
Archaic imports in place!
This commit is contained in:
@@ -33,7 +33,7 @@ def import_set(in_stream, headers=True):
|
||||
rows = csv.reader(in_stream.split())
|
||||
for i, row in enumerate(rows):
|
||||
|
||||
if (i == 1) and (headers):
|
||||
if (i == 0) and (headers):
|
||||
data.headers = row
|
||||
else:
|
||||
data.append(row)
|
||||
|
||||
+14
-7
@@ -14,19 +14,26 @@ def export_set(dataset):
|
||||
|
||||
def export_book(databook):
|
||||
"""Returns JSON representation of Databook."""
|
||||
|
||||
return json.dumps(databook._package())
|
||||
|
||||
|
||||
def detect(contents):
|
||||
"""Return True if contets are JSON."""
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def import_set(in_stream):
|
||||
"""Returns dataset from JSON stream."""
|
||||
|
||||
data = tablib.core.Dataset()
|
||||
data.dict = json.loads(in_stream)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def import_book(in_stream):
|
||||
"""Returns databook from JSON stream."""
|
||||
|
||||
book = tablib.core.Databook()
|
||||
for sheet in json.loads(in_stream):
|
||||
data = tablib.core.Dataset()
|
||||
data.title = sheet['title']
|
||||
data.dict = sheet['data']
|
||||
book.add_sheet(data)
|
||||
|
||||
return book
|
||||
+14
-1
@@ -24,4 +24,17 @@ def import_set(in_stream):
|
||||
data = tablib.core.Dataset()
|
||||
data.dict = yaml.load(in_stream)
|
||||
|
||||
return data
|
||||
return data
|
||||
|
||||
|
||||
def import_book(in_stream):
|
||||
"""Returns databook from YAML stream."""
|
||||
|
||||
book = tablib.core.Databook()
|
||||
for sheet in yaml.load(in_stream):
|
||||
data = tablib.core.Dataset()
|
||||
data.title = sheet['title']
|
||||
data.dict = sheet['data']
|
||||
book.add_sheet(data)
|
||||
|
||||
return book
|
||||
@@ -200,6 +200,11 @@ class TablibTestCase(unittest.TestCase):
|
||||
|
||||
new_data.headers = self.headers
|
||||
new_data = tablib.formats.json.import_set(str(new_data.json))
|
||||
|
||||
book = tablib.Databook()
|
||||
book.add_sheet(new_data)
|
||||
new_book = tablib.formats.json.import_book(str(book.json))
|
||||
|
||||
|
||||
def test_yaml_import(self):
|
||||
"""Generate and import YAML serialization."""
|
||||
@@ -208,6 +213,11 @@ class TablibTestCase(unittest.TestCase):
|
||||
|
||||
new_data.headers = self.headers
|
||||
new_data = tablib.formats.yaml.import_set(str(new_data.json))
|
||||
|
||||
book = tablib.Databook()
|
||||
book.add_sheet(new_data)
|
||||
new_book = tablib.formats.yaml.import_book(str(book.yaml))
|
||||
|
||||
|
||||
def test_csv_import(self):
|
||||
"""Generate and import CSV serialization."""
|
||||
|
||||
Reference in New Issue
Block a user