Skip to content

Commit

Permalink
feat: add markdown_tables.format_table
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Oct 31, 2024
1 parent 1fad020 commit 2e62698
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions calcipy/code_tag_collector/_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from corallium.log import LOGGER
from corallium.shell import capture_shell

from calcipy._md_helpers import _format_md_table
from calcipy.markdown_table import format_table

SKIP_PHRASE = 'calcipy_skip_tags'
"""String that indicates the file should be excluded from the tag search."""
Expand Down Expand Up @@ -279,7 +279,7 @@ def _format_report(
)
counter[comment.tag] += 1
if records:
output += '\n' + '\n'.join(_format_md_table(headers=[*records[0]], records=records))
output += '\n' + format_table(headers=[*records[0]], records=records)
LOGGER.text_debug('counter', counter=counter)

sorted_counter = {tag: counter[tag] for tag in tag_order if tag in counter}
Expand Down
17 changes: 10 additions & 7 deletions calcipy/_md_helpers.py → calcipy/markdown_table.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""Markdown utilities."""
"""Markdown table formatting."""

from beartype.typing import Any, Dict, List
from __future__ import annotations

from typing import Any

def _format_md_table(headers: List[str], records: List[Dict[str, Any]]) -> List[str]:
"""Format the input as a Github markdown table."""

def format_table(headers: list[str], records: list[dict[str, Any]]) -> str:
"""Returns a formatted Github Markdown table."""
table = [[str(_r[col]) for col in headers] for _r in records]
widths = [max(len(row[col_idx].strip()) for row in [headers, *table]) for col_idx in range(len(headers))]

def pad(values: List[str]) -> List[str]:
def pad(values: list[str]) -> list[str]:
return [val.strip().ljust(widths[col_idx]) for col_idx, val in enumerate(values)]

def join(row: List[str], spacer: str = ' ') -> str:
def join(row: list[str], spacer: str = ' ') -> str:
return f'|{spacer}' + f'{spacer}|{spacer}'.join(row) + f'{spacer}|'

return [
lines = [
join(pad(headers)),
join(['-' * widths[col_idx] for col_idx in range(len(headers))], '-'),
*[join(pad(row)) for row in table],
]
return '\n'.join(lines)
6 changes: 4 additions & 2 deletions calcipy/md_writer/_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Markdown Machine."""

from __future__ import annotations

import json
import re
from pathlib import Path
Expand All @@ -8,9 +10,9 @@
from corallium.file_helpers import read_lines
from corallium.log import LOGGER

from calcipy._md_helpers import _format_md_table
from calcipy.file_search import find_project_files_by_suffix
from calcipy.invoke_helpers import get_project_path
from calcipy.markdown_table import format_table

HandlerLookupT = Dict[str, Callable[[str, Path], List[str]]]
"""Handler Lookup."""
Expand Down Expand Up @@ -183,7 +185,7 @@ def _format_cov_table(coverage_data: Dict[str, Any]) -> List[str]:
)
records = [{**_r, 'Coverage': f"{round(_r['Coverage'], 1)}%"} for _r in records]

lines_table = _format_md_table(headers=['File', *col_key_map], records=records)
lines_table = format_table(headers=['File', *col_key_map], records=records).split('\n')
short_date = coverage_data['meta']['timestamp'].split('T')[0]
lines_table.extend(['', f'Generated on: {short_date}'])
return lines_table
Expand Down
1 change: 1 addition & 0 deletions docs/docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Refactor

