Skip to content

Commit a869168

Browse files
committed
Refactoring. Using black code style
1 parent 73cffd2 commit a869168

14 files changed

+87
-69
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
5+
6+
7+
import re
58

69

710
def get_groups_seqs(text):
8-
import re
9-
return [match[0] for match in re.finditer(r'(.)\1+', text)]
11+
return [match[0] for match in re.finditer(r"(.)\1+", text)]
1012

1113

12-
if __name__ == '__main__':
13-
text = 'acccabcfbbffffffcccc'
14+
if __name__ == "__main__":
15+
text = "acccabcfbbffffffcccc"
1416
items = get_groups_seqs(text)
1517
print(items) # ['ccc', 'bb', 'ffffff', 'cccc']
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import re
88

99

1010
def find_longest(s):
11-
return max(re.findall(r'((\w+?)\2+)', s), key=lambda t: t[0].count(t[1]))
11+
return max(re.findall(r"((\w+?)\2+)", s), key=lambda t: t[0].count(t[1]))
1212

1313

14-
if __name__ == '__main__':
14+
if __name__ == "__main__":
1515
text = "helloworld world world hellohellohelloworldworld"
1616
print(find_longest(text)) # ('hellohellohello', 'hello')
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7-
text = '"41", "1,234", "6,368,745", "12,34,567", "1234", "123,456"'
8-
97
import re
10-
print(re.findall(r'"(\d{,3}(?:,\d{3})*)"', text)) # ['41', '1,234', '6,368,745', '123,456']
8+
9+
10+
text = '"41", "1,234", "6,368,745", "12,34,567", "1234", "123,456"'
11+
items = re.findall(r'"(\d{,3}(?:,\d{3})*)"', text)
12+
print(items)
13+
# ['41', '1,234', '6,368,745', '123,456']
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
5+
6+
7+
import re
58

69

710
def template(template_text, map_values):
8-
import re
9-
for match in re.findall('<(.+?)>', text):
11+
for match in re.findall("<(.+?)>", text):
1012
if match in map_values:
11-
template_text = template_text.replace('<' + match + '>', map_values[match])
13+
template_text = template_text.replace("<" + match + ">", map_values[match])
1214

1315
return template_text
1416

1517

16-
if __name__ == '__main__':
17-
text = 'Я купил тебе, <имя>, <любая фигня>!'
18+
if __name__ == "__main__":
19+
text = "Я купил тебе, <имя>, <любая фигня>!"
1820

19-
new_text = template(text, {'имя': 'милая', 'любая фигня': 'вишню'})
21+
new_text = template(text, {"имя": "милая", "любая фигня": "вишню"})
2022
print(new_text)
2123

22-
new_text = template(text, {'имя': 'Иван Петрович', 'любая фигня': 'большой андронный коллайдер'})
24+
new_text = template(
25+
text, {"имя": "Иван Петрович", "любая фигня": "большой андронный коллайдер"}
26+
)
2327
print(new_text)

regexp_examples/pattern_debug.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import re
@@ -19,7 +19,7 @@
1919
print()
2020
print(type(pattern), pattern)
2121

22-
print('\n')
22+
print("\n")
2323

2424
pattern = re.compile(r"\d{2,6}", re.DEBUG)
2525
# MAX_REPEAT 2 6
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'IPETRASH'
4+
__author__ = "IPETRASH"
55

66

7-
def remove_punctuation(text):
8-
import re
9-
import string
10-
pattern = re.compile('[%s]' % re.escape(string.punctuation))
7+
import re
8+
import string
9+
1110

12-
return pattern.sub('', text)
11+
def remove_punctuation(text):
12+
pattern = re.compile("[%s]" % re.escape(string.punctuation))
13+
return pattern.sub("", text)
1314

1415

15-
if __name__ == '__main__':
16-
text = 'classes, freq = defaultdict(lambda:0), defaultdict(lambda:0)'
17-
print(remove_punctuation(text)) # classes freq defaultdictlambda0 defaultdictlambda0
16+
if __name__ == "__main__":
17+
text = "classes, freq = defaultdict(lambda:0), defaultdict(lambda:0)"
18+
print(remove_punctuation(text))
19+
# classes freq defaultdictlambda0 defaultdictlambda0

regexp_examples/replace_on_upper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7-
"""Скрипт, использую регулярные выражения, находит текст по шаблону и
7+
"""
8+
Скрипт, использую регулярные выражения, находит текст по шаблону и
89
заменяет его на аналогичный, но в верхнем регистре.
910
"""
1011

1112

13+
import re
14+
15+
1216
text = """
1317
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
1418
<xs:element name="Root">
@@ -35,6 +39,5 @@
3539
</xs:schema>
3640
"""
3741

38-
import re
3942
text = re.sub('name="(.+?)"', lambda x: 'name="{}"'.format(x.group(1).upper()), text)
4043
print(text)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

77
import re
88
import requests
99

1010

11-
rs = requests.get('http://shedevr.org.ru/games/Sol/BoF-Solution.txt')
12-
rs.encoding = 'cp1251'
11+
rs = requests.get("http://shedevr.org.ru/games/Sol/BoF-Solution.txt")
12+
rs.encoding = "cp1251"
1313

14-
items = re.findall(r'.+?\.\r\n-------------------\r\n', rs.text)
15-
print('Stages:', len(items))
14+
items = re.findall(r".+?\.\r\n-------------------\r\n", rs.text)
15+
print("Stages:", len(items))
1616
# print(items)

regexp_examples/split_by_case.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
5+
6+
7+
import re
58

69

710
def split_by_case(text: str) -> list:
8-
import re
9-
return re.findall(r'[a-zA-Z][a-z]+', text)
11+
return re.findall(r"[a-zA-Z][a-z]+", text)
1012

1113

12-
if __name__ == '__main__':
13-
print(split_by_case('fadeInLeft'))
14-
print(split_by_case('CharSequence'))
15-
print(split_by_case('String'))
16-
print(split_by_case('compareToIgnoreCase'))
14+
if __name__ == "__main__":
15+
print(split_by_case("fadeInLeft"))
16+
print(split_by_case("CharSequence"))
17+
print(split_by_case("String"))
18+
print(split_by_case("compareToIgnoreCase"))
1719

18-
assert split_by_case('fadeInLeft') == ['fade', 'In', 'Left']
19-
assert split_by_case('CharSequence') == ['Char', 'Sequence']
20-
assert split_by_case('String') == ['String']
21-
assert split_by_case('compareToIgnoreCase') == ['compare', 'To', 'Ignore', 'Case']
20+
assert split_by_case("fadeInLeft") == ["fade", "In", "Left"]
21+
assert split_by_case("CharSequence") == ["Char", "Sequence"]
22+
assert split_by_case("String") == ["String"]
23+
assert split_by_case("compareToIgnoreCase") == ["compare", "To", "Ignore", "Case"]
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
__author__ = 'ipetrash'
4+
__author__ = "ipetrash"
55

66

7-
text = 'Иванов Иван Иванович'
7+
import re
88

9-
replace_match = lambda x: '{} {}. {}.'.format(x[1], x[2][0], x[3][0])
109

11-
import re
12-
new_text = re.sub(r'(\w+) (\w+) (\w+)', replace_match, text)
10+
text = "Иванов Иван Иванович"
11+
12+
replace_match = lambda x: "{} {}. {}.".format(x[1], x[2][0], x[3][0])
13+
new_text = re.sub(r"(\w+) (\w+) (\w+)", replace_match, text)
1314
print(new_text) # Иванов И. И.

0 commit comments

Comments
 (0)