bridgeobjects 0.1.34__tar.gz → 0.1.36__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 (112) hide show
  1. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/PKG-INFO +4 -3
  2. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/pyproject.toml +4 -3
  3. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/pyproject.toml.orig +4 -3
  4. bridgeobjects-0.1.36/src/bridgeobjects/__init__.py +37 -0
  5. bridgeobjects-0.1.36/src/bridgeobjects/_create_pbn_string.py +138 -0
  6. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/_create_rbn_string.py +21 -23
  7. bridgeobjects-0.1.36/src/bridgeobjects/_file_constants.py +51 -0
  8. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/_parse_lin_string.py +30 -30
  9. bridgeobjects-0.1.36/src/bridgeobjects/_parse_pbn_string.py +175 -0
  10. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/_parse_rbn_string.py +35 -31
  11. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/auction.py +24 -24
  12. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/board.py +78 -74
  13. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/call.py +23 -20
  14. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/card.py +33 -25
  15. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/constants.py +47 -18
  16. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/contract.py +131 -95
  17. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/denomination.py +26 -21
  18. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/event.py +13 -13
  19. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/file_operations.py +42 -16
  20. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/hand.py +15 -15
  21. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/2C.png +0 -0
  22. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/2D.png +0 -0
  23. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/2H.png +0 -0
  24. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/2S.png +0 -0
  25. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/3C.png +0 -0
  26. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/3D.png +0 -0
  27. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/3H.png +0 -0
  28. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/3S.png +0 -0
  29. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/4C.png +0 -0
  30. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/4D.png +0 -0
  31. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/4H.png +0 -0
  32. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/4S.png +0 -0
  33. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/5C.png +0 -0
  34. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/5D.png +0 -0
  35. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/5H.png +0 -0
  36. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/5S.png +0 -0
  37. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/6C.png +0 -0
  38. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/6D.png +0 -0
  39. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/6H.png +0 -0
  40. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/6S.png +0 -0
  41. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/7C.png +0 -0
  42. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/7D.png +0 -0
  43. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/7H.png +0 -0
  44. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/7S.png +0 -0
  45. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/8C.png +0 -0
  46. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/8D.png +0 -0
  47. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/8H.png +0 -0
  48. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/8S.png +0 -0
  49. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/9C.png +0 -0
  50. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/9D.png +0 -0
  51. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/9H.png +0 -0
  52. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/9S.png +0 -0
  53. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/AC.png +0 -0
  54. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/AD.png +0 -0
  55. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/AH.png +0 -0
  56. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/AS.png +0 -0
  57. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/JC.png +0 -0
  58. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/JD.png +0 -0
  59. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/JH.png +0 -0
  60. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/JS.png +0 -0
  61. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/KC.png +0 -0
  62. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/KD.png +0 -0
  63. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/KH.png +0 -0
  64. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/KS.png +0 -0
  65. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/QC.png +0 -0
  66. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/QD.png +0 -0
  67. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/QH.png +0 -0
  68. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/QS.png +0 -0
  69. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/TC.png +0 -0
  70. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/TD.png +0 -0
  71. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/TH.png +0 -0
  72. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/TS.png +0 -0
  73. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/aaa.zip +0 -0
  74. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/back.png +0 -0
  75. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/blank.png +0 -0
  76. bridgeobjects-0.1.36/src/bridgeobjects/images/cards/blue_back.png +0 -0
  77. bridgeobjects-0.1.36/src/bridgeobjects/images.py +14 -0
  78. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/suit.py +18 -14
  79. bridgeobjects-0.1.36/src/bridgeobjects/tests/test_auctions.py +122 -0
  80. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_boards.py +149 -142
  81. bridgeobjects-0.1.36/src/bridgeobjects/tests/test_calls.py +243 -0
  82. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_cards.py +65 -62
  83. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_contracts.py +39 -38
  84. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/rbn_output_file_1.rbn +1 -1
  85. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_denominations.py +20 -12
  86. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_events.py +6 -4
  87. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_hands.py +387 -212
  88. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_lin.py +47 -29
  89. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_pbn.py +79 -61
  90. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_rbn.py +90 -65
  91. bridgeobjects-0.1.36/src/bridgeobjects/tests/test_scores.py +541 -0
  92. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_suits.py +16 -15
  93. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_trick.py +22 -21
  94. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/trick.py +28 -33
  95. bridgeobjects-0.1.34/src/bridgeobjects/__init__.py +0 -39
  96. bridgeobjects-0.1.34/src/bridgeobjects/_create_pbn_string.py +0 -212
  97. bridgeobjects-0.1.34/src/bridgeobjects/_file_constants.py +0 -26
  98. bridgeobjects-0.1.34/src/bridgeobjects/_parse_pbn_string.py +0 -323
  99. bridgeobjects-0.1.34/src/bridgeobjects/tests/test_auctions.py +0 -101
  100. bridgeobjects-0.1.34/src/bridgeobjects/tests/test_calls.py +0 -242
  101. bridgeobjects-0.1.34/src/bridgeobjects/tests/test_scores.py +0 -460
  102. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/README.md +0 -0
  103. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/_version.py +0 -0
  104. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/suit_base.py +0 -0
  105. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/__init__.py +0 -0
  106. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/lin_input_file_1.lin +0 -0
  107. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/pbn_auction_1.pbn +0 -0
  108. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/pbn_auction_2.pbn +0 -0
  109. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/pbn_input_3.pbn +0 -0
  110. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/pbn_input_file_1.pbn +0 -0
  111. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/pbn_output_file_1.pbn +0 -0
  112. {bridgeobjects-0.1.34 → bridgeobjects-0.1.36}/src/bridgeobjects/tests/test_data/rbn_input_file_1.rbn +0 -0