- finish removing pydantic dependency
- switch serialized BaseModel to dataclass
- switch some BaseModels to plain dataclasses
- sync Github Labeler
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/CODE_TAG_SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
|---------|------------------------------------------------------------------------------|------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| PLANNED | support completions | 2024-10-07 | [calcipy/cli.py:103](https://github.com/KyleKing/calcipy/blame/aa2adcff703f8fe7da95dc2486030dbacf4a5802/calcipy/cli.py#L103) |
| PLANNED | Also see how https://docs.astral.sh/ruff/configuration/#shell-autocompletion | 2024-10-10 | [calcipy/cli.py:112](https://github.com/KyleKing/calcipy/blame/8c9b14b336b2d8f28e2e946018d9add3f260bdfe/calcipy/cli.py#L112) |
| TODO | How to capture output? | 2024-10-05 | [calcipy/collection.py:38](https://github.com/KyleKing/calcipy/blame/22a490ebc56994f3269b7a83071e88b8b3fd5f89/calcipy/collection.py#L38) |
| TODO | How to capture output? | 2024-10-05 | [calcipy/collection.py:50](https://github.com/KyleKing/calcipy/blame/22a490ebc56994f3269b7a83071e88b8b3fd5f89/calcipy/collection.py#L38) |
| TODO | Handle ">=3.0.0,<4" | 2024-10-05 | [calcipy/experiments/sync_package_dependencies.py:48](https://github.com/KyleKing/calcipy/blame/22a490ebc56994f3269b7a83071e88b8b3fd5f89/calcipy/experiments/sync_package_dependencies.py#L48) |
| TODO | Add unit test | 2024-07-06 | [calcipy/tasks/pack.py:59](https://github.com/KyleKing/calcipy/blame/e45ecadfa5b994d9c0a2a47138fa0e083261e3eb/calcipy/tasks/pack.py#L57) |
| TODO | Add unit test | 2024-07-06 | [calcipy/tasks/pack.py:93](https://github.com/KyleKing/calcipy/blame/e45ecadfa5b994d9c0a2a47138fa0e083261e3eb/calcipy/tasks/pack.py#L91) |
| PLANNED | finish updating docstrings for Returns | 2024-10-08 | [pyproject.toml:161](https://github.com/KyleKing/calcipy/blame/9cf3c6d2d9820cec475d35bdb7c53fc83627a4b2/pyproject.toml#L161) |
| PLANNED | finish updating docstrings for Returns | 2024-10-08 | [pyproject.toml:154](https://github.com/KyleKing/calcipy/blame/9cf3c6d2d9820cec475d35bdb7c53fc83627a4b2/pyproject.toml#L161) |
| TODO | Capture logging output and check... | 2023-02-19 | [tests/check_for_stale_packages/test_check_for_stale_packages.py:64](https://github.com/KyleKing/calcipy/blame/a8b69e7b04d9b15eabff8897f2de7703898c2afc/tests/check_for_stale_packages/test_check_for_stale_packages.py#L63) |
| TODO | Capture logging output and check... | 2023-02-19 | [tests/check_for_stale_packages/test_check_for_stale_packages.py:88](https://github.com/KyleKing/calcipy/blame/3f42ad855eb7024ff48af35d496633a87d4a14ac/tests/check_for_stale_packages/test_check_for_stale_packages.py#L26) |
| PLANNED | Add unit test for sync_package_dependencies.py | 2024-10-07 | [tests/experiments/test_sync_package_dependencies.py:1](https://github.com/KyleKing/calcipy/blame/dbe495b3653edb2fd06f9e3865619707d941ed87/tests/experiments/test_sync_package_dependencies.py#L1) |
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ poetry config pypi-token.pypi ...
| File | Statements | Missing | Excluded | Coverage |
|-----------------------------------------------------------------|------------|---------|----------|----------|
| `calcipy/__init__.py` | 4 | 0 | 0 | 100.0% |
| `calcipy/_md_helpers.py` | 9 | 0 | 0 | 100.0% |
| `calcipy/_runtime_type_check_setup.py` | 13 | 0 | 33 | 100.0% |
| `calcipy/can_skip.py` | 14 | 1 | 0 | 88.9% |
| `calcipy/check_for_stale_packages/__init__.py` | 5 | 2 | 0 | 60.0% |
Expand All @@ -65,8 +64,9 @@ poetry config pypi-token.pypi ...
| `calcipy/experiments/sync_package_dependencies.py` | 47 | 47 | 0 | 0.0% |
| `calcipy/file_search.py` | 32 | 0 | 2 | 100.0% |
| `calcipy/invoke_helpers.py` | 27 | 4 | 0 | 83.8% |
| `calcipy/markdown_table.py` | 11 | 0 | 0 | 100.0% |
| `calcipy/md_writer/__init__.py` | 5 | 2 | 0 | 60.0% |
| `calcipy/md_writer/_writer.py` | 91 | 6 | 0 | 89.6% |
| `calcipy/md_writer/_writer.py` | 92 | 6 | 0 | 89.7% |
| `calcipy/noxfile/__init__.py` | 5 | 2 | 0 | 60.0% |
| `calcipy/noxfile/_noxfile.py` | 39 | 2 | 51 | 91.5% |
| `calcipy/scripts.py` | 6 | 0 | 51 | 100.0% |
Expand All @@ -84,7 +84,7 @@ poetry config pypi-token.pypi ...
| `calcipy/tasks/tags.py` | 18 | 1 | 0 | 90.9% |
| `calcipy/tasks/test.py` | 39 | 1 | 2 | 90.9% |
| `calcipy/tasks/types.py` | 11 | 0 | 0 | 93.3% |
| **Totals** | 1044 | 133 | 292 | 83.0% |
| **Totals** | 1047 | 133 | 292 | 83.0% |

Generated on: 2024-10-31
<!-- {cte} -->

0 comments on commit 2e62698

Please sign in to comment.