From c4a25df20e287dc794bc31682a111482ec397f91 Mon Sep 17 00:00:00 2001 From: turulomio Date: Sat, 2 Dec 2023 07:57:22 +0100 Subject: [PATCH] Added lol_print (#21) --- pydicts/lol.py | 23 ++++++++++++++++++++++- pydicts/tests/test_lol.py | 24 +++++++++++++----------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pydicts/lol.py b/pydicts/lol.py index cb51623..f7281a2 100644 --- a/pydicts/lol.py +++ b/pydicts/lol.py @@ -1,6 +1,7 @@ from gettext import translation from importlib.resources import files from pydicts import exceptions +from tabulate import tabulate try: t=translation('pydicts', files("pydicts") / 'locale') @@ -25,7 +26,6 @@ def lol_add_column(rows, index, column): def list_remove_positions(l, listindex): if l is None: raise exceptions.LolException(_("I can't remove positions from a None list")) - return None r=[] for i, o in enumerate(l): if i not in listindex: @@ -47,6 +47,8 @@ def lol_remove_rows(rows, listindex): ## Return a lol transposed. Changed rows by columns def lol_transposed(lol): + if lol is None: + raise exceptions.LolException(_("I can't traspaso a None object")) if len(lol)==0: return [] r=[] @@ -93,3 +95,22 @@ def lol_sum_column(lol, column, from_index, to_index, zerovalue=0): if row[column] is not None: s=s + row[column] return s + +def lol_print(lod, number=None): + """ + Function Prints a list of list with tabulate module. + + @param lol + @type List of lists + @param number Number of lists to print. If None prints all lod. (defaults to None) + @type Integer + """ + number=len(lod) if number is None else number + + if len(lod)==0: + print(_("lol_print: This list of lists hasn't data to print")) + return + if number==0: + print(_("lol_print: No data was printed due to you selected 0 rows")) + return + print(tabulate(lod[0:number], tablefmt="psql")) \ No newline at end of file diff --git a/pydicts/tests/test_lol.py b/pydicts/tests/test_lol.py index 5d84657..d379570 100644 --- a/pydicts/tests/test_lol.py +++ b/pydicts/tests/test_lol.py @@ -1,25 +1,27 @@ #from datetime import datetime, date #from decimal import Decimal -from pydicts import lol -#from pytest import raises, fixture +from pydicts import lol, exceptions +from pytest import raises lol_=[] for i in range(10): lol_.append([1*i,2*i,3*i]) - -print(lol_) - def test_lol_add_column(): lol.lol_add_column(lol_, 2, [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) + +def test_lol_print(): + lol.lol_print(lol_) + lol.lol_print(lol_,0) + lol.lol_print(lol_,-1) -# -#if __name__ == "__main__": -# def print_lor(lor): -# print("") -# for row in lor: -# print(row) +def test_lol_transposed(): + transposed=lol.lol_transposed(lol_) + transposed=lol.lol_transposed([]) + with raises(exceptions.LolException): + transposed=lol.lol_transposed(None) + # # lor=[] # column_to_add=[]