1import re
 2
 3def parse_wishlist(wished_gifts):
 4    pattern = re.compile(r'\(([\d.]+)\)\s*([^[]+)\s*\[(\d+)\s*(?:\s*\D*)?\]\s*((?:(?:\s*-|\s*\*\s*)(?:.*?)(?=\n[-*\n]|$))*)', re.DOTALL)
 5    wishlists_found = pattern.findall(wished_gifts)
 6    parsed_information = []
 7    for wishlist_information in wishlists_found:
 8        niceness, name, age, gifts_list = wishlist_information
 9        niceness = float(niceness.strip())
10        name = str(name.strip())
11        age = int(age.strip())
12
13        gifts = re.split(r'\s*\-\s*(?!\S)', gifts_list)
14        new_gifts = []
15        for gift in gifts:
16            collection = re.split(r'\s*\*\s*(?!\S)', gift)
17            for element in collection:
18                new_gifts.append(element)
19                
20        valid_gifts = []
21        for gift in new_gifts:
22            if gift != "":
23                valid_gifts.append(gift.strip())
24        parsed_information.append((niceness, name, age, tuple(valid_gifts)))
25    return parsed_information
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[142 chars])), (6.9, 'Mr. D-r', 999, ('[Doctor stuff] Ска[336 chars]м'))] != [(3.1[142 chars])), (1.0, 'Gospodin (he/him) Mitko', 12, ('1\n[217 chars]м'))]
First differing element 2:
(6.9, 'Mr. D-r', 999, ('[Doctor stuff] Ска[72 chars]пе'))
(1.0, 'Gospodin (he/him) Mitko', 12, ('1\n[50 chars]st'))
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',)),
-  (6.9,
-   'Mr. D-r',
-   999,
-   ('[Doctor stuff] Скалпел',
-    '[Doctor stuff] Мехлем за дупе',
-    '[Non-doctor stuff] Мехлем за дупе')),
-  (1,
+  (1.0,
?    ++
    'Gospodin (he/him) Mitko',
    12,
-   ('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test')),
?      ^^^^
+   ('1\n -2', '3', '4', 'Malko mi pisva da go pisha toq test')),
?      ^^^^
   (99.9534412345,
    'На мама сладкото ангелче',
    3,
    ('Най-хубавото дървено конче (размер 1.5)',
     'Най-страхотното лего (тамън отвориха лицензиран магазин)',
     'Хлороформ'))]
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (failures=1)
| f | 1 | import re | f | 1 | import re | 
| 2 | 2 | ||||
| 3 | def parse_wishlist(wished_gifts): | 3 | def parse_wishlist(wished_gifts): | ||
| 4 | pattern = re.compile(r'\(([\d.]+)\)\s*([^[]+)\s*\[(\d+)\s*(?:\s*\D*)?\]\s*((?:(?:\s*-|\s*\*\s*)(?:.*?)(?=\n[-*\n]|$))*)', re.DOTALL) | 4 | pattern = re.compile(r'\(([\d.]+)\)\s*([^[]+)\s*\[(\d+)\s*(?:\s*\D*)?\]\s*((?:(?:\s*-|\s*\*\s*)(?:.*?)(?=\n[-*\n]|$))*)', re.DOTALL) | ||
| 5 | wishlists_found = pattern.findall(wished_gifts) | 5 | wishlists_found = pattern.findall(wished_gifts) | ||
| 6 | parsed_information = [] | 6 | parsed_information = [] | ||
| 7 | for wishlist_information in wishlists_found: | 7 | for wishlist_information in wishlists_found: | ||
| 8 | niceness, name, age, gifts_list = wishlist_information | 8 | niceness, name, age, gifts_list = wishlist_information | ||
| 9 | niceness = float(niceness.strip()) | 9 | niceness = float(niceness.strip()) | ||
| 10 | name = str(name.strip()) | 10 | name = str(name.strip()) | ||
| 11 | age = int(age.strip()) | 11 | age = int(age.strip()) | ||
| 12 | 12 | ||||
| n | 13 | gifts = re.split(r'\s*\-\s*(?!\S)',gifts_list) | n | 13 | gifts = re.split(r'\s*\-\s*(?!\S)', gifts_list) | 
| 14 | new_gifts = [] | 14 | new_gifts = [] | ||
| 15 | for gift in gifts: | 15 | for gift in gifts: | ||
| 16 | collection = re.split(r'\s*\*\s*(?!\S)', gift) | 16 | collection = re.split(r'\s*\*\s*(?!\S)', gift) | ||
| 17 | for element in collection: | 17 | for element in collection: | ||
| 18 | new_gifts.append(element) | 18 | new_gifts.append(element) | ||
| 19 | 19 | ||||
| 20 | valid_gifts = [] | 20 | valid_gifts = [] | ||
| 21 | for gift in new_gifts: | 21 | for gift in new_gifts: | ||
| 22 | if gift != "": | 22 | if gift != "": | ||
| 23 | valid_gifts.append(gift.strip()) | 23 | valid_gifts.append(gift.strip()) | ||
| t | 24 | parsed_information.append((niceness,name,age,tuple(valid_gifts))) | t | 24 | parsed_information.append((niceness, name, age, tuple(valid_gifts))) | 
| 25 | return parsed_information | 25 | return parsed_information | 
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
 | 
 | |||||||||