1import re
2
3coeff_name_age_patt = r'^\s*\((\d+\.?\d+)\)\s*([^\[\]\n]+)\[(\d+)\s*\S*]\s*$'
4wishes_patt = r'^\s*[-\*](.+)$'
5
6def parse_wishlist(wishlist):
7 result = []
8 kid_data = []
9 current_wishes = []
10 trailing_wishes = True
11 big_pattern = '|'.join([coeff_name_age_patt, wishes_patt])
12 info = re.findall(big_pattern, wishlist, re.MULTILINE)
13 for coeff, name, age, wish in info:
14 if name and not wish:
15 if current_wishes and not trailing_wishes:
16 kid_data.append(tuple(current_wishes))
17 current_wishes = []
18 trailing_wishes = False
19 if kid_data:
20 result.append(tuple(kid_data))
21 kid_data = []
22 kid_data.extend([float(coeff.strip()), name.strip(), int(age.strip())])
23 continue
24 if wish.strip():
25 current_wishes.append(wish.strip())
26 if current_wishes:
27 kid_data.append(tuple(current_wishes))
28 if kid_data:
29 result.append(tuple(kid_data))
30 return result
F..
======================================================================
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, ('Плейстейшан', [102 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, ('Vancheto ot tretiq etaj',))]
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[137 chars]taj',)), (6.9, 'Mr. D-r', 999, ('[Doctor stuff[341 chars]м'))] != [(3.1[137 chars]taj', '[Doctor stuff] Скалпел', '[Doctor stuff[277 chars]м'))]
First differing element 1:
(1.94[57 chars]taj',))
(1.94[57 chars]taj', '[Doctor stuff] Скалпел', '[Doctor stuff[110 chars]st'))
First list contains 2 additional elements.
First extra element 3:
(1, 'Gospodin (he/him) Mitko', 12, ('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test'))
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
- (6.9,
? ^
+ (1.94,
? ^ +
- 'Mr. D-r',
- 999,
+ 'Georgi "Jorkata" Georgiev',
+ 43,
+ ('Vancheto ot tretiq etaj',
- ('[Doctor stuff] Скалпел',
? ^
+ '[Doctor stuff] Скалпел',
? ^
'[Doctor stuff] Мехлем за дупе',
- '[Non-doctor stuff] Мехлем за дупе')),
? --
+ '[Non-doctor stuff] Мехлем за дупе',
- (1,
- 'Gospodin (he/him) Mitko',
- 12,
+ '1',
+ '2',
+ '3',
+ '4',
- ('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test')),
? --------------------
+ 'Malko mi pisva da go pisha toq test')),
(99.9534412345,
'На мама сладкото ангелче',
3,
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
'Хлороформ'))]
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (failures=1)
Виктор Бечев
29.12.2023 18:11Както казва Христо Стоичков - съмтайм уин, съмтайм люн. Все пак браво за фикса! :grin:
|
Мартин Кузманов
29.12.2023 15:02Оказва се, погрешно съм интерпретирал условието за скобите в началото, пропускайки възможността за интервали между '()' 😅, и с тази добавка
-> coeff_name_age_patt = r'^\s*\((\s*\d+\.?\d*\s*)\)\s*([^\[\]\n]+)\[\s*(\d+)\s*\S*]\s*$'
си работи абсолютно коректно за всички тестове:\
|
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
n | 3 | coeff_name_age_patt=r'^\s*\((\d+\.?\d+)\)\s*([^\[\]]+)\[(\d+)\s*\S*]\s*$' | n | 3 | coeff_name_age_patt = r'^\s*\((\d+\.?\d+)\)\s*([^\[\]\n]+)\[(\d+)\s*\S*]\s*$' |
4 | wishes_patt=r'^\s*[-\*](.+)$' | 4 | wishes_patt = r'^\s*[-\*](.+)$' | ||
5 | 5 | ||||
6 | def parse_wishlist(wishlist): | 6 | def parse_wishlist(wishlist): | ||
7 | result = [] | 7 | result = [] | ||
8 | kid_data = [] | 8 | kid_data = [] | ||
9 | current_wishes = [] | 9 | current_wishes = [] | ||
n | n | 10 | trailing_wishes = True | ||
10 | big_pattern='|'.join([coeff_name_age_patt, wishes_patt]) | 11 | big_pattern = '|'.join([coeff_name_age_patt, wishes_patt]) | ||
11 | info = re.findall(big_pattern, wishlist, re.MULTILINE) | 12 | info = re.findall(big_pattern, wishlist, re.MULTILINE) | ||
12 | for coeff, name, age, wish in info: | 13 | for coeff, name, age, wish in info: | ||
13 | if name and not wish: | 14 | if name and not wish: | ||
n | 14 | if current_wishes: | n | 15 | if current_wishes and not trailing_wishes: |
15 | kid_data.append(tuple(current_wishes)) | 16 | kid_data.append(tuple(current_wishes)) | ||
16 | current_wishes = [] | 17 | current_wishes = [] | ||
n | n | 18 | trailing_wishes = False | ||
17 | if kid_data: | 19 | if kid_data: | ||
18 | result.append(tuple(kid_data)) | 20 | result.append(tuple(kid_data)) | ||
19 | kid_data = [] | 21 | kid_data = [] | ||
n | 20 | kid_data.extend([float(coeff.strip()), name.strip(),int(age.strip())]) | n | 22 | kid_data.extend([float(coeff.strip()), name.strip(), int(age.strip())]) |
21 | continue | 23 | continue | ||
22 | if wish.strip(): | 24 | if wish.strip(): | ||
23 | current_wishes.append(wish.strip()) | 25 | current_wishes.append(wish.strip()) | ||
24 | if current_wishes: | 26 | if current_wishes: | ||
25 | kid_data.append(tuple(current_wishes)) | 27 | kid_data.append(tuple(current_wishes)) | ||
26 | if kid_data: | 28 | if kid_data: | ||
27 | result.append(tuple(kid_data)) | 29 | result.append(tuple(kid_data)) | ||
28 | return result | 30 | return result | ||
t | t | 31 | |||
32 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|