@@ -1,14 +1,15 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: bridgeobjects
3
- Version: 0.1.34
3
+ Version: 0.1.36
4
4
  Summary: Objects used in bridge games, for example, board, hand, bid or a contract.
5
5
  Author: Jeff
6
- Author-email: Jeff <<jeffwatkins2000@gmail.com>>
6
+ Author-email: Jeff <jeffwatkins2000@gmail.com>
7
7
  Requires-Dist: iniconfig>=2.1.0
8
8
  Requires-Dist: packaging>=25.0
9
9
  Requires-Dist: pluggy>=1.6.0
10
10
  Requires-Dist: pygments>=2.19.2
11
- Requires-Python: ==3.13.*
11
+ Requires-Dist: python-dateutil>=4.0.0
12
+ Requires-Python: >=3.11
12
13
  Description-Content-Type: text/markdown
13
14
 
14
15
  # bridgeobjects
@@ -1,7 +1,7 @@
1
1
  [project]
2
2
  name = "bridgeobjects"
3
- version = "0.1.34"
4
- requires-python = "==3.13.*"
3
+ version = "0.1.36"
4
+ requires-python = ">=3.11"
5
5
  description = "Objects used in bridge games, for example, board, hand, bid or a contract."
6
6
  readme = "README.md"
7
7
  include = ["HISTORY.md"]
@@ -10,11 +10,12 @@ dependencies = [
10
10
  "packaging>=25.0",
11
11
  "pluggy>=1.6.0",
12
12
  "pygments>=2.19.2",
13
+ "python-dateutil>=4.0.0",
13
14
  ]
14
15
 
15
16
  [[project.authors]]
16
17
  name = "Jeff"
17
- email = "<jeffwatkins2000@gmail.com>"
18
+ email = "jeffwatkins2000@gmail.com"
18
19
 
19
20
  [dependency-groups]
20
21
  dev = ["pytest"]
@@ -1,10 +1,10 @@
1
1
  [project]
2
2
  name = "bridgeobjects"
3
- version = "0.1.34"
4
- requires-python = "==3.13.*"
3
+ version = "0.1.36"
4
+ requires-python = ">=3.11"
5
5
  description = "Objects used in bridge games, for example, board, hand, bid or a contract."
6
6
  authors = [
7
- { name = "Jeff", email = "<jeffwatkins2000@gmail.com>" },
7
+ { name = "Jeff", email = "jeffwatkins2000@gmail.com" },
8
8
  ]
9
9
  readme = "README.md"
