From 2b3f2771384a8de615fab485d8541d38fc529c0d Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 29 Aug 2010 23:38:03 -0400 Subject: [PATCH] Time for some testing. --- tablib/core.py | 17 +++++++++++++---- tablib/helpers.py | 7 ++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tablib/core.py b/tablib/core.py index 1032013..0a8fce7 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -9,7 +9,9 @@ import csv +from helpers import * +__all__ = ['Dataset', 'source'] __version__ = '0.0.3' __build__ = '0x000003' @@ -17,8 +19,6 @@ __author__ = 'Kenneth Reitz' __license__ = 'MIT' __copyright__ = 'Copyright 2010 Kenneth Reitz' -__all__ = ['Dataset', 'source'] - class Dataset(object): @@ -39,7 +39,15 @@ class Dataset(object): def __getitem__(self, key): - return self._data[key] + + if is_string(key): + if key in self.headers: + pos = self.headers.index(key) # get 'key' index from each data + return [row[pos] for row in self._data] + else: + raise KeyError + else: + return self._data[key] def __setitem__(self, key, value): @@ -93,7 +101,8 @@ class Dataset(object): @property def json(self): - pass + if self.headers: + pass @property diff --git a/tablib/helpers.py b/tablib/helpers.py index 0f5232c..1afbc4c 100644 --- a/tablib/helpers.py +++ b/tablib/helpers.py @@ -15,6 +15,11 @@ class Object(object): def piped(): """Returns piped input via stdin, else False""" - with sys.stdin as stdin: return stdin.read() if not stdin.isatty() else None + + +def is_string(obj): + """Tests if an object is a string""" + + return True if type(obj).__name__ == 'str' else False \ No newline at end of file