summaryrefslogtreecommitdiff
path: root/tablib/core.py
blob: 4ea6b94581d1c087300951574cfd83c9e8c88060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# -*- coding: utf-8 -*-

# _____         ______  ______        _________
# __  /_______ ____  /_ ___  /_ _____ ______  /
# _  __/_  __ `/__  __ \__  __ \_  _ \_  __  / 
# / /_  / /_/ / _  /_/ /_  /_/ //  __// /_/ /  
# \__/  \__,_/  /_.___/ /_.___/ \___/ \__,_/   


import csv

from helpers import *
from packages import simplejson as json
from packages import xlwt


__all__ = ['Dataset', 'source']

__version__ = '0.0.3'
__build__ = '0x000003'
__author__ = 'Kenneth Reitz'
__license__ = 'MIT'
__copyright__ = 'Copyright 2010 Kenneth Reitz'



class Dataset(object):
	"""Amazing Tabular Dataset object. """

	def __init__(self, *args, **kwargs):
		self._data = None
		self._filename = None

		self._data = list(args)

		try:
		    self.headers = kwargs['headers']
		except KeyError, why:
			self.headers = None

			
	def __len__(self):
		return self.height

	def __getitem__(self, 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):
		self.validate(value)
		self._data[key] = value


	def __delitem__(self, key):
		del self._data[key]

		
	def __repr__(self):
		return '<dataset object>' 


	def validate(self, row=None, safety=False):
		"""Assures size of every row in dataset is of proper proportions."""
		if row:
			is_valid = (len(row) == self.width) if self.width else True
		else:
			is_valid = all((len(x)== self.width for x in self._data))

		if is_valid:
			return True

		else:
			if not safety:
				raise InvalidDimensions
			return False
	
	def digest(self):
		"""Retruns digest information of dataset in human-readable format."""
		pass


	@property
	def height(self):
		"""Returns the height of the Dataset."""
		return len(self._data)


	@property
	def width(self):
		"""Returns the width of the Dataset."""
		
		try:
		    return len(self._data[0])
		except KeyError, why:
		    return 0
			

	@property
	def json(self):
		if self.headers:
			pass

		
	@property
	def yaml(self):
		# TODO: YAML Export
		pass


	@property
	def csv(self):
		# TODO: CSV Export
		pass


	@property
	def xls(self):
		# TODO: XLS Export
		pass


	def append(self, row, index=None):
		# todo: impliment index
		self.validate(row)
		self._data.append(row)

		
	def sort_by(self, key):
		"""SORTS datastet by given key"""
		# todo: accpept string if headers, or index nubmer
		pass


	def save(self):
		pass

		# note export format
		# open file, save the bitch

class InvalidDimensions(Exception):
	"Invalid size"


def source(io_string=None, filename=None):
	"""docstring for import"""
	#open by filename
	pass