Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
Fix exception handling for list values.
Add remove_zero()
  • Loading branch information
JINWOO-J committed Nov 7, 2023
1 parent 612e091 commit 4c3fefb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion BUILD_ARGS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

--build-arg BASE_IMAGE=python:3.9.13-slim-buster
--build-arg BUILD_DATE=2023-11-07T05:29:04UTC
--build-arg BUILD_DATE=2023-11-07T05:30:31UTC
--build-arg DOCKER_BUILD_OPTION=--no-cache --rm=true
--build-arg ECHO_OPTION=
--build-arg GIT_USER=JINWOO-J
Expand Down
12 changes: 11 additions & 1 deletion examples/color_print/table_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def _make_value(value):
columns_options=dict(
address=dict(
style="magenta",
sdsds="sdsd"
# sdsds="sdsd"
)
),
)
Expand Down Expand Up @@ -109,4 +109,14 @@ def add_comma(value):
)


list_data = ["aaaaaaaaaa", "aaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]


PrintRichTable(
title="RichTable List with description",
data=list_data,
with_idx=True,
call_value_func=add_comma,
show_lines=True,
# columns=["address"]
)
2 changes: 1 addition & 1 deletion pawnlib/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__title__ = 'pawnlib'
__description__ = 'pawnlib is a collection of libraries for IaC.'
__url__ = 'https://github.com/jinwoo-j/pawnlib'
__version__ = '1.0.32'
__version__ = '1.0.33'
__author__ = 'Jinwoo Jeong'
__author_email__ = 'jinwoo@parametacorp.com'
__license__ = 'MIT'
Expand Down
7 changes: 6 additions & 1 deletion pawnlib/output/color_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ def _draw_horizontal_table(self):
row_dict[column] = value
line_row.append(value)
self.rows.append(line_row)
else:
self.rows.append([f"{self.row_count}", f"{item}"])
self.row_count += 1

for col in self.columns:
Expand All @@ -342,7 +344,10 @@ def _draw_horizontal_table(self):
def _extract_columns(self):
# if self.table_data and len(self.columns) == 0 and isinstance(self.table_data[0], dict):
if self.table_data and len(self.columns) == 0:
self.columns = list(self.table_data[0].keys())
try:
self.columns = list(self.table_data[0].keys())
except Exception as e:
self.columns = ["value"]
if self.with_idx:
self.columns.insert(0, "idx")
if callable(self.call_desc_func):
Expand Down
4 changes: 4 additions & 0 deletions pawnlib/resource/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def hex_mask_to_cidr(hex_mask):
server.hex_mask_to_cidr("FF00")
# >> 8
"""
try:
# Convert hexadecimal mask to integer
Expand Down
1 change: 1 addition & 0 deletions pawnlib/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
shorten_text,
truncate_float,
truncate_decimal,
remove_zero,
remove_tags,
json_to_hexadecimal,
hexadecimal_to_json,
Expand Down
23 changes: 23 additions & 0 deletions pawnlib/typing/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2425,6 +2425,29 @@ def truncate_decimal(number, digits: int = 2) -> decimal.Decimal:
return round(new_number, digits)


def remove_zero(int_value):
"""
Remove zero from the end of a float number if it's an integer.
:param int_value: The value to remove zero from.
:return: The value without trailing zero if it's a float number and is an integer, otherwise the original value.
Example:
.. code-block:: python
remove_zero(5.0)
# >> 5
remove_zero(5.5)
# >> 5.5
"""
if isinstance(int_value, float) and int(int_value) == int_value:
return int(int_value)
return int_value


def remove_tags(text,
case_sensitive: Literal["lower", "upper", "both"] = "lower",
tag_style: Literal["angle", "square"] = "square") -> str:
Expand Down

0 comments on commit 4c3fefb

Please sign in to comment.