Skip to content

Commit

Permalink
Added lol_print (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
turulomio committed Dec 2, 2023
1 parent 170e17f commit c4a25df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
23 changes: 22 additions & 1 deletion pydicts/lol.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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:
Expand All @@ -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=[]
Expand Down Expand Up @@ -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"))
24 changes: 13 additions & 11 deletions pydicts/tests/test_lol.py
Original file line number Diff line number Diff line change
@@ -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=[]
Expand Down

0 comments on commit c4a25df

Please sign in to comment.