prettytable 3.12.0__tar.gz → 3.14.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. {prettytable-3.12.0 → prettytable-3.14.0}/.coveragerc +4 -0
  2. {prettytable-3.12.0 → prettytable-3.14.0}/.github/workflows/labels.yml +4 -3
  3. {prettytable-3.12.0 → prettytable-3.14.0}/.github/workflows/lint.yml +10 -6
  4. {prettytable-3.12.0 → prettytable-3.14.0}/.github/workflows/release-drafter.yml +0 -3
  5. {prettytable-3.12.0 → prettytable-3.14.0}/.github/workflows/release.yml +2 -2
  6. {prettytable-3.12.0 → prettytable-3.14.0}/.github/workflows/require-pr-label.yml +8 -3
  7. {prettytable-3.12.0 → prettytable-3.14.0}/.github/workflows/test.yml +4 -6
  8. {prettytable-3.12.0 → prettytable-3.14.0}/.pre-commit-config.yaml +14 -8
  9. {prettytable-3.12.0 → prettytable-3.14.0}/PKG-INFO +19 -8
  10. {prettytable-3.12.0 → prettytable-3.14.0}/README.md +16 -5
  11. {prettytable-3.12.0 → prettytable-3.14.0}/pyproject.toml +12 -10
  12. prettytable-3.14.0/src/prettytable/__main__.py +47 -0
  13. {prettytable-3.12.0 → prettytable-3.14.0}/src/prettytable/_version.py +2 -2
  14. {prettytable-3.12.0 → prettytable-3.14.0}/src/prettytable/prettytable.py +54 -34
  15. {prettytable-3.12.0 → prettytable-3.14.0}/tests/test_prettytable.py +406 -43
  16. prettytable-3.14.0/tests/test_sections.py +55 -0
  17. {prettytable-3.12.0 → prettytable-3.14.0}/tox.ini +2 -2
  18. {prettytable-3.12.0 → prettytable-3.14.0}/.github/FUNDING.yml +0 -0
  19. {prettytable-3.12.0 → prettytable-3.14.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  20. {prettytable-3.12.0 → prettytable-3.14.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  21. {prettytable-3.12.0 → prettytable-3.14.0}/.github/SECURITY.md +0 -0
  22. {prettytable-3.12.0 → prettytable-3.14.0}/.github/dependabot.yml +0 -0
  23. {prettytable-3.12.0 → prettytable-3.14.0}/.github/labels.yml +0 -0
  24. {prettytable-3.12.0 → prettytable-3.14.0}/.github/release-drafter.yml +0 -0
  25. {prettytable-3.12.0 → prettytable-3.14.0}/.gitignore +0 -0
  26. {prettytable-3.12.0 → prettytable-3.14.0}/CHANGELOG.md +0 -0
  27. {prettytable-3.12.0 → prettytable-3.14.0}/LICENSE +0 -0
  28. {prettytable-3.12.0 → prettytable-3.14.0}/RELEASING.md +0 -0
  29. {prettytable-3.12.0 → prettytable-3.14.0}/src/prettytable/__init__.py +2 -2
  30. {prettytable-3.12.0 → prettytable-3.14.0}/src/prettytable/colortable.py +0 -0
  31. {prettytable-3.12.0 → prettytable-3.14.0}/src/prettytable/py.typed +0 -0
  32. {prettytable-3.12.0 → prettytable-3.14.0}/tests/test_colortable.py +0 -0
@@ -7,3 +7,7 @@ exclude_also =
7
7
  if 0:
8
8
  if __name__ == .__main__.:
9
9
  def main()
10
+
11
+ [run]
12
+ omit =
13
+ **/__main__.py
@@ -1,8 +1,5 @@
1
1
  name: Sync labels
2
2
 
3
- permissions:
4
- pull-requests: write
5
-
6
3
  on:
7
4
  push:
8
5
  branches:
@@ -13,9 +10,13 @@ on:
13
10
 
14
11
  jobs:
15
12
  sync:
13
+ permissions:
14
+ pull-requests: write
16
15
  runs-on: ubuntu-latest
17
16
  steps:
18
17
  - uses: actions/checkout@v4
18
+ with:
19
+ persist-credentials: false
19
20
  - uses: micnncim/action-label-syncer@v1
20
21
  with:
21
22
  prune: false
@@ -4,10 +4,8 @@ on: [push, pull_request, workflow_dispatch]
4
4
 
5
5
  env:
6
6
  FORCE_COLOR: 1
7
- PIP_DISABLE_PIP_VERSION_CHECK: 1
8
7
 
9
- permissions:
10
- contents: read
8
+ permissions: {}
11
9
 
12
10
  jobs:
13
11
  lint:
@@ -15,18 +13,24 @@ jobs:
15
13
 
16
14
  steps:
17
15
  - uses: actions/checkout@v4
16
+ with:
17
+ persist-credentials: false
18
18
  - uses: actions/setup-python@v5
19
19
  with:
20
20
  python-version: "3.x"
21
- cache: pip
22
- - uses: pre-commit/action@v3.0.1
21
+ - uses: tox-dev/action-pre-commit-uv@v1
23
22
 
24
23
  mypy:
25
24
  runs-on: ubuntu-latest
26
25
 
27
26
  steps:
28
27
  - uses: actions/checkout@v4
28
+ with:
29
+ persist-credentials: false
30
+ - uses: actions/setup-python@v5
31
+ with:
32
+ python-version: "3.x"
29
33
  - name: Install uv
30
- uses: hynek/setup-cached-uv@v2
34
+ uses: astral-sh/setup-uv@v5
31
35
  - name: Mypy
32
36
  run: uvx --with tox-uv tox -e mypy
@@ -14,9 +14,6 @@ on:
14
14
  # types: [opened, reopened, synchronize]
15
15
  workflow_dispatch:
16
16
 
17
- permissions:
18
- contents: read
19
-
20
17
  jobs:
21
18
  update_release_draft:
22
19
  if: github.repository_owner == 'prettytable'
@@ -8,8 +8,7 @@ on:
8
8
  - published
9
9
  workflow_dispatch:
10
10
 
11
- permissions:
12
- contents: read
11
+ permissions: {}
13
12
 
14
13
  env:
15
14
  FORCE_COLOR: 1
@@ -24,6 +23,7 @@ jobs:
24
23
  - uses: actions/checkout@v4
25
24
  with:
26
25
  fetch-depth: 0
26
+ persist-credentials: false
27
27
 
28
28
  - uses: hynek/build-and-inspect-python-package@v2
29
29
 
@@ -17,6 +17,11 @@ jobs:
17
17
  with:
18
18
  mode: minimum
19
19
  count: 1
20
- labels:
21
- "changelog: Added, changelog: Changed, changelog: Deprecated, changelog:
22
- Fixed, changelog: Removed, changelog: Security, changelog: skip"
20
+ labels: |
21
+ changelog: Added
22
+ changelog: Changed
23
+ changelog: Deprecated
24
+ changelog: Fixed
25
+ changelog: Removed
26
+ changelog: Security
27
+ changelog: skip
@@ -2,8 +2,7 @@ name: Test
2
2
 
3
3
  on: [push, pull_request, workflow_dispatch]
4
4
 
5
- permissions:
6
- contents: read
5
+ permissions: {}
7
6
 
8
7
  env:
9
8
  FORCE_COLOR: 1
@@ -14,7 +13,7 @@ jobs:
14
13
  strategy:
15
14
  fail-fast: false
16
15
  matrix:
17
- python-version: ["pypy3.10", "3.9", "3.10", "3.11", "3.12", "3.13"]
16
+ python-version: ["pypy3.10", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14"]
18
17
  os: [windows-latest, macos-latest, ubuntu-latest]
19
18
 
20
19
  steps:
@@ -29,18 +28,17 @@ jobs:
29
28
  allow-prereleases: true
30
29
 
31
30
  - name: Install uv
32
- uses: hynek/setup-cached-uv@v2
31
+ uses: astral-sh/setup-uv@v5
33
32
 
34
33
  - name: Tox tests
35
34
  run: |
36
35
  uvx --with tox-uv tox -e py
37
36
 
38
37
  - name: Upload coverage
39
- uses: codecov/codecov-action@v4
38
+ uses: codecov/codecov-action@v5
40
39
  with:
41
40
  flags: ${{ matrix.os }}
42
41
  name: ${{ matrix.os }} Python ${{ matrix.python-version }}
43
- token: ${{ secrets.CODECOV_TOKEN }}
44
42
 
45
43
  success:
46
44
  needs: test
@@ -1,12 +1,12 @@
1
1
  repos:
2
2
  - repo: https://github.com/astral-sh/ruff-pre-commit
3
- rev: v0.6.9
3
+ rev: v0.9.2
4
4
  hooks:
5
5
  - id: ruff
6
6
  args: [--exit-non-zero-on-fix]
7
7
 
8
8
  - repo: https://github.com/psf/black-pre-commit-mirror
9
- rev: 24.8.0
9
+ rev: 24.10.0
10
10
  hooks:
11
11
  - id: black
12
12
 
@@ -25,32 +25,38 @@ repos:
25
25
  exclude: .github/ISSUE_TEMPLATE/bug_report.md|tests/test_prettytable.py
26
26
 
27
27
  - repo: https://github.com/python-jsonschema/check-jsonschema
28
- rev: 0.29.3
28
+ rev: 0.31.0
29
29
  hooks:
30
+ - id: check-dependabot
30
31
  - id: check-github-workflows
31
32
 
32
33
  - repo: https://github.com/rhysd/actionlint
33
- rev: v1.7.3
34
+ rev: v1.7.7
34
35
  hooks:
35
36
  - id: actionlint
36
37
 
38
+ - repo: https://github.com/woodruffw/zizmor-pre-commit
39
+ rev: v1.2.2
40
+ hooks:
41
+ - id: zizmor
42
+
37
43
  - repo: https://github.com/tox-dev/pyproject-fmt
38
- rev: 2.2.4
44
+ rev: v2.5.0
39
45
  hooks:
40
46
  - id: pyproject-fmt
41
47
 
42
48
  - repo: https://github.com/abravalheri/validate-pyproject
43
- rev: v0.21
49
+ rev: v0.23
44
50
  hooks:
45
51
  - id: validate-pyproject
46
52
 
47
53
  - repo: https://github.com/tox-dev/tox-ini-fmt
48
- rev: 1.4.1
54
+ rev: 1.5.0
49
55
  hooks:
50
56
  - id: tox-ini-fmt
51
57
 
52
58
  - repo: https://github.com/rbubley/mirrors-prettier
53
- rev: v3.3.3
59
+ rev: v3.4.2
54
60
  hooks:
55
61
  - id: prettier
56
62
  args: [--prose-wrap=always, --print-width=88]
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.3
1
+ Metadata-Version: 2.4
2
2
  Name: prettytable
3
- Version: 3.12.0
3
+ Version: 3.14.0
4
4
  Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format
5
5
  Project-URL: Changelog, https://github.com/prettytable/prettytable/releases
6
6
  Project-URL: Funding, https://tidelift.com/subscription/pkg/pypi-prettytable?utm_source=pypi-prettytable&utm_medium=pypi
@@ -10,7 +10,6 @@ Author-email: Luke Maurits <luke@maurits.id.au>
10
10
  Maintainer: Hugo van Kemenade
11
11
  License-Expression: BSD-3-Clause
12
12
  License-File: LICENSE
13
- Classifier: License :: OSI Approved :: BSD License
14
13
  Classifier: Programming Language :: Python
15
14
  Classifier: Programming Language :: Python :: 3 :: Only
16
15
  Classifier: Programming Language :: Python :: 3.9
@@ -18,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.10
18
17
  Classifier: Programming Language :: Python :: 3.11
19
18
  Classifier: Programming Language :: Python :: 3.12
20
19
  Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
21
  Classifier: Programming Language :: Python :: Implementation :: CPython
22
22
  Classifier: Programming Language :: Python :: Implementation :: PyPy
23
23
  Classifier: Topic :: Text Processing
@@ -70,6 +70,14 @@ Or from `requirements.txt`:
70
70
 
71
71
  -e git://github.com/prettytable/prettytable.git#egg=prettytable
72
72
 
73
+ ## Demo
74
+
75
+ To see demo output, run:
76
+
77
+ ```bash
78
+ python3 -m prettytable
79
+ ```
80
+
73
81
  ## Tutorial on how to use the PrettyTable API
74
82
 
75
83
  ### Getting your data into (and out of) the table
@@ -415,14 +423,15 @@ instance of the data in the `sort_by` column.
415
423
 
416
424
  #### Adding sections to a table
417
425
 
418
- You can divide your table into different sections using the `divider` argument. This
419
- will add a dividing line into the table under the row who has this field set. So we can
420
- set up a table like this:
426
+ You can divide your table into different sections using the `add_divider` method or
427
+ `divider` argument . This will add a dividing line into the table under the row who has
428
+ this field set. So we can set up a table like this:
421
429
 
422
430
  ```python
423
431
  table = PrettyTable()
424
432
  table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
425
433
  table.add_row(["Adelaide", 1295, 1158259, 600.5])
434
+ table.add_divider()
426
435
  table.add_row(["Brisbane", 5905, 1857594, 1146.4])
427
436
  table.add_row(["Darwin", 112, 120900, 1714.7])
428
437
  table.add_row(["Hobart", 1357, 205556, 619.5], divider=True)
@@ -438,6 +447,7 @@ to get a table like this:
438
447
  | City name | Area | Population | Annual Rainfall |
439
448
  +-----------+------+------------+-----------------+
440
449
  | Adelaide | 1295 | 1158259 | 600.5 |
450
+ +-----------+------+------------+-----------------+
441
451
  | Brisbane | 5905 | 1857594 | 1146.4 |
442
452
  | Darwin | 112 | 120900 | 1714.7 |
443
453
  | Hobart | 1357 | 205556 | 619.5 |
@@ -464,8 +474,9 @@ You can set the style for your table using the `set_style` method before any cal
464
474
  `print` or `get_string`. Here's how to print a table in Markdown format:
465
475
 
466
476
  ```python
467
- from prettytable import MARKDOWN
468
- table.set_style(MARKDOWN)
477
+ from prettytable import TableStyle
478
+
479
+ table.set_style(TableStyle.MARKDOWN)
469
480
  print(table)
470
481
  ```
471
482
 
@@ -38,6 +38,14 @@ Or from `requirements.txt`:
38
38
 
39
39
  -e git://github.com/prettytable/prettytable.git#egg=prettytable
40
40
 
41
+ ## Demo
42
+
43
+ To see demo output, run:
44
+
45
+ ```bash
46
+ python3 -m prettytable
47
+ ```
48
+
41
49
  ## Tutorial on how to use the PrettyTable API
42
50
 
43
51
  ### Getting your data into (and out of) the table
@@ -383,14 +391,15 @@ instance of the data in the `sort_by` column.
383
391
 
384
392
  #### Adding sections to a table
385
393
 
386
- You can divide your table into different sections using the `divider` argument. This
387
- will add a dividing line into the table under the row who has this field set. So we can
388
- set up a table like this:
394
+ You can divide your table into different sections using the `add_divider` method or
395
+ `divider` argument . This will add a dividing line into the table under the row who has
396
+ this field set. So we can set up a table like this:
389
397
 
390
398
  ```python
391
399
  table = PrettyTable()
392
400
  table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
393
401
  table.add_row(["Adelaide", 1295, 1158259, 600.5])
402
+ table.add_divider()
394
403
  table.add_row(["Brisbane", 5905, 1857594, 1146.4])
395
404
  table.add_row(["Darwin", 112, 120900, 1714.7])
396
405
  table.add_row(["Hobart", 1357, 205556, 619.5], divider=True)
@@ -406,6 +415,7 @@ to get a table like this:
406
415
  | City name | Area | Population | Annual Rainfall |
407
416
  +-----------+------+------------+-----------------+
408
417
  | Adelaide | 1295 | 1158259 | 600.5 |
418
+ +-----------+------+------------+-----------------+
409
419
  | Brisbane | 5905 | 1857594 | 1146.4 |
410
420
  | Darwin | 112 | 120900 | 1714.7 |
411
421
  | Hobart | 1357 | 205556 | 619.5 |
@@ -432,8 +442,9 @@ You can set the style for your table using the `set_style` method before any cal
432
442
  `print` or `get_string`. Here's how to print a table in Markdown format:
433
443
 
434
444
  ```python
435
- from prettytable import MARKDOWN
436
- table.set_style(MARKDOWN)
445
+ from prettytable import TableStyle
446
+
447
+ table.set_style(TableStyle.MARKDOWN)
437
448
  print(table)
438
449
  ```
439
450
 
@@ -2,7 +2,7 @@
2
2
  build-backend = "hatchling.build"
3
3
  requires = [
4
4
  "hatch-vcs",
5
- "hatchling",
5
+ "hatchling>=1.27",
6
6
  ]
7
7
 
8
8
  [project]
@@ -10,11 +10,11 @@ name = "prettytable"
10
10
  description = "A simple Python library for easily displaying tabular data in a visually appealing ASCII table format"
11
11
  readme = "README.md"
12
12
  license = "BSD-3-Clause"
13
+ license-files = [ "LICENSE" ]
13
14
  maintainers = [ { name = "Hugo van Kemenade" } ]
14
15
  authors = [ { name = "Luke Maurits", email = "luke@maurits.id.au" } ]
15
16
  requires-python = ">=3.9"
16
17
  classifiers = [
17
- "License :: OSI Approved :: BSD License",
18
18
  "Programming Language :: Python",
19
19
  "Programming Language :: Python :: 3 :: Only",
20
20
  "Programming Language :: Python :: 3.9",
@@ -22,6 +22,7 @@ classifiers = [
22
22
  "Programming Language :: Python :: 3.11",
23
23
  "Programming Language :: Python :: 3.12",
24
24
  "Programming Language :: Python :: 3.13",
25
+ "Programming Language :: Python :: 3.14",
25
26
  "Programming Language :: Python :: Implementation :: CPython",
26
27
  "Programming Language :: Python :: Implementation :: PyPy",
27
28
  "Topic :: Text Processing",
@@ -55,9 +56,9 @@ fix = true
55
56
 
56
57
  lint.select = [
57
58
  "C4", # flake8-comprehensions
58
- "E", # pycodestyle errors
59
+ "E", # pycodestyle
59
60
  "EM", # flake8-errmsg
60
- "F", # pyflakes errors
61
+ "F", # pyflakes
61
62
  "I", # isort
62
63
  "ICN", # flake8-import-conventions
63
64
  "ISC", # flake8-implicit-str-concat
@@ -67,14 +68,15 @@ lint.select = [
67
68
  "RUF022", # unsorted-dunder-all
68
69
  "RUF100", # unused noqa (yesqa)
69
70
  "UP", # pyupgrade
70
- "W", # pycodestyle warnings
71
+ "W", # pycodestyle
71
72
  "YTT", # flake8-2020
72
73
  ]
73
74
  lint.ignore = [
74
- "E203", # Whitespace before ':'
75
- "E221", # Multiple spaces before operator
76
- "E226", # Missing whitespace around arithmetic operator
77
- "E241", # Multiple spaces after ','
75
+ "E203", # Whitespace before ':'
76
+ "E221", # Multiple spaces before operator
77
+ "E226", # Missing whitespace around arithmetic operator
78
+ "E241", # Multiple spaces after ','
79
+ "UP038", # Makes code slower and more verbose
78
80
  ]
79
81
  lint.flake8-import-conventions.aliases.datetime = "dt"
80
82
  lint.flake8-import-conventions.banned-from = [ "datetime" ]
@@ -82,7 +84,7 @@ lint.isort.known-first-party = [ "prettytable" ]
82
84
  lint.isort.required-imports = [ "from __future__ import annotations" ]
83
85
 
84
86
  [tool.pyproject-fmt]
85
- max_supported_python = "3.13"
87
+ max_supported_python = "3.14"
86
88
 
87
89
  [tool.pytest.ini_options]
88
90
  filterwarnings = [
@@ -0,0 +1,47 @@
1
+ from __future__ import annotations
2
+
3
+ from prettytable import PrettyTable, TableStyle
4
+ from prettytable.colortable import ColorTable, Theme, Themes
5
+
6
+ FIELD_NAMES = ["City name", "Area", "Population", "Annual Rainfall"]
7
+ ROWS = [
8
+ ["Adelaide", 1295, 1158259, 600.5],
9
+ ["Brisbane", 5905, 1857594, 1146.4],
10
+ ["Darwin", 112, 120900, 1714.7],
11
+ ["Hobart", 1357, 205556, 619.5],
12
+ ["Melbourne", 1566, 3806092, 646.9],
13
+ ["Perth", 5386, 1554769, 869.4],
14
+ ["Sydney", 2058, 4336374, 1214.8],
15
+ ]
16
+
17
+ if __name__ == "__main__":
18
+ table = PrettyTable()
19
+ table.field_names = FIELD_NAMES
20
+ for row in ROWS:
21
+ if row[0] == "Hobart":
22
+ table.add_row(row, divider=True)
23
+ else:
24
+ table.add_row(row)
25
+
26
+ for style in TableStyle:
27
+ print("PrettyTable style:", style.name)
28
+ print()
29
+ table.set_style(style)
30
+ print(table)
31
+ print()
32
+
33
+ table = ColorTable()
34
+ table.field_names = FIELD_NAMES
35
+ for row in ROWS:
36
+ if row[0] == "Hobart":
37
+ table.add_row(row, divider=True)
38
+ else:
39
+ table.add_row(row)
40
+
41
+ for name, theme in vars(Themes).items():
42
+ if isinstance(theme, Theme):
43
+ print("ColorTable theme:", name)
44
+ print()
45
+ table.theme = theme
46
+ print(table)
47
+ print()
@@ -12,5 +12,5 @@ __version__: str
12
12
  __version_tuple__: VERSION_TUPLE
13
13
  version_tuple: VERSION_TUPLE
14
14
 
15
- __version__ = version = '3.12.0'
16
- __version_tuple__ = version_tuple = (3, 12, 0)
15
+ __version__ = version = '3.14.0'
16
+ __version_tuple__ = version_tuple = (3, 14, 0)
@@ -276,11 +276,6 @@ class PrettyTable:
276
276
  self.custom_format = {}
277
277
  self._style = None
278
278
 
279
- if field_names:
280
- self.field_names = field_names
281
- else:
282
- self._widths: list[int] = []
283
-
284
279
  # Options
285
280
  self._options = [
286
281
  "title",
@@ -329,17 +324,26 @@ class PrettyTable:
329
324
  "escape_header",
330
325
  "escape_data",
331
326
  ]
327
+
328
+ self._none_format: dict[str, str | None] = {}
329
+ self._kwargs = {}
330
+ if field_names:
331
+ self.field_names = field_names
332
+ else:
333
+ self._widths: list[int] = []
334
+
332
335
  for option in self._options:
333
336
  if option in kwargs:
334
337
  self._validate_option(option, kwargs[option])
338
+ self._kwargs[option] = kwargs[option]
335
339
  else:
336
340
  kwargs[option] = None
341
+ self._kwargs[option] = None
337
342
 
338
343
  self._title = kwargs["title"] or None
339
344
  self._start = kwargs["start"] or 0
340
345
  self._end = kwargs["end"] or None
341
346
  self._fields = kwargs["fields"] or None
342
- self._none_format: dict[str, str | None] = {}
343
347
 
344
348
  if kwargs["header"] in (True, False):
345
349
  self._header = kwargs["header"]
@@ -372,15 +376,8 @@ class PrettyTable:
372
376
  self._escape_header = kwargs["escape_header"]
373
377
  else:
374
378
  self._escape_header = True
375
- # Column specific arguments, use property.setters
376
- self.align = kwargs["align"] or {}
377
- self.valign = kwargs["valign"] or {}
378
- self.max_width = kwargs["max_width"] or {}
379
- self.min_width = kwargs["min_width"] or {}
380
- self.int_format = kwargs["int_format"] or {}
381
- self.float_format = kwargs["float_format"] or {}
382
- self.custom_format = kwargs["custom_format"] or {}
383
- self.none_format = kwargs["none_format"] or {}
379
+
380
+ self._column_specific_args()
384
381
 
385
382
  self._min_table_width = kwargs["min_table_width"] or None
386
383
  self._max_table_width = kwargs["max_table_width"] or None
@@ -416,6 +413,22 @@ class PrettyTable:
416
413
  self._xhtml = kwargs["xhtml"] or False
417
414
  self._attributes = kwargs["attributes"] or {}
418
415
 
416
+ def _column_specific_args(self):
417
+ # Column specific arguments, use property.setters
418
+ for attr in (
419
+ "align",
420
+ "valign",
421
+ "max_width",
422
+ "min_width",
423
+ "int_format",
424
+ "float_format",
425
+ "custom_format",
426
+ "none_format",
427
+ ):
428
+ setattr(
429
+ self, attr, (self._kwargs[attr] or {}) if attr in self._kwargs else {}
430
+ )
431
+
419
432
  def _justify(self, text: str, width: int, align: AlignType) -> str:
420
433
  excess = width - _str_block_width(text)
421
434
  if align == "l":
@@ -761,6 +774,9 @@ class PrettyTable:
761
774
  if self._field_names:
762
775
  old_names = self._field_names[:]
763
776
  self._field_names = val
777
+
778
+ self._column_specific_args()
779
+
764
780
  if self._align and old_names:
765
781
  for old_name, new_name in zip(old_names, val):
766
782
  self._align[new_name] = self._align[old_name]
@@ -1441,10 +1457,9 @@ class PrettyTable:
1441
1457
  ##############################
1442
1458
 
1443
1459
  def set_style(self, style: TableStyle) -> None:
1460
+ self._set_default_style()
1444
1461
  self._style = style
1445
- if style == TableStyle.DEFAULT:
1446
- self._set_default_style()
1447
- elif style == TableStyle.MSWORD_FRIENDLY:
1462
+ if style == TableStyle.MSWORD_FRIENDLY:
1448
1463
  self._set_msword_style()
1449
1464
  elif style == TableStyle.PLAIN_COLUMNS:
1450
1465
  self._set_columns_style()
@@ -1458,12 +1473,11 @@ class PrettyTable:
1458
1473
  self._set_single_border_style()
1459
1474
  elif style == TableStyle.RANDOM:
1460
1475
  self._set_random_style()
1461
- else:
1476
+ elif style != TableStyle.DEFAULT:
1462
1477
  msg = "Invalid pre-set style"
1463
1478
  raise ValueError(msg)
1464
1479
 
1465
1480
  def _set_orgmode_style(self) -> None:
1466
- self._set_default_style()
1467
1481
  self.orgmode = True
1468
1482
 
1469
1483
  def _set_markdown_style(self) -> None:
@@ -1604,6 +1618,11 @@ class PrettyTable:
1604
1618
  del self._rows[row_index]
1605
1619
  del self._dividers[row_index]
1606
1620
 
1621
+ def add_divider(self) -> None:
1622
+ """Add a divider to the table"""
1623
+ if len(self._dividers) >= 1:
1624
+ self._dividers[-1] = True
1625
+
1607
1626
  def add_column(
1608
1627
  self,
1609
1628
  fieldname: str,
@@ -1646,8 +1665,8 @@ class PrettyTable:
1646
1665
  Arguments:
1647
1666
  fieldname - name of the field to contain the new column of data"""
1648
1667
  self._field_names.insert(0, fieldname)
1649
- self._align[fieldname] = self.align
1650
- self._valign[fieldname] = self.valign
1668
+ self._align[fieldname] = self._kwargs["align"] or "c"
1669
+ self._valign[fieldname] = self._kwargs["valign"] or "t"
1651
1670
  for i, row in enumerate(self._rows):
1652
1671
  row.insert(0, i + 1)
1653
1672
 
@@ -1990,7 +2009,7 @@ class PrettyTable:
1990
2009
  for row, divider in zip(formatted_rows[:-1], dividers[:-1]):
1991
2010
  lines.append(self._stringify_row(row, options, self._hrule))
1992
2011
  if divider:
1993
- lines.append(self._stringify_hrule(options, where="bottom_"))
2012
+ lines.append(self._stringify_hrule(options))
1994
2013
  if formatted_rows:
1995
2014
  lines.append(
1996
2015
  self._stringify_row(
@@ -2506,9 +2525,12 @@ class PrettyTable:
2506
2525
  if options["escape_header"]:
2507
2526
  field = escape(field)
2508
2527
 
2528
+ content = field.replace("\n", linebreak)
2509
2529
  lines.append(
2510
- ' <th style="padding-left: %dem; padding-right: %dem; text-align: center">%s</th>' # noqa: E501
2511
- % (lpad, rpad, field.replace("\n", linebreak))
2530
+ f' <th style="'
2531
+ f"padding-left: {lpad}em; "
2532
+ f"padding-right: {rpad}em; "
2533
+ f'text-align: center">{content}</th>'
2512
2534
  )
2513
2535
  lines.append(" </tr>")
2514
2536
  lines.append(" </thead>")
@@ -2536,15 +2558,13 @@ class PrettyTable:
2536
2558
  if options["escape_data"]:
2537
2559
  datum = escape(datum)
2538
2560
 
2561
+ content = datum.replace("\n", linebreak)
2539
2562
  lines.append(
2540
- ' <td style="padding-left: %dem; padding-right: %dem; text-align: %s; vertical-align: %s">%s</td>' # noqa: E501
2541
- % (
2542
- lpad,
2543
- rpad,
2544
- align,
2545
- valign,
2546
- datum.replace("\n", linebreak),
2547
- )
2563
+ f' <td style="'
2564
+ f"padding-left: {lpad}em; "
2565
+ f"padding-right: {rpad}em; "
2566
+ f"text-align: {align}; "
2567
+ f'vertical-align: {valign}">{content}</td>'
2548
2568
  )
2549
2569
  lines.append(" </tr>")
2550
2570
  lines.append(" </tbody>")
@@ -2856,7 +2876,7 @@ def from_html_one(html_code: str, **kwargs) -> PrettyTable:
2856
2876
 
2857
2877
  def _warn_deprecation(name: str, module_globals: dict[str, Any]) -> Any:
2858
2878
  if (val := module_globals.get(f"_DEPRECATED_{name}")) is None:
2859
- msg = f"module '{__name__}' has no attribute '{name}"
2879
+ msg = f"module '{__name__}' has no attribute '{name}'"
2860
2880
  raise AttributeError(msg)
2861
2881
  module_globals[name] = val
2862
2882
  if name in {"FRAME", "ALL", "NONE", "HEADER"}:
@@ -788,6 +788,40 @@ class TestSorting:
788
788
  """.strip()
789
789
  )
790
790
 
791
+ def test_sort_key_at_class_declaration(self) -> None:
792
+ # Test sorting by length of city name
793
+ def key(vals):
794
+ vals[0] = len(vals[0])
795
+ return vals
796
+
797
+ table = PrettyTable(
798
+ field_names=["City name", "Area", "Population", "Annual Rainfall"],
799
+ sortby="City name",
800
+ sort_key=key,
801
+ )
802
+ assert table.sort_key == key
803
+ table.add_row(["Adelaide", 1295, 1158259, 600.5])
804
+ table.add_row(["Brisbane", 5905, 1857594, 1146.4])
805
+ table.add_row(["Darwin", 112, 120900, 1714.7])
806
+ table.add_row(["Hobart", 1357, 205556, 619.5])
807
+ table.add_row(["Sydney", 2058, 4336374, 1214.8])
808
+ table.add_row(["Melbourne", 1566, 3806092, 646.9])
809
+ table.add_row(["Perth", 5386, 1554769, 869.4])
810
+ assert (
811
+ """+-----------+------+------------+-----------------+
812
+ | City name | Area | Population | Annual Rainfall |
813
+ +-----------+------+------------+-----------------+
814
+ | Perth | 5386 | 1554769 | 869.4 |
815
+ | Darwin | 112 | 120900 | 1714.7 |
816
+ | Hobart | 1357 | 205556 | 619.5 |
817
+ | Sydney | 2058 | 4336374 | 1214.8 |
818
+ | Adelaide | 1295 | 1158259 | 600.5 |
819
+ | Brisbane | 5905 | 1857594 | 1146.4 |
820
+ | Melbourne | 1566 | 3806092 | 646.9 |
821
+ +-----------+------+------------+-----------------+"""
822
+ == table.get_string().strip()
823
+ )
824
+
791
825
  def test_sort_slice(self) -> None:
792
826
  """Make sure sorting and slicing interact in the expected way"""
793
827
  table = PrettyTable(["Foo"])
@@ -800,6 +834,37 @@ class TestSorting:
800
834
  assert "10" not in oldstyle
801
835
  assert "20" in oldstyle
802
836
 
837
+ def test_sortby_at_class_declaration(self) -> None:
838
+ """
839
+ Fix #354 where initialization of a table with sortby fails
840
+ """
841
+ table = PrettyTable(
842
+ field_names=["City name", "Area", "Population", "Annual Rainfall"],
843
+ sortby="Area",
844
+ )
845
+ assert table.sortby == "Area"
846
+ table.add_row(["Adelaide", 1295, 1158259, 600.5])
847
+ table.add_row(["Brisbane", 5905, 1857594, 1146.4])
848
+ table.add_row(["Darwin", 112, 120900, 1714.7])
849
+ table.add_row(["Hobart", 1357, 205556, 619.5])
850
+ table.add_row(["Sydney", 2058, 4336374, 1214.8])
851
+ table.add_row(["Melbourne", 1566, 3806092, 646.9])
852
+ table.add_row(["Perth", 5386, 1554769, 869.4])
853
+ assert (
854
+ """+-----------+------+------------+-----------------+
855
+ | City name | Area | Population | Annual Rainfall |
856
+ +-----------+------+------------+-----------------+
857
+ | Darwin | 112 | 120900 | 1714.7 |
858
+ | Adelaide | 1295 | 1158259 | 600.5 |
859
+ | Hobart | 1357 | 205556 | 619.5 |
860
+ | Melbourne | 1566 | 3806092 | 646.9 |
861
+ | Sydney | 2058 | 4336374 | 1214.8 |
862
+ | Perth | 5386 | 1554769 | 869.4 |
863
+ | Brisbane | 5905 | 1857594 | 1146.4 |
864
+ +-----------+------+------------+-----------------+"""
865
+ == table.get_string().strip()
866
+ )
867
+
803
868
 
804
869
  @pytest.fixture(scope="function")
805
870
  def float_pt() -> PrettyTable:
@@ -1712,6 +1777,134 @@ class TestStyle:
1712
1777
  with pytest.raises(ValueError):
1713
1778
  t.set_style(HRuleStyle.ALL) # type: ignore[arg-type]
1714
1779
 
1780
+ @pytest.mark.parametrize(
1781
+ "original_style,style, expected",
1782
+ [
1783
+ pytest.param(
1784
+ TableStyle.MARKDOWN,
1785
+ TableStyle.DEFAULT,
1786
+ """
1787
+ +---+---------+---------+---------+
1788
+ | | Field 1 | Field 2 | Field 3 |
1789
+ +---+---------+---------+---------+
1790
+ | 1 | value 1 | value2 | value3 |
1791
+ | 4 | value 4 | value5 | value6 |
1792
+ | 7 | value 7 | value8 | value9 |
1793
+ +---+---------+---------+---------+
1794
+ """,
1795
+ id="DEFAULT",
1796
+ ),
1797
+ pytest.param(
1798
+ TableStyle.MSWORD_FRIENDLY,
1799
+ TableStyle.MARKDOWN,
1800
+ """
1801
+ | | Field 1 | Field 2 | Field 3 |
1802
+ | :-: | :-----: | :-----: | :-----: |
1803
+ | 1 | value 1 | value2 | value3 |
1804
+ | 4 | value 4 | value5 | value6 |
1805
+ | 7 | value 7 | value8 | value9 |
1806
+ """,
1807
+ id="MARKDOWN",
1808
+ ),
1809
+ pytest.param(
1810
+ TableStyle.MARKDOWN,
1811
+ TableStyle.MSWORD_FRIENDLY,
1812
+ """
1813
+ | | Field 1 | Field 2 | Field 3 |
1814
+ | 1 | value 1 | value2 | value3 |
1815
+ | 4 | value 4 | value5 | value6 |
1816
+ | 7 | value 7 | value8 | value9 |
1817
+ """,
1818
+ id="MSWORD_FRIENDLY",
1819
+ ),
1820
+ pytest.param(
1821
+ TableStyle.MARKDOWN,
1822
+ TableStyle.ORGMODE,
1823
+ """
1824
+ |---+---------+---------+---------|
1825
+ | | Field 1 | Field 2 | Field 3 |
1826
+ |---+---------+---------+---------|
1827
+ | 1 | value 1 | value2 | value3 |
1828
+ | 4 | value 4 | value5 | value6 |
1829
+ | 7 | value 7 | value8 | value9 |
1830
+ |---+---------+---------+---------|
1831
+ """,
1832
+ id="ORGMODE",
1833
+ ),
1834
+ pytest.param(
1835
+ TableStyle.MARKDOWN,
1836
+ TableStyle.PLAIN_COLUMNS,
1837
+ """
1838
+ Field 1 Field 2 Field 3
1839
+ 1 value 1 value2 value3
1840
+ 4 value 4 value5 value6
1841
+ 7 value 7 value8 value9
1842
+ """, # noqa: W291
1843
+ id="PLAIN_COLUMNS",
1844
+ ),
1845
+ pytest.param(
1846
+ TableStyle.MARKDOWN,
1847
+ TableStyle.RANDOM,
1848
+ """
1849
+ '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
1850
+ % 1 value 1 value2 value3%
1851
+ % 4 value 4 value5 value6%
1852
+ % 7 value 7 value8 value9%
1853
+ '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
1854
+ """,
1855
+ id="RANDOM",
1856
+ ),
1857
+ pytest.param(
1858
+ TableStyle.MARKDOWN,
1859
+ TableStyle.DOUBLE_BORDER,
1860
+ """
1861
+ ╔═══╦═════════╦═════════╦═════════╗
1862
+ ║ ║ Field 1 ║ Field 2 ║ Field 3 ║
1863
+ ╠═══╬═════════╬═════════╬═════════╣
1864
+ ║ 1 ║ value 1 ║ value2 ║ value3 ║
1865
+ ║ 4 ║ value 4 ║ value5 ║ value6 ║
1866
+ ║ 7 ║ value 7 ║ value8 ║ value9 ║
1867
+ ╚═══╩═════════╩═════════╩═════════╝
1868
+ """,
1869
+ id="DOUBLE_BORDER",
1870
+ ),
1871
+ pytest.param(
1872
+ TableStyle.MARKDOWN,
1873
+ TableStyle.SINGLE_BORDER,
1874
+ """
1875
+ ┌───┬─────────┬─────────┬─────────┐
1876
+ │ │ Field 1 │ Field 2 │ Field 3 │
1877
+ ├───┼─────────┼─────────┼─────────┤
1878
+ │ 1 │ value 1 │ value2 │ value3 │
1879
+ │ 4 │ value 4 │ value5 │ value6 │
1880
+ │ 7 │ value 7 │ value8 │ value9 │
1881
+ └───┴─────────┴─────────┴─────────┘
1882
+ """,
1883
+ id="SINGLE_BORDER",
1884
+ ),
1885
+ ],
1886
+ )
1887
+ def test_style_reset(self, original_style, style, expected) -> None:
1888
+ """
1889
+ Testing to ensure that default styling is reset between changes
1890
+ of styles on a PrettyTable
1891
+
1892
+ Args:
1893
+ style (str): Style to be used (Default, markdown, etc)
1894
+ expected (str): The expected format of style as a string representation
1895
+ """
1896
+ # Arrange
1897
+ t = helper_table()
1898
+ random.seed(1234)
1899
+
1900
+ # Act
1901
+ t.set_style(original_style)
1902
+ t.set_style(style)
1903
+
1904
+ # Assert
1905
+ result = t.get_string()
1906
+ assert result.strip() == expected.strip()
1907
+
1715
1908
  @pytest.mark.parametrize(
1716
1909
  "style, expected",
1717
1910
  [
@@ -2354,6 +2547,169 @@ class TestMaxTableWidth:
2354
2547
  +---+-----------------+---+-----------------+---+-----------------+""".strip()
2355
2548
  )
2356
2549
 
2550
+ def test_table_width_on_init_wo_columns(self) -> None:
2551
+ """See also #272"""
2552
+ table = PrettyTable(max_width=10)
2553
+ table.add_row(
2554
+ [
2555
+ "Lorem",
2556
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2557
+ "ipsum",
2558
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2559
+ "dolor",
2560
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2561
+ ]
2562
+ )
2563
+
2564
+ assert (
2565
+ table.get_string().strip()
2566
+ == """
2567
+ +---------+------------+---------+------------+---------+------------+
2568
+ | Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 |
2569
+ +---------+------------+---------+------------+---------+------------+
2570
+ | Lorem | Lorem | ipsum | Lorem | dolor | Lorem |
2571
+ | | ipsum | | ipsum | | ipsum |
2572
+ | | dolor sit | | dolor sit | | dolor sit |
2573
+ | | amet, | | amet, | | amet, |
2574
+ | | consetetur | | consetetur | | consetetur |
2575
+ | | sadipscing | | sadipscing | | sadipscing |
2576
+ | | elitr, sed | | elitr, sed | | elitr, sed |
2577
+ | | diam | | diam | | diam |
2578
+ +---------+------------+---------+------------+---------+------------+""".strip()
2579
+ )
2580
+
2581
+ def test_table_width_on_init_with_columns(self) -> None:
2582
+ """See also #272"""
2583
+ table = PrettyTable(
2584
+ ["Field 1", "Field 2", "Field 3", "Field 4", "Field 5", "Field 6"],
2585
+ max_width=10,
2586
+ )
2587
+ table.add_row(
2588
+ [
2589
+ "Lorem",
2590
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2591
+ "ipsum",
2592
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2593
+ "dolor",
2594
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2595
+ ]
2596
+ )
2597
+
2598
+ assert (
2599
+ table.get_string().strip()
2600
+ == """
2601
+ +---------+------------+---------+------------+---------+------------+
2602
+ | Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 |
2603
+ +---------+------------+---------+------------+---------+------------+
2604
+ | Lorem | Lorem | ipsum | Lorem | dolor | Lorem |
2605
+ | | ipsum | | ipsum | | ipsum |
2606
+ | | dolor sit | | dolor sit | | dolor sit |
2607
+ | | amet, | | amet, | | amet, |
2608
+ | | consetetur | | consetetur | | consetetur |
2609
+ | | sadipscing | | sadipscing | | sadipscing |
2610
+ | | elitr, sed | | elitr, sed | | elitr, sed |
2611
+ | | diam | | diam | | diam |
2612
+ +---------+------------+---------+------------+---------+------------+""".strip()
2613
+ )
2614
+
2615
+ def test_table_minwidth_on_init_with_columns(self) -> None:
2616
+ table = PrettyTable(["Field 1", "Field 2"], min_width=20)
2617
+ table.add_row(
2618
+ [
2619
+ "Lorem",
2620
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2621
+ ]
2622
+ )
2623
+
2624
+ assert (
2625
+ table.get_string().strip()
2626
+ == """+----------------------+--------------------------------------------------------------------+
2627
+ | Field 1 | Field 2 |
2628
+ +----------------------+--------------------------------------------------------------------+
2629
+ | Lorem | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam |
2630
+ +----------------------+--------------------------------------------------------------------+""" # noqa: E501
2631
+ )
2632
+
2633
+ def test_table_min_max_width_on_init_with_columns(self) -> None:
2634
+ table = PrettyTable(["Field 1", "Field 2"], min_width=20, max_width=40)
2635
+ table.add_row(
2636
+ [
2637
+ "Lorem",
2638
+ "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ",
2639
+ ]
2640
+ )
2641
+
2642
+ assert (
2643
+ table.get_string().strip()
2644
+ == """+----------------------+------------------------------------------+
2645
+ | Field 1 | Field 2 |
2646
+ +----------------------+------------------------------------------+
2647
+ | Lorem | Lorem ipsum dolor sit amet, consetetur |
2648
+ | | sadipscing elitr, sed diam |
2649
+ +----------------------+------------------------------------------+"""
2650
+ )
2651
+
2652
+ def test_table_float_formatting_on_init_wo_columns(self) -> None:
2653
+ """See also #243"""
2654
+ table = prettytable.PrettyTable(float_format="10.2")
2655
+ table.field_names = ["Metric", "Initial sol.", "Best sol."]
2656
+ table.add_rows([["foo", 1.0 / 3.0, 1.0 / 3.0]])
2657
+
2658
+ assert (
2659
+ table.get_string().strip()
2660
+ == """
2661
+ +--------+--------------+------------+
2662
+ | Metric | Initial sol. | Best sol. |
2663
+ +--------+--------------+------------+
2664
+ | foo | 0.33 | 0.33 |
2665
+ +--------+--------------+------------+""".strip()
2666
+ )
2667
+
2668
+ def test_table_formatted_html_autoindex(self) -> None:
2669
+ """See also #199"""
2670
+ table = PrettyTable(["Field 1", "Field 2", "Field 3"])
2671
+ for row in range(1, 3 * 3, 3):
2672
+ table.add_row(
2673
+ [f"value {row*100}", f"value {row+1*100}", f"value {row+2*100}"]
2674
+ )
2675
+ table.format = True
2676
+ table.add_autoindex("I")
2677
+
2678
+ assert (
2679
+ table.get_html_string().strip()
2680
+ == """
2681
+ <table frame="box" rules="cols">
2682
+ <thead>
2683
+ <tr>
2684
+ <th style="padding-left: 1em; padding-right: 1em; text-align: center">I</th>
2685
+ <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th>
2686
+ <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th>
2687
+ <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th>
2688
+ </tr>
2689
+ </thead>
2690
+ <tbody>
2691
+ <tr>
2692
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">1</td>
2693
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 100</td>
2694
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 101</td>
2695
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 201</td>
2696
+ </tr>
2697
+ <tr>
2698
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">2</td>
2699
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 400</td>
2700
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 104</td>
2701
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 204</td>
2702
+ </tr>
2703
+ <tr>
2704
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">3</td>
2705
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 700</td>
2706
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 107</td>
2707
+ <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 207</td>
2708
+ </tr>
2709
+ </tbody>
2710
+ </table>""".strip() # noqa: E501
2711
+ )
2712
+
2357
2713
  def test_max_table_width_wide_vrules_frame(self) -> None:
2358
2714
  table = PrettyTable()
2359
2715
  table.max_table_width = 52
@@ -2419,54 +2775,61 @@ class TestMaxTableWidth:
2419
2775
  )
2420
2776
 
2421
2777
 
2422
- class TestRowEndSection:
2423
- def test_row_end_section(self) -> None:
2778
+ class TestFields:
2779
+ def test_fields_at_class_declaration(self) -> None:
2780
+ table = PrettyTable(
2781
+ field_names=["City name", "Area", "Population", "Annual Rainfall"],
2782
+ fields=["City name", "Annual Rainfall"],
2783
+ )
2784
+ table.add_row(["Adelaide", 1295, 1158259, 600.5])
2785
+ table.add_row(["Brisbane", 5905, 1857594, 1146.4])
2786
+ table.add_row(["Darwin", 112, 120900, 1714.7])
2787
+ table.add_row(["Hobart", 1357, 205556, 619.5])
2788
+ table.add_row(["Sydney", 2058, 4336374, 1214.8])
2789
+ table.add_row(["Melbourne", 1566, 3806092, 646.9])
2790
+ table.add_row(["Perth", 5386, 1554769, 869.4])
2791
+ assert (
2792
+ """+-----------+-----------------+
2793
+ | City name | Annual Rainfall |
2794
+ +-----------+-----------------+
2795
+ | Adelaide | 600.5 |
2796
+ | Brisbane | 1146.4 |
2797
+ | Darwin | 1714.7 |
2798
+ | Hobart | 619.5 |
2799
+ | Sydney | 1214.8 |
2800
+ | Melbourne | 646.9 |
2801
+ | Perth | 869.4 |
2802
+ +-----------+-----------------+"""
2803
+ == table.get_string().strip()
2804
+ )
2805
+
2806
+ def test_fields(self) -> None:
2424
2807
  table = PrettyTable()
2425
- v = 1
2426
- for row in range(4):
2427
- if row % 2 == 0:
2428
- table.add_row(
2429
- [f"value {v}", f"value{v+1}", f"value{v+2}"], divider=True
2430
- )
2431
- else:
2432
- table.add_row(
2433
- [f"value {v}", f"value{v+1}", f"value{v+2}"], divider=False
2434
- )
2435
- v += 3
2436
- table.del_row(0)
2808
+ table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
2809
+ table.fields = ["City name", "Annual Rainfall"]
2810
+ table.add_row(["Adelaide", 1295, 1158259, 600.5])
2811
+ table.add_row(["Brisbane", 5905, 1857594, 1146.4])
2812
+ table.add_row(["Darwin", 112, 120900, 1714.7])
2813
+ table.add_row(["Hobart", 1357, 205556, 619.5])
2814
+ table.add_row(["Sydney", 2058, 4336374, 1214.8])
2815
+ table.add_row(["Melbourne", 1566, 3806092, 646.9])
2816
+ table.add_row(["Perth", 5386, 1554769, 869.4])
2437
2817
  assert (
2438
- table.get_string().strip()
2439
- == """
2440
- +----------+---------+---------+
2441
- | Field 1 | Field 2 | Field 3 |
2442
- +----------+---------+---------+
2443
- | value 4 | value5 | value6 |
2444
- | value 7 | value8 | value9 |
2445
- +----------+---------+---------+
2446
- | value 10 | value11 | value12 |
2447
- +----------+---------+---------+
2448
- """.strip()
2818
+ """+-----------+-----------------+
2819
+ | City name | Annual Rainfall |
2820
+ +-----------+-----------------+
2821
+ | Adelaide | 600.5 |
2822
+ | Brisbane | 1146.4 |
2823
+ | Darwin | 1714.7 |
2824
+ | Hobart | 619.5 |
2825
+ | Sydney | 1214.8 |
2826
+ | Melbourne | 646.9 |
2827
+ | Perth | 869.4 |
2828
+ +-----------+-----------------+"""
2829
+ == table.get_string().strip()
2449
2830
  )
2450
2831
 
2451
2832
 
2452
- class TestClearing:
2453
- def test_clear_rows(self, row_prettytable: PrettyTable) -> None:
2454
- t = helper_table()
2455
- t.add_row([0, "a", "b", "c"], divider=True)
2456
- t.clear_rows()
2457
- assert t.rows == []
2458
- assert t.dividers == []
2459
- assert t.field_names == ["", "Field 1", "Field 2", "Field 3"]
2460
-
2461
- def test_clear(self, row_prettytable: PrettyTable) -> None:
2462
- t = helper_table()
2463
- t.add_row([0, "a", "b", "c"], divider=True)
2464
- t.clear()
2465
- assert t.rows == []
2466
- assert t.dividers == []
2467
- assert t.field_names == []
2468
-
2469
-
2470
2833
  class TestPreservingInternalBorders:
2471
2834
  def test_internal_border_preserved(self) -> None:
2472
2835
  pt = helper_table()
@@ -0,0 +1,55 @@
1
+ from __future__ import annotations
2
+
3
+ from test_prettytable import helper_table
4
+
5
+ from prettytable import PrettyTable, TableStyle
6
+
7
+
8
+ class TestRowEndSection:
9
+ EXPECTED_RESULT = """
10
+ ┌──────────┬──────────┬──────────┐
11
+ │ Field 1 │ Field 2 │ Field 3 │
12
+ ├──────────┼──────────┼──────────┤
13
+ │ value 4 │ value 5 │ value 6 │
14
+ │ value 7 │ value 8 │ value 9 │
15
+ ├──────────┼──────────┼──────────┤
16
+ │ value 10 │ value 11 │ value 12 │
17
+ └──────────┴──────────┴──────────┘
18
+ """.strip()
19
+
20
+ def test_row_end_section_via_argument(self) -> None:
21
+ table = PrettyTable()
22
+ table.set_style(TableStyle.SINGLE_BORDER)
23
+ table.add_row(["value 4", "value 5", "value 6"])
24
+ table.add_row(["value 7", "value 8", "value 9"], divider=True)
25
+ table.add_row(["value 10", "value 11", "value 12"])
26
+
27
+ assert table.get_string().strip() == self.EXPECTED_RESULT
28
+
29
+ def test_row_end_section_via_method(self) -> None:
30
+ table = PrettyTable()
31
+ table.set_style(TableStyle.SINGLE_BORDER)
32
+ table.add_row(["value 4", "value 5", "value 6"])
33
+ table.add_row(["value 7", "value 8", "value 9"])
34
+ table.add_divider()
35
+ table.add_row(["value 10", "value 11", "value 12"])
36
+
37
+ assert table.get_string().strip() == self.EXPECTED_RESULT
38
+
39
+
40
+ class TestClearing:
41
+ def test_clear_rows(self) -> None:
42
+ t = helper_table()
43
+ t.add_row([0, "a", "b", "c"], divider=True)
44
+ t.clear_rows()
45
+ assert t.rows == []
46
+ assert t.dividers == []
47
+ assert t.field_names == ["", "Field 1", "Field 2", "Field 3"]
48
+
49
+ def test_clear(self) -> None:
50
+ t = helper_table()
51
+ t.add_row([0, "a", "b", "c"], divider=True)
52
+ t.clear()
53
+ assert t.rows == []
54
+ assert t.dividers == []
55
+ assert t.field_names == []
@@ -4,7 +4,7 @@ requires =
4
4
  env_list =
5
5
  lint
6
6
  mypy
7
- py{py3, 313, 312, 311, 310, 39}
7
+ py{py3, 314, 313, 312, 311, 310, 39}
8
8
 
9
9
  [testenv]
10
10
  extras =
@@ -23,7 +23,7 @@ commands =
23
23
  [testenv:lint]
24
24
  skip_install = true
25
25
  deps =
26
- pre-commit
26
+ pre-commit-uv
27
27
  pass_env =
28
28
  PRE_COMMIT_COLOR
29
29
  commands =
File without changes
File without changes
File without changes
File without changes
@@ -34,7 +34,6 @@ __all__ = [
34
34
  "ALL",
35
35
  "DEFAULT",
36
36
  "DOUBLE_BORDER",
37
- "SINGLE_BORDER",
38
37
  "FRAME",
39
38
  "HEADER",
40
39
  "MARKDOWN",
@@ -43,18 +42,19 @@ __all__ = [
43
42
  "ORGMODE",
44
43
  "PLAIN_COLUMNS",
45
44
  "RANDOM",
45
+ "SINGLE_BORDER",
46
46
  "HRuleStyle",
47
47
  "PrettyTable",
48
48
  "RowType",
49
49
  "TableHandler",
50
50
  "TableStyle",
51
51
  "VRuleStyle",
52
+ "__version__",
52
53
  "from_csv",
53
54
  "from_db_cursor",
54
55
  "from_html",
55
56
  "from_html_one",
56
57
  "from_json",
57
- "__version__",
58
58
  ]
59
59
 
60
60