Exemple #1
0
    def test_queries_empty(self):
        df0 = Table(cols, [], 'Empty')
        q_create = df0.to_sql_create()
        q_values = df0.to_sql_values()

        df2 = Table.from_sql_create(q_create)
        df2.add_sql_values(q_values)

        self.assertEqual(df2.columns, cols)
        self.assertEqual(df2.rowcnt, 0)
Exemple #2
0
	def parse_xls(self, bs):
		import xlrd

		def xlrd_parse_date(value):
			# XLRD returns dates as strange timestamps which must be properly parsed
			return datetime.datetime(*xlrd.xldate_as_tuple(float(value), book.datemode)).date()

		book = xlrd.open_workbook(file_contents=bs)
		first_sheet = book.sheet_by_index(0)
		self._parse_init()
		self.row_index = 0
		_typefuns = {}
		_typefuns.update(typefuns)
		_typefuns['date'] = xlrd_parse_date

		while True:
			try:
				row = first_sheet.row_values(self.row_index)
				self.parse_row(row, _typefuns)
				self.row_index += 1
				if self.row_limit > 0 and self.row_index > self.row_limit:
					break
			except IndexError:
				break
		return Table(self.res_columns, self.res_rows)
Exemple #3
0
	def parse_csv(self, fl, delimiter=None):
		self._parse_init()
		csvreader = csv.reader(fl, delimiter=delimiter, quotechar='"')

		for self.row_index, row in enumerate(csvreader):
			self.parse_row(row)
			if self.row_limit > 0 and self.row_index > self.row_limit:
				break
		return Table(self.res_columns, self.res_rows)
Exemple #4
0
 def test_from_columns(self):
     columns = [
         df1['type'],
         df1['weight'] + df1['size']**2,
     ]
     title = 'Random test title'
     df2 = Table.from_columns(columns, title)
     self.assertEqual(df2.rowcnt, df1.rowcnt)
     self.assertEqual(df2.colcnt, len(columns))
     self.assertEqual(df2.title, title)
Exemple #5
0
    def test_queries(self):
        q_create = df1.to_sql_create()
        q_values = df1.to_sql_values()

        df2 = Table.from_sql_create(q_create)
        df2.add_sql_values(q_values)

        self.assertEqual(df2.columns, df1.columns)
        self.assertEqual(df2.rowcnt, df1.rowcnt)
        self.assertEqual(df2['size'].sum(), df1['size'].sum())
        self.assertEqual(df2['weight'].sum(), df1['weight'].sum())
Exemple #6
0
	def test_concat(self):
		df3 = Table.concat([df1, df2])
		self.assertEqual(df1.columns, df3.columns)
		self.assertEqual(df3.rowcnt, df1.rowcnt+df2.rowcnt)
		self.assertEqual(df3['size'].sum(), df1['size'].sum()+df2['size'].sum())

		df1.append(df2)
		# Now, df1 must be equal to df3
		self.assertEqual(df1.columns, df2.columns)
		self.assertEqual(df1.rowcnt, df3.rowcnt)
		self.assertEqual(df1['size'].sum(), df3['size'].sum())

		trash = '1234567890'
		df3.rows[1][3] = trash
		df3.rows[4][3] = trash
		# It must not affect df1 or df2
		self.assertEqual((df1['tel']==trash).sum(), 0)
		self.assertEqual((df2['tel']==trash).sum(), 0)
		self.assertEqual((df3['tel']==trash).sum(), 2)
Exemple #7
0
from lemuras import Table

cols = ['type', 'size', 'weight', 'tel', 'when']
rows = [
	['A', 1, 12, '+79360360193', '1983-09-14'],
	['B', 4, 12, 84505505151, '13.08.2018'],
	['A', 3, 10, '31415926535', '2019-02-23 15:20'],
	['B', 6, 14, '', '2011/11/11'],
	['A', 4, 15, '23816326412', '25/03/95'],
	['A', 2, 12, None, None],
]
df1 = Table(cols, rows, 'Sample')
Exemple #8
0
	def __init__(self, *args, **kwargs):
		super(TestLemurasTableGroupby, self).__init__(*args, **kwargs)
		df1 = Table(cols, rows)
		self.gr = df1.groupby(['type', 'weight'])
Exemple #9
0
	def test_load(self):
		df1 = Table.from_html(html_table, title='Numbers')
		self.assertEqual(df1.columns, ['Name', 'Value'])
		self.assertEqual(df1.rowcnt, 3)
		self.assertEqual(df1['Value'].sum(), 7.4779084706)
Exemple #10
0
 def test_json_dict_pretty(self):
     txt = df1.to_json(as_dict=True, pretty=True)
     df2 = Table.from_json(txt)
     self.assertEqual(df2.columns, cols)
     self.assertEqual(df2.rowcnt, df1.rowcnt)
     self.assertEqual(df2['weight'].sum(), df1['weight'].sum())
