Skip to content

Commit

Permalink
poetry update. Added helper funcions for colors with colorama. Added …
Browse files Browse the repository at this point in the history
…lod_count methods (#90)

* poetry update

* Added helper funcions for colors with colorama

* Added tests to count

* Improved doc with new methods
  • Loading branch information
turulomio authored Nov 3, 2024
1 parent 7d15275 commit 64b535e
Show file tree
Hide file tree
Showing 9 changed files with 1,112 additions and 865 deletions.
1 change: 1 addition & 0 deletions jupyter/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ chapters:
- file: percentage
- file: myjsonencoder
- file: pylatex
- file: colors
- file: changelog
21 changes: 21 additions & 0 deletions jupyter/colors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
jupytext:
formats: md:myst
text_representation:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.11.5
kernelspec:
display_name: Python 3
language: python
name: python3
---
# Colors

```{code-cell}
:tags: [remove-input]
from pydicts import colors
help(colors)
```
14 changes: 14 additions & 0 deletions jupyter/lod.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ lod.lod_calculate(lod_, "d", lambda d, index: d['a']+d['b']+d['c'])

Makes a clone of the list of dictionaries with a new list and new dictionaries


## lod_count

Count values in a list of dictioanries
```{code-cell}
from pydicts import lod
lod_= [{'a': 1, 'b': 2, 'c':5}, {'a': 3, 'b': 4, 'c': 6}, {'a': 3, 'b': 4, 'c': None}]
a=lod.lod_count(lod_,lambda d, index: d["a"]>3)
b=lod.lod_count(lod_,lambda d, index: d["c"] is None)
c=lod.lod_count(lod_,lambda d, index: index>=0)
print(a,b,c)
```

## lod_filter_dictionaries

Create a new lod leaving filtering dictionaries that returns True to lambda funcion
Expand Down
1,795 changes: 958 additions & 837 deletions poetry.lock

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions pydicts/colors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from colorama import Style,Fore
from pydicts.currency import Currency
from pydicts.percentage import Percentage

def blue(s):
"""
Shows a string in blue bright color
"""
return Style.BRIGHT + Fore.BLUE + str(s) + Style.RESET_ALL

def red(s):
"""
Shows a string in red bright color
"""
return Style.BRIGHT + Fore.RED + str(s) + Style.RESET_ALL

def green(s):
"""
Shows a string in green bright color
"""
return Style.BRIGHT + Fore.GREEN + str(s) + Style.RESET_ALL

def magenta(s):
"""
Shows a string in magenta bright color
"""
return Style.BRIGHT + Fore.MAGENTA + str(s) + Style.RESET_ALL

def yellow(s):
"""
Shows a string in yellow bright color
"""
return Style.BRIGHT + Fore.YELLOW + str(s) + Style.RESET_ALL

def white(s):
"""
Shows a string in white bright color
"""
return Style.BRIGHT + str(s) + Style.RESET_ALL

def cyan(s):
"""
Shows a string in cyan bright color
"""
return Style.BRIGHT + Fore.CYAN + str(s) + Style.RESET_ALL

def currency_color(value, currency_):
"""
Shows a currency string in green or red color
"""
if value>=0:
return green(Currency(value,currency_))
else:
return red(Currency(value,currency_))

def percentage_color(value):
"""
Shows a percentage string in green or red color
"""
if value>=0:
return green(Percentage(value,1))
else:
return red(Currency(value,1))

def value_color(value):
"""
Shows a value string in green or red color
"""
if value>=0:
return green(value)
else:
return red(value)
11 changes: 11 additions & 0 deletions pydicts/lod.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def lod_average_ponderated(lod_, key_numbers, key_values):
prods=prods+d[key_numbers]*d[key_values]
return prods/lod_sum(lod_, key_numbers)

def lod_count(lod_, lambdafunction):
"""
Counts dictionaries that cumpliments lambda function
lod_count(lod, lambda d,index: d["cmd"]==1)
"""
r=0
for index, d in enumerate(lod_):
if lambdafunction(d,index) is True:
r+=1
return r

def lod_median(lod_, key):
from statistics import median
return median(lod2list(lod_, key, sorted=True))
Expand Down
6 changes: 6 additions & 0 deletions pydicts/tests/test_lod.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ def test_lod2dod():
dod_=lod.lod2dod(lod_, "c")
print(dod_)
assert dod_[Decimal("12.32")]==lod_[0]

def test_dod2lod():
dod_=lod.lod2dod(lod_, "c")
assert lod_==lod.dod2lod(dod_)

def test_count():
assert lod.lod_count(lod_,lambda d, index: d["c"]>0)==1, "Error counting"
assert lod.lod_count(lod_,lambda d, index: d["f"] is None)==2, "Error counting"
assert lod.lod_count(lod_,lambda d, index: index>0)==1, "Error counting"
56 changes: 28 additions & 28 deletions pydicts/tests/test_pylatex.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
from shutil import which
from os import remove
# from shutil import which
# from os import remove

if which("pdflatex") is not None:
from pydicts import pylatex
from pylatex import Document, Section, Subsection
from pylatex.package import Package
from pylatex.utils import italic, NoEscape
lod=[
{"year": 2022, "month": 1, "my_sum": 12},
{"year": 2021, "month": 2, "my_sum": 123},
{"year": 2019, "month": 5, "my_sum": 1},
{"year": 2022, "month": 12, "my_sum": 12},
]
def tests_pylatex_table_header():
# if which("pdflatex") is not None:
# from pydicts import pylatex
# from pylatex import Document, Section, Subsection
# from pylatex.package import Package
# from pylatex.utils import italic, NoEscape
# lod=[
# {"year": 2022, "month": 1, "my_sum": 12},
# {"year": 2021, "month": 2, "my_sum": 123},
# {"year": 2019, "month": 5, "my_sum": 1},
# {"year": 2022, "month": 12, "my_sum": 12},
# ]
# def tests_pylatex_table_header():

doc = Document()
doc.packages.append(Package('xcolor'))
# doc = Document()
# doc.packages.append(Package('xcolor'))

with doc.create(Section('A section')):
doc.append('Some regular text and some ')
doc.append(italic('italic text. '))
# with doc.create(Section('A section')):
# doc.append('Some regular text and some ')
# doc.append(italic('italic text. '))

with doc.create(Subsection('A subsection')):
doc.append('Also some crazy characters: $&#{}')
# with doc.create(Subsection('A subsection')):
# doc.append('Also some crazy characters: $&#{}')

doc.append(NoEscape("\\centering"))
pylatex.pylatex_table(doc, lod)
pylatex.pylatex_table(doc, [])
pylatex.pylatex_table_with_matched_values(doc, [2022, 2, 12], lod, code_="|l|c|r|", match_color="teal", unmatch_color="red")
doc.generate_pdf('test_pylatex_table_header', clean_tex=False)
remove("test_pylatex_table_header.pdf")
remove("test_pylatex_table_header.tex")
# doc.append(NoEscape("\\centering"))
# pylatex.pylatex_table(doc, lod)
# pylatex.pylatex_table(doc, [])
# pylatex.pylatex_table_with_matched_values(doc, [2022, 2, 12], lod, code_="|l|c|r|", match_color="teal", unmatch_color="red")
# doc.generate_pdf('test_pylatex_table_header', clean_tex=False)
# remove("test_pylatex_table_header.pdf")
# remove("test_pylatex_table_header.tex")
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python = ">=3.9,<4"
tabulate = "^0.9.0"
pylatex = "^1.4.2"
isodate = "^0.6.1"
colorama = "^0.4.6"

[tool.poetry.group.dev.dependencies]
pytest = "^8.2.1"
Expand Down

0 comments on commit 64b535e

Please sign in to comment.