1import re
2
3def parse_wishlist(wishlist):
4 pattern = r'\((\d+(\.\d+)?)\)\s+([^\[]+)\s+\[(\d+)[^\]]*\]\s*\n((?:\s*[-*] [^\n]*\n?)*)'
5
6 matches = re.findall(pattern, wishlist)
7
8 all_whishes = []
9 for match in matches:
10 obedience, _, name, age, wishes = match
11 obedience = float(obedience)
12 age = int(age)
13 wishes_list = tuple(wish.strip(' -*') for wish in wishes.split('\n') if wish.strip(' -*'))
14 all_whishes.append((obedience, name.strip(), age, wishes_list))
15
16 return all_whishes
FFF
======================================================================
FAIL: test_full (test.TestRegex)
A single test to rule them all.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 121, in test_full
self.assertEqual(converted, parse_wishlist(modified_example))
AssertionError: Lists differ: [('3.14', 'Иван Иванов', '10', ('Плейстейша[110 chars]',))] != [(3.14, 'Иван Иванов', 10, ('Плейстейшан', [76 chars] ())]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', 10, ('Плейстейшан', [30 chars]че'))
- [('3.14',
- 'Иван Иванов',
- '10',
- ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^
+ [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^^^^^^^^^^^^^^^^^^^^^^^^^^
- ('1.94', 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? - - - - --------------------------
+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ())]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 123, in test_full
self.assertEqual(expected_full, parse_wishlist(full))
AssertionError: Lists differ: [(3.1[111 chars]43, ('Vancheto ot tretiq etaj',)), (6.9, 'Mr. [367 chars]м'))] != [(3.1[111 chars]43, ()), (1.0, 'Gospodin (he/him) Mitko', 12, [169 chars]м'))]
First differing element 1:
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))
(1.94, 'Georgi "Jorkata" Georgiev', 43, ())
First list contains 1 additional elements.
First extra element 4:
(99.9534412345, 'На мама сладкото ангелче', 3, ('Най-хубавото дървено конче (размер 1.5)', 'Най-страхотното лего (тамън отвориха лицензиран магазин)', 'Хлороформ'))
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
? --------------------------
+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ()),
- (6.9,
- 'Mr. D-r',
- 999,
- ('[Doctor stuff] Скалпел',
- '[Doctor stuff] Мехлем за дупе',
- '[Non-doctor stuff] Мехлем за дупе')),
- (1,
- 'Gospodin (he/him) Mitko',
+ (1.0, 'Gospodin (he/him) Mitko', 12, ('1',)),
? +++++ +++++++++++++
- 12,
- ('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test')),
(99.9534412345,
'На мама сладкото ангелче',
3,
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
'Хлороформ'))]
======================================================================
FAIL: test_half (test.TestRegex)
A test without the more edgy cases.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 111, in test_half
self.assertEqual(converted, parse_wishlist(modified_example))
AssertionError: Lists differ: [('3.14', 'Иван Иванов', '10', ('Плейстейша[110 chars]',))] != [(3.14, 'Иван Иванов', 10, ('Плейстейшан', [76 chars] ())]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', 10, ('Плейстейшан', [30 chars]че'))
- [('3.14',
- 'Иван Иванов',
- '10',
- ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^
+ [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^^^^^^^^^^^^^^^^^^^^^^^^^^
- ('1.94', 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? - - - - --------------------------
+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ())]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 113, in test_half
self.assertEqual(expected_half, parse_wishlist(half))
AssertionError: Lists differ: [(3.1[111 chars]43, ('Vancheto ot tretiq etaj',)), (99.9534412[149 chars]м'))] != [(3.1[111 chars]43, ()), (99.9534412345, 'На мама сладкото анг[123 chars]м'))]
First differing element 1:
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))
(1.94, 'Georgi "Jorkata" Georgiev', 43, ())
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
? --------------------------
+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ()),
(99.9534412345,
'На мама сладкото ангелче',
3,
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
'Хлороформ'))]
======================================================================
FAIL: test_modified_example (test.TestRegex)
A test similar to the example (just a lil' bit different).
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 101, in test_modified_example
self.assertEqual(converted, parse_wishlist(modified_example))
AssertionError: Lists differ: [('3.14', 'Иван Иванов', '10', ('Плейстейша[110 chars]',))] != [(3.14, 'Иван Иванов', 10, ('Плейстейшан', [76 chars] ())]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', 10, ('Плейстейшан', [30 chars]че'))
- [('3.14',
- 'Иван Иванов',
- '10',
- ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^
+ [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^^^^^^^^^^^^^^^^^^^^^^^^^^
- ('1.94', 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? - - - - --------------------------
+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ())]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 103, in test_modified_example
self.assertEqual(expected_example, parse_wishlist(modified_example))
AssertionError: Lists differ: [(3.1[82 chars]'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))] != [(3.1[82 chars]'Georgi "Jorkata" Georgiev', 43, ())]
First differing element 1:
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))
(1.94, 'Georgi "Jorkata" Georgiev', 43, ())
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))]
? --------------------------
+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ())]
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (failures=3)
Виктор Бечев
27.12.2023 12:27Бонус точка за решението. Не е защото сме почти роднини, I promise.
|