Exemple #11
0
 def test_json_list(self):
     txt = df1.to_json(pretty=False)
     df2 = Table.from_json(txt)
     self.assertEqual(df2.columns, cols)
     self.assertEqual(df2.rowcnt, df1.rowcnt)
     self.assertEqual(df2['weight'].sum(), df1['weight'].sum())
Exemple #12
0
	def test_outer(self):
		df3 = Table.merge(df1, df2, ['type', 'size'], 'outer')
		self.assertEqual(df3.rowcnt, cnt_inner+cnt_left+cnt_right)
		self.assertEqual(df3.columns, ['type', 'size', 'weight', 'tel', 'cost'])
		self.assertEqual(df3.nunique()[1].sum(), 23)
		self.assertEqual(df3['weight'].sum(), 74)
Exemple #13
0
	def test_inner(self):
		df3 = Table.merge(df1, df2, ['type', 'size'], 'inner')
		self.assertEqual(df3.rowcnt, cnt_inner)
		self.assertEqual(df3.columns, ['type', 'size', 'weight', 'tel', 'cost'])
		self.assertEqual(df3.nunique()[1].sum(), 13)
		self.assertEqual(df3['weight'].sum(), 34)
Exemple #14
0
import unittest
from lemuras import Table

cols1 = ['type', 'size', 'weight', 'tel']
rows1 = [
	['A', 2, 12, '+79360360193'],
	['B', 1, 12, 84505505151],
	['D', 1, 10, '31415926535'],
	['B', 2, 14, ''],
	['A', 1, 15, '23816326412'],
	['D', 2, 11, 0],
]
df1 = Table(cols1, rows1)

cols2 = ['type', 'size', 'cost']
rows2 = [
	['A', 2, 3.1415],
	['B', 1, 2.7182],
	['C', 2, 1.2345],
	['D', 1, 1.2345],
]
df2 = Table(cols2, rows2)

cnt_inner = 3
cnt_left = 3
cnt_right = 1


class TestLemurasTableMerge(unittest.TestCase):
	def test_inner(self):
		df3 = Table.merge(df1, df2, ['type', 'size'], 'inner')
Exemple #15
0
 def test_load_results(self):
     df2 = Table.from_sql_result(sql_result)
     self.assertEqual(df2.columns, ['id', 'name', 'q1', 'q2', 'q3'])
     self.assertEqual(df2.rowcnt, 8)
     self.assertEqual(df2['id'].sum(), 19169)
Exemple #16
0
	def test_save(self):
		txt = Table.from_html(html_table).html()
		df2 = Table.from_html(txt, title='Numbers')
		self.assertEqual(df2.columns, ['Name', 'Value'])
		self.assertEqual(df2.rowcnt, 3)
		self.assertEqual(df2['Value'].sum(), 7.4779084706)
Exemple #17
0
import unittest
from lemuras import Table

cols = ['x', 'y', 'val']
rows = [
    [0, 'c', 3],
    [2, 'b', 7],
    [1, 'a', 2],
    [4, 'c', 2],
]
df1 = Table(cols, rows)

cols = ['x', 'y', 'val']
rows = [
    [1, 'a', 3],
    [0, 'c', 3],
    [2, 'b', 7],
    [0, 'c', 1],
    [1, 'a', 2],
    [4, 'c', 2],
]
df2 = Table(cols, rows)


class TestLemurasTablePivot(unittest.TestCase):
    def test_pivot_main(self):
        res = df1.pivot('x', 'y', 'val', 0)
        self.assertEqual(res.colcnt, df1['x'].nunique() + 1)
        self.assertEqual(res.rowcnt, df1['y'].nunique())
        self.assertEqual(
            res.loc(res['y'] == 'c').row().sum(),
Exemple #18
0
import unittest
from lemuras import Table

cols = ['type', 'size', 'weight', 'tel']
rows = [
	['A', 1, 10, '+79360360193'],
	['B', 4, 12, 84505505151],
]
df1 = Table(cols, rows, 'First')

cols = ['type', 'size', 'weight', 'tel']
rows = [
	['A', 2, 10, '31415926535'],
	['B', 6, 14, ''],
	['A', 3, 12, 0],
]
df2 = Table(cols, rows, 'Second')


class TestLemurasTableConcat(unittest.TestCase):
	def test_concat(self):
		df3 = Table.concat([df1, df2])
		self.assertEqual(df1.columns, df3.columns)
		self.assertEqual(df3.rowcnt, df1.rowcnt+df2.rowcnt)
		self.assertEqual(df3['size'].sum(), df1['size'].sum()+df2['size'].sum())

		df1.append(df2)
		# Now, df1 must be equal to df3
		self.assertEqual(df1.columns, df2.columns)
		self.assertEqual(df1.rowcnt, df3.rowcnt)
		self.assertEqual(df1['size'].sum(), df3['size'].sum())