10
10
  include = [
@@ -15,6 +15,7 @@ dependencies = [
15
15
  "packaging>=25.0",
16
16
  "pluggy>=1.6.0",
17
17
  "pygments>=2.19.2",
18
+ "python-dateutil>=4.0.0",
18
19
  ]
19
20
 
20
21
  [dependency-groups]
@@ -0,0 +1,37 @@
1
+ """
2
+ The bridgeobjects package exposes the following classes:
3
+ (inspect the individual modules to see their APIs):
4
+
5
+ constants: various constants used throughout the package,
6
+ e.g. SEATS = ['N', 'E', 'S', 'W'];
7
+ board: represents a board of 4 hands;
8
+ hand: holds the 13 cards dealt to the hand;
9
+ card: a single card in a hand, board or trick;
10
+ suit: one of Spades, Hearts, Clubs or diamonds;
11
+ denomination: the name of a call in the auction or contract,
12
+ it includes the four suits and NT;
13
+ auction: the calls made on a particular board
14
+ contract: the contract reach on a board;
15
+ call: an individual call made to reach a contract
16
+ event: the match or competition at which the boards are played:
17
+ trick: four cards played which one pair wins:
18
+ file_operations: a module to load or save events in a PBN,
19
+ RBN or LIN format.
20
+ """
21
+
22
+ from bridgeobjects._version import __version__
23
+ from bridgeobjects.auction import *
24
+ from bridgeobjects.board import *
25
+ from bridgeobjects.call import *
26
+ from bridgeobjects.card import *
27
+ from bridgeobjects.constants import *
28
+ from bridgeobjects.contract import *
29
+ from bridgeobjects.denomination import *
30
+ from bridgeobjects.event import *
31
+ from bridgeobjects.file_operations import *
32
+ from bridgeobjects.hand import *
33
+ from bridgeobjects.images import *
34
+ from bridgeobjects.suit import *
35
+ from bridgeobjects.trick import *
36
+
37
+ VERSION = __version__
@@ -0,0 +1,138 @@
1
+ """Processes to create a pbn event."""
2
+
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING
5
+
6
+ if TYPE_CHECKING:
7
+ from bridgeobjects.board import Board
8
+
9
+ from bridgeobjects._file_constants import (
10
+ PBN_CALL_CONVERSION,
11
+ PBN_CONTENT_TEXT,
12
+ PBN_CREATOR_TEXT,
13
+ PBN_FORMAT_TEXT,
14
+ PBN_VERSION_TEXT,
15
+ )
16
+ from bridgeobjects.constants import PBN_FILE_HEADER, PBN_STRING_FIELDS, SEATS
17
+ from bridgeobjects.event import Event
18
+
19
+ if TYPE_CHECKING:
20
+ from bridgeobjects.board import Board
21
+
22
+
23
+ def create_pbn_event_list(events: list[Event], path: Path | str = "") -> list[str]:
24
+ """Create a list of strings for events in pbn format."""
25
+ output = []
26
+ output = PBN_FILE_HEADER
27
+ if path:
28
+ if not path.is_file():
29
+ return PBN_FILE_HEADER
30
+ with open(path, "r") as pbn_file:
31
+ for line in pbn_file:
32
+ line = line.replace("\n", "")
33
+ if len(line.strip()) > 0 and line[0] != "%":
34
+ output = PBN_FILE_HEADER
35
+ break
36
+ contents = pbn_file.readlines()
37
+ if len(contents) == 0:
38
+ output = PBN_FILE_HEADER
39
+
40
+ for event in events:
41
+ output.extend(_create_pbn_event(event))
42
+ return output
43
+
44
+
45
+ def _create_pbn_event(event: Event) -> str:
46
+ """Return an event as a list of strings in pbn format."""
47
+ output = PBN_FILE_HEADER
48
+ for board in event.boards:
49
+ output.extend(_build_pbn_output(board, event.name))
50
+ return "\n".join(output)
51
+
52
+
53
+ def PBN_FILE_HEADER() -> list[str]:
54
+ """Create a list containing the pbn file header."""
55
+ return [
56
+ PBN_VERSION_TEXT,
57
+ PBN_FORMAT_TEXT,
58
+ PBN_CONTENT_TEXT,
59
+ PBN_CREATOR_TEXT,
60
+ "",
61
+ ]
62
+
63
+
64
+ def create_pbn_board(board: "Board") -> str:
65
+ return _build_pbn_output(board)
66
+ # board_list = _create_pbn_board_list(board)
67
+ # return "\n".join(board_list)
68
+
69
+
70
+ def _build_pbn_output(board: "Board", event: str = "") -> str:
71
+ output = []
72
+ for key, field in PBN_STRING_FIELDS.items():
73
+ # Has to come before the hasattr check
74
+ if key == "Event" and event:
75
+ output.append(f'[{key} "{event}"]')
76
+ continue
77
+ if key == "Deal" and board.hands:
78
+ output.append(f'[{key} "{_get_deal(board)}"]')
79
+ continue
80
+ if not hasattr(board, field):
81
+ continue
82
+ value = getattr(board, field)
83
+ if not value:
84
+ continue
85
+ if key == "Contract":
86
+ value = board.contract.name
87
+ output.append(f'[{key} "{value}"]')
88
+
89
+ if board.auction.calls:
90
+ output.extend(_get_auction(board))
91
+
92
+ if board.tricks and board.tricks[0].cards:
93
+ output.extend(_get_play(board))
94
+ return "\n".join(output)
95
+
96
+
97
+ def _get_auction(board: "Board") -> list[str]:
98
+ auction = [f'[Auction "{board.dealer}"]']
99
+ calls = []
100
+ for call in board.auction.calls:
101
+ call_name = call.name
102
+ if call_name in PBN_CALL_CONVERSION:
103
+ call_name = PBN_CALL_CONVERSION[call_name]
104
+ calls.append(call_name)
105
+ if len(calls) == 4:
106
+ auction.append(" ".join(calls))
107
+ calls = []
108
+
109
+ auction.append(" ".join(calls))
110
+ return auction
111
+
112
+
113
+ def _get_play(board: "Board") -> list[str]:
114
+ play = [f'[Play "{board.declarer}"]']
115
+ cards: list[str] = []
116
+ for trick in board.tricks:
117
+ for card in trick.cards:
118
+ card_name = card.name[1] + card.name[0]
119
+ cards.append(card_name)
120
+ if len(cards) == 4:
121
+ play.append(" ".join(cards))
122
+ cards = []
123
+ play.append(" ".join(cards))
124
+ return play
125
+
126
+
127
+ def _get_deal(board: "Board") -> str:
128
+ hands_str = f"{board.dealer}:{get_pbn_hands_str(board)}"
129
+ return hands_str
130
+
131
+
132
+ def get_pbn_hands_str(board: "Board", delimiter: str = " ") -> str:
133
+ dealer_index = SEATS.index(board.dealer)
134
+ hands_str = ""
135
+ for index in range(4):
136
+ hand_index = (dealer_index + index) % 4
137
+ hands_str += f"{board.hands[hand_index].pbn_string}{delimiter}"
138
+ return hands_str.strip(delimiter)
@@ -1,12 +1,10 @@
1
-
2
- from .event import Event
3
- from ._file_constants import (PBN_VULNERABILITY_CONVERSION,
4
- PBN_CALL_CONVERSION,
5
- PBN_VERSION_TEXT, PBN_FORMAT_TEXT,
6
- PBN_CONTENT_TEXT, PBN_CREATOR_TEXT,
7
- RBN_VERSION_TEXT, RBN_CREATOR_TEXT)
8
-
9
- from ._create_pbn_string import get_pbn_deal
1
+ from bridgeobjects._create_pbn_string import get_pbn_hands_str
2
+ from bridgeobjects._file_constants import (
3
+ PBN_VULNERABILITY_CONVERSION,
4
+ RBN_CREATOR_TEXT,
5
+ RBN_VERSION_TEXT,
6
+ )
7
+ from bridgeobjects.event import Event
10
8
 
11
9
 
12
10
  def create_rbn_list(events: list[Event]) -> list[str]:
@@ -19,11 +17,11 @@ def create_rbn_list(events: list[Event]) -> list[str]:
19
17
 
20
18
  def _create_rbn_event(event: Event) -> list[str]:
21
19
  """Return an event as string in rbn format as a list."""
22
- output = [f'E {event.name}', f'L {event.site}']
20
+ output = [f"E {event.name}", f"L {event.site}"]
23
21
  if event.date:
24
- event_date = event.date.strftime('%Y%m%d')
25
- output.append(f'D {event_date}')
26
- output.append(f'M {_question_from_null(event.scoring_method)}')
22
+ event_date = event.date.strftime("%Y%m%d")
23
+ output.append(f"D {event_date}")
24
+ output.append(f"M {_question_from_null(event.scoring_method)}")
27
25
  for board in event.boards:
28
26
  output.extend(_create_rbn_board(board))
29
27
  return output
@@ -31,37 +29,37 @@ def _create_rbn_event(event: Event) -> list[str]:
31
29
 
32
30
  def _create_rbn_board(board: "Board") -> list[str]:
33
31
  """Return a board as a list of strings in pbn format."""
34
- output = [f'B {board.number}']
32
+ output = [f"B {board.number}"]
35
33
 
36
34
  if names := _create_rbn_names(board):
37
- output.append(f'N {names}')
35
+ output.append(f"N {names}")
38
36
 
39
37
  # hands
40
- deal = get_pbn_deal(board, ':')
41
- output.append(f'H {board.dealer}:{deal}')
38
+ deal = get_pbn_hands_str(board, ":")
39
+ output.append(f"H {board.dealer}:{deal}")
42
40
 
43
41
  # auction
44
42
  vulnerable = PBN_VULNERABILITY_CONVERSION[board.vulnerable]
45
- output.append(f'A {board.dealer}{vulnerable}:')
43
+ output.append(f"A {board.dealer}{vulnerable}:")
46
44
 
47
45
  # contract
48
46
  board_contract = _question_from_null(board.contract.name)
49
47
  board_declarer = _question_from_null(board.declarer)
50
- contract = ':'.join([board_contract, board_declarer])
48
+ contract = ":".join([board_contract, board_declarer])
51
49
  if contract != "?:?":
52
- output.append(f'C {contract}')
50
+ output.append(f"C {contract}")
53
51
 
54
52
  # result
55
53
  result = _question_from_null(board.result)
56
54
  if result != "?":
57
- output.append('[R result')
58
- output.append('')
55
+ output.append("[R result")
56
+ output.append("")
59
57
  return output
60
58
 
61
59
 
62
60
  def _create_rbn_names(board: "Board") -> str:
63
61
  """Return the boards players in rbn format."""
64
- names = f'{board.north}+{board.south}:{board.west}+{board.east}'
62
+ names = f"{board.north}+{board.south}:{board.west}+{board.east}"
65
63
  if names == "+:+":
66
64
  names = ""
67
65
  if names[:3] == "+:":
@@ -0,0 +1,51 @@
1
+ """Constants for PBN and LIN files conversions."""
2
+
3
+ import datetime
4
+
5
+ PBN_VULNERABILITY_CONVERSION = {
6
+ "Z": "None",
7
+ "N": "NS",
8
+ "E": "EW",
9
+ "B": "Both",
10
+ "?": "",
11
+ "None": "Z",
12
+ "NS": "N",
13
+ "EW": "E",
14
+ "Both": "B",
15
+ "": "?",
16
+ }
17
+
18
+ LIN_VULNERABILITY_CONVERSION = {
19
+ "O": "None",
20
+ "N": "NS",
21
+ "E": "EW",
22
+ "B": "Both",
23
+ "?": "",
24
+ "None": "O",
25
+ "NS": "N",
26
+ "EW": "E",
27
+ "Both": "B",
28
+ "": "?",
29
+ }
30
+
31
+ PBN_CALL_CONVERSION = {
32
+ "P": "Pass",
33
+ "D": "X",
34
+ "R": "XX",
35
+ "A": "AP",
36
+ "Pass": "P",
37
+ "X": "D",
38
+ "XX": "R",
39
+ "AP": "A",
40
+ }
41
+
42
+ PBN_VERSION_TEXT = "% PBN 2.1"
43
+ PBN_FORMAT_TEXT = "% EXPORT"
44
+ PBN_CONTENT_TEXT = "% Content-type: text/pbn; charset=ISO-8859-1"
45
+ PBN_CREATOR_TEXT = "% Creator: bridgeobjects"
46
+
47
+ RBN_VERSION_TEXT = "% RBN version 3.2"
48
+ RBN_CREATOR_TEXT = "% Created by bridgeobjects"
49
+
50
+ DEFAULT_EVENT_NAME = "BfG default event name"
51
+ DEFAULT_EVENT_DATE = datetime.date(1980, 1, 1)
@@ -1,31 +1,31 @@
1
1
  """
2
- Helper functions for parsing lin strings.
2
+ Helper functions for parsing lin strings.
3
3
 
4
- The lin format is proprietary see
5
- (https://www.bridgebase.com/forums/topic/46671-lin-file-format-description/)
4
+ The lin format is proprietary see
5
+ (https://www.bridgebase.com/forums/topic/46671-lin-file-format-description/)
6
6
 
7
- But this Ruby program
8
- (https://github.com/morgoth/lin/commit/2b75aa74a20cb8f872d454309f2f87af39292cc9)
9
- demonstrates how to parse a played board.
7
+ But this Ruby program
8
+ (https://github.com/morgoth/lin/commit/2b75aa74a20cb8f872d454309f2f87af39292cc9)
9
+ demonstrates how to parse a played board.
10
10
 
11
11
 
12
- The parse_lin function is intended to return the event from a lin file
13
- containing the boards for a tournament.
12
+ The parse_lin function is intended to return the event from a lin file
13
+ containing the boards for a tournament.
14
14
 
15
- The attributes seem to be contained in a pair of list elements,
16
- the first being an identifier, the second the value
17
- e.g.|sv|N| = {vulnerable: 'NS')
15
+ The attributes seem to be contained in a pair of list elements,
16
+ the first being an identifier, the second the value
17
+ e.g.|sv|N| = {vulnerable: 'NS')
18
18
 
19
- The elements for a board all being on one line.
19
+ The elements for a board all being on one line.
20
20
  """
21
21
 
22
- from .event import Event
23
- from .board import Board
24
- from .suit import Suit
25
- from .hand import Hand
26
- from .card import Card
27
- from .constants import SEATS
28
- from ._file_constants import LIN_VULNERABILITY_CONVERSION
22
+ from bridgeobjects._file_constants import LIN_VULNERABILITY_CONVERSION
23
+ from bridgeobjects.board import Board
24
+ from bridgeobjects.card import Card
25
+ from bridgeobjects.constants import SEATS
26
+ from bridgeobjects.event import Event
27
+ from bridgeobjects.hand import Hand
28
+ from bridgeobjects.suit import Suit
29
29
 
30
30
 
31
31
  # TODO test for lin event
@@ -38,12 +38,12 @@ def parse_lin(file_text: list[str]) -> list[Event]:
38
38
  board = None
39
39
  for line in file_text:
40
40
  if line:
41
- line_list = line.split('|')
41
+ line_list = line.split("|")
42
42
  identifiers, values = line_list[::2], line_list[1::2]
43
43
  for identifier, value in zip(identifiers, values):
44
- if identifier == 'bn':
45
- board_numbers = value.split(',')
46
- elif identifier == 'qx':
44
+ if identifier == "bn":
45
+ board_numbers = value.split(",")
46
+ elif identifier == "qx":
47
47
  board = _parse_lin_board(identifiers, values)
48
48
  event.boards.append(board)
49
49
  return events
@@ -53,13 +53,13 @@ def _parse_lin_board(identifiers: list[str], values: list[str]) -> Board:
53
53
  """Return a board object from a lin line."""
54
54
  board = Board()
55
55
  for identifier, value in zip(identifiers, values):
56
- if identifier == 'ah':
56
+ if identifier == "ah":
57
57
  board.identifier = value
58
- elif identifier == 'md':
58
+ elif identifier == "md":
59
59
  board.hands = _parse_lin_hands(value)
60
60
  dealer_index = (int(value[0]) - 3) % 4
61
61
  board.dealer = SEATS[dealer_index]
62
- elif identifier == 'sv':
62
+ elif identifier == "sv":
63
63
  board.vulnerable = LIN_VULNERABILITY_CONVERSION[value]
64
64
  return board
65
65
 
@@ -67,14 +67,14 @@ def _parse_lin_board(identifiers: list[str], values: list[str]) -> Board:
67
67
  def _parse_lin_hands(hands_string: str) -> dict[object, Hand]:
68
68
  """Return a dict of hands from lin text."""
69
69
  hands: dict[object, Hand] = {}
70
- dealer_index = (int(hands_string[0]))
70
+ dealer_index = int(hands_string[0])
71
71
  hand_index = (2 - dealer_index) % 4
72
- for hand_string in hands_string[1:].split(','):
72
+ for hand_string in hands_string[1:].split(","):
73
73
  hand = Hand()
74
74
  for character in hand_string:
75
75
  if character in Suit.SHORT_NAMES:
76
76
  suit_name = character
77
- elif character == ',':
77
+ elif character == ",":
78
78
  hands[hand_index] = hand
79
79
  hands[SEATS[hand_index]] = hand
80
80
  hand = Hand()
@@ -88,7 +88,7 @@ def _parse_lin_hands(hands_string: str) -> dict[object, Hand]:
88
88
  hand_index %= 4
89
89
 
90
90
  hand = Hand()
91
- hand = Board.build_fourth_hand(hands)
91
+ hand = Board().build_fourth_hand(hands)
92
92
  hands[hand_index] = hand
93
93
  hands[SEATS[hand_index]] = hand
94
94
  return hands
@@ -0,0 +1,175 @@
1
+ """Helper functions for parsing pbn strings."""
2
+
3
+ import re
4
+
5
+ from dateutil import parser
6
+
7
+ from bridgeobjects.auction import Auction
8
+ from bridgeobjects.board import Board
9
+ from bridgeobjects.call import Call
10
+ from bridgeobjects.card import Card
11
+ from bridgeobjects.constants import PBN_STRING_FIELDS, SEATS
12
+ from bridgeobjects.contract import Contract
13
+ from bridgeobjects.event import Event
14
+ from bridgeobjects.hand import Hand
15
+ from bridgeobjects.trick import Trick
16
+
17
+ TAG_PATTERN = re.compile(r'^\[(\w+)\s+"(.*)"\]$')
18
+
19
+ PBN_CALL_CONVERSION = {
20
+ "P": "Pass",
21
+ "D": "X",
22
+ "R": "XX",
23
+ "A": "AP",
24
+ "Pass": "P",
25
+ "X": "D",
26
+ "XX": "R",
27
+ "AP": "A",
28
+ }
29
+ HEADER_TAGS = ("Event", "Site", "Date")
30
+ BOARD_START_TAGS = (*HEADER_TAGS, "Board")
31
+ SECTION_TAGS = ("Auction", "Play", "Note", "OptimumResultTable")
32
+
33
+
34
+ def parse_pbn(pbn_strs: str) -> list[Event]:
35
+ """Populate bridgeobjects objects from a list of pbn strings and
36
+ return a list of events."""
37
+ event_store = {}
38
+ events = []
39
+
40
+ boards = _parse_pbn_list(pbn_strs)
41
+ for board in boards:
42
+ board = _create_board_from_pbn(board)
43
+ if board.event not in event_store:
44
+ event = Event(board.event)
45
+ event_store[board.event] = event
46
+ events.append(event)
47
+ event.boards.append(board)
48
+ event.site = board.site
49
+ if board.date:
50
+ event.date = board.date
51
+ return events
52
+
53
+
54
+ def _parse_pbn_list(pbn_strs: list[str]) -> list[dict]:
55
+ """Parse a list of pbn string into a list of board dictionaries."""
56
+ boards: list[dict] = []
57
+ current_board: dict = {}
58
+ current_section = None
59
+
60
+ for pbn_str in pbn_strs:
61
+ line = pbn_str.strip()
62
+ if not line or line.startswith("%"):
63
+ continue
64
+
65
+ match = TAG_PATTERN.match(line)
66
+ if match:
67
+ tag, value = match.groups()
68
+ if tag in BOARD_START_TAGS and "Board" in current_board:
69
+ boards.append(current_board)
70
+ current_board = _next_board(current_board, tag)
71
+ current_board[tag] = value
72
+ current_section = tag if tag in SECTION_TAGS else None
73
+ elif current_section:
74
+ key = f"{current_section}_data"
75
+ current_board.setdefault(key, []).append(line)
76
+
77
+ if current_board:
78
+ boards.append(current_board)
79
+ return boards
80
+
81
+
82
+ def _next_board(previous: dict, start_tag: str) -> dict:
83
+ """Return the starting dict for a new board.
84
+
85
+ If the board begins at its own Event/Site/Date tags, start afresh.
86
+ If it begins at Board, the headers were given once, so carry them over.
87
+ """
88
+ if start_tag != "Board":
89
+ return {}
90
+ return {tag: previous[tag] for tag in HEADER_TAGS if tag in previous}
91
+
92
+
93
+ def _create_board_from_pbn(data: dict) -> Board:
94
+ board = Board()
95
+ board.contract = Contract()
96
+ for key, value in data.items():
97
+ if value == "?":
98
+ continue
99
+
100
+ if key == "Deal":
101
+ board.hands = _get_hands(value)
102
+
103
+ elif key == "Contract":
104
+ contract = value
105
+ board.contract = Contract(contract)
106
+
107
+ elif key == "Auction" and value:
108
+ board.auction = _get_auction(data.get("Auction_data", []))
109
+ board.auction.first_caller = board.declarer
110
+
111
+ elif key == "Play" and value:
112
+ board.tricks = _get_play(
113
+ data.get("Play_data", []),
114
+ board.declarer,
115
+ )
116
+
117
+ elif key in PBN_STRING_FIELDS and value:
118
+ if key == "Date":
119
+ value = parser.parse(value, dayfirst=True).date()
120
+ setattr(board, PBN_STRING_FIELDS[key], value)
121
+ return board
122
+
123
+
124
+ def _get_hands(deal_str: str) -> dict[object, Hand]:
125
+ """Return a list of hands from a pbn deal string."""
126
+ hands: dict[object, Hand] = {}
127
+ if not deal_str:
128
+ return hands
129
+
130
+ raw_hands = deal_str[2:].split(" ")
131
+ first_seat = SEATS.index(deal_str[0])
132
+ for index, card_list in enumerate(raw_hands):
133
+ seat_index = (first_seat + index) % 4
134
+ seat_name = SEATS[seat_index]
135
+ hand = Hand(card_list)
136
+ hands[seat_index] = hand
137
+ hands[seat_name] = hand
138
+ return hands
139
+
140
+
141
+ def _get_auction(auction_data: list[str]) -> Auction:
142
+ """Return a list of auction strings from a pbn auction string."""
143
+ auction = Auction()
144
+ for line in auction_data:
145
+ calls = line.split(" ")
146
+ for call in calls:
147
+ if call == "A":
148
+ all_pass = [Call("P") for _ in range(4)]
149
+ auction.calls.extend(all_pass)
150
+ break
151
+ if call in PBN_CALL_CONVERSION:
152
+ call = PBN_CALL_CONVERSION[call]
153
+ auction.calls.append(Call(call))
154
+ return auction
155
+
156
+
157
+ def _get_play(
158
+ play_data: list[str], trump_suit: str = "", declarer: str = ""
159
+ ) -> list[Trick]:
160
+ """Return a list of play strings from a pbn play string."""
161
+ if not declarer:
162
+ return []
163
+ tricks = []
164
+ trick_leader = SEATS[(SEATS.index(declarer) + 3) % 4]
165
+ for line in play_data:
166
+ cards = line.split(" ")
167
+ trick = Trick()
168
+ trick.leader = trick_leader
169
+ for card in cards:
170
+ trick.cards.append(Card(card))
171
+ if len(trick.cards) == 4:
172
+ trick.complete(trump_suit)
173
+ trick.leader = trick.winner
174
+ tricks.append(trick)
175
+ return